Class: Yorisoi::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/yorisoi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ Builder

Returns a new instance of Builder.



45
46
47
48
49
# File 'lib/yorisoi.rb', line 45

def initialize(object_name, object, template, options)
  self.default_tag = options[:builder_tag] || {}
  store_error(object)
  super
end

Instance Attribute Details

#error_wrapperObject

Returns the value of attribute error_wrapper.



42
43
44
# File 'lib/yorisoi.rb', line 42

def error_wrapper
  @error_wrapper
end

#errors_wrapperObject

Returns the value of attribute errors_wrapper.



42
43
44
# File 'lib/yorisoi.rb', line 42

def errors_wrapper
  @errors_wrapper
end

#invalid_wrapperObject

Returns the value of attribute invalid_wrapper.



42
43
44
# File 'lib/yorisoi.rb', line 42

def invalid_wrapper
  @invalid_wrapper
end

#stored_errorObject

Returns the value of attribute stored_error.



42
43
44
# File 'lib/yorisoi.rb', line 42

def stored_error
  @stored_error
end

#wrapperObject

Returns the value of attribute wrapper.



42
43
44
# File 'lib/yorisoi.rb', line 42

def wrapper
  @wrapper
end

Instance Method Details

#check_box(attribute, options = {}, label_and_value = %w(1 1), unchecked_value = '0') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/yorisoi.rb', line 68

def check_box(attribute, options = {}, label_and_value = %w(1 1), unchecked_value = '0')
  wrap_field(attribute, options) do
    if label_and_value.is_a?(Array)
      @template.check_box(@object_name, attribute,
          objectify_options(options.merge(around: around_proc(label_and_value.first))),
          label_and_value.last, unchecked_value)
    else
      super
    end
  end
end

#check_boxes(attribute, options = {}, labels_and_values = [['', '1']], children_wrapper: check_box_wrapper, child_wrapper: check_box_child_wrapper) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/yorisoi.rb', line 81

def check_boxes(attribute, options = {}, labels_and_values = [['', '1']],
    children_wrapper: check_box_wrapper, child_wrapper: check_box_child_wrapper)
  wrap_field(attribute, options.merge(no_wrap: true)) do
    children_wrapper.(labels_and_values.map do |label_and_value|
      child_wrapper.(check_box(attribute, options.merge(multiple: true, no_errors: true), label_and_value, nil), attribute)
    end.join.html_safe, attribute)
  end
end

#default_tag=(tags) ⇒ Object



91
92
93
94
95
96
# File 'lib/yorisoi.rb', line 91

def default_tag=(tags)
  self.wrapper = tags[:wrapper] || default_wrapper
  self.invalid_wrapper = tags[:invalid_wrapper] || default_invalid_wrapper
  self.errors_wrapper = tags[:errors_wrapper] || default_errors_wrapper
  self.error_wrapper = (tags[:error_wrapper] || default_error_wrapper).flip.curry
end

#delete_stored_error!(attribute) ⇒ Object



164
165
166
# File 'lib/yorisoi.rb', line 164

def delete_stored_error!(attribute)
  @stored_error.delete(attribute)
end

#label(attribute, options = {}) ⇒ Object



99
100
101
# File 'lib/yorisoi.rb', line 99

def label(attribute, options = {})
  wrap_field(attribute, options.merge(no_errors: true)) { super }
end

#radio_button(attribute, label_and_value, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/yorisoi.rb', line 104

def radio_button(attribute, label_and_value, options = {})
  wrap_field(attribute, options) do
    if label_and_value.is_a?(Array)
      @template.radio_button(@object_name, attribute, label_and_value.last,
          objectify_options(options.merge(around: around_proc(label_and_value.first))))
    else
      super
    end
  end
end

#radio_buttons(attribute, options = {}, labels_and_values = [['', '1']], children_wrapper: radio_button_wrapper, child_wrapper: radio_button_child_wrapper) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/yorisoi.rb', line 116

def radio_buttons(attribute, options = {}, labels_and_values = [['', '1']],
    children_wrapper: radio_button_wrapper, child_wrapper: radio_button_child_wrapper)
  wrap_field(attribute, options.merge(no_wrap: true)) do
    children_wrapper.(labels_and_values.map do |label_and_value|
      child_wrapper.(radio_button(attribute, label_and_value, options.merge(no_errors: true)), attribute)
    end.join.html_safe, attribute)
  end
end

#remnantObject



126
127
128
129
130
131
132
133
# File 'lib/yorisoi.rb', line 126

def remnant
  return if @stored_error.blank?
  errors_wrapper.(@stored_error.map { |attribute, *errors|
    errors.map { |message|
      error_wrapper.(attribute, message)
    }
  }.flatten.join.html_safe, :remnant)
end

#select(attribute, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



136
137
138
# File 'lib/yorisoi.rb', line 136

def select(attribute, choices = nil, options = {}, html_options = {}, &block)
  wrap_field(attribute, options) { super }
end

#store_error(object) ⇒ Object



52
53
54
# File 'lib/yorisoi.rb', line 52

def store_error(object)
  @stored_error = object.errors.presence || {}
end

#wrap_field(attribute, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/yorisoi.rb', line 141

def wrap_field(attribute, options={})
  no_errors = options.delete(:no_errors)
  no_wrap = options.delete(:no_wrap)

  if (error_html = pick_error(attribute)).present?
    if no_errors
      yield
    else
      delete_stored_error!(attribute)
      yield + error_html
    end
  else
    no_wrap ? yield : wrapper.(yield, attribute)
  end.html_safe
end

#write_error(attribute) ⇒ Object



158
159
160
161
# File 'lib/yorisoi.rb', line 158

def write_error(attribute)
  delete_stored_error!(attribute)
  pick_error(attribute)
end