Class: Yorisoi::Builder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Yorisoi::Builder
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/yorisoi.rb
Instance Attribute Summary collapse
-
#error_wrapper ⇒ Object
Returns the value of attribute error_wrapper.
-
#errors_wrapper ⇒ Object
Returns the value of attribute errors_wrapper.
-
#invalid_wrapper ⇒ Object
Returns the value of attribute invalid_wrapper.
-
#stored_error ⇒ Object
Returns the value of attribute stored_error.
-
#wrapper ⇒ Object
Returns the value of attribute wrapper.
Instance Method Summary collapse
- #check_box(attribute, options = {}, label_and_value = %w(1 1), unchecked_value = '0') ⇒ Object
- #check_boxes(attribute, options = {}, labels_and_values = [['', '1']], children_wrapper: check_box_wrapper, child_wrapper: check_box_child_wrapper) ⇒ Object
- #default_tag=(tags) ⇒ Object
- #delete_stored_error!(attribute) ⇒ Object
-
#initialize(object_name, object, template, options) ⇒ Builder
constructor
A new instance of Builder.
- #label(attribute, options = {}) ⇒ Object
- #radio_button(attribute, label_and_value, options = {}) ⇒ Object
- #radio_buttons(attribute, options = {}, labels_and_values = [['', '1']], children_wrapper: radio_button_wrapper, child_wrapper: radio_button_child_wrapper) ⇒ Object
- #remnant ⇒ Object
- #select(attribute, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
- #store_error(object) ⇒ Object
- #wrap_field(attribute, options = {}) ⇒ Object
- #write_error(attribute) ⇒ Object
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, ) self.default_tag = [:builder_tag] || {} store_error(object) super end |
Instance Attribute Details
#error_wrapper ⇒ Object
Returns the value of attribute error_wrapper.
42 43 44 |
# File 'lib/yorisoi.rb', line 42 def error_wrapper @error_wrapper end |
#errors_wrapper ⇒ Object
Returns the value of attribute errors_wrapper.
42 43 44 |
# File 'lib/yorisoi.rb', line 42 def errors_wrapper @errors_wrapper end |
#invalid_wrapper ⇒ Object
Returns the value of attribute invalid_wrapper.
42 43 44 |
# File 'lib/yorisoi.rb', line 42 def invalid_wrapper @invalid_wrapper end |
#stored_error ⇒ Object
Returns the value of attribute stored_error.
42 43 44 |
# File 'lib/yorisoi.rb', line 42 def stored_error @stored_error end |
#wrapper ⇒ Object
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, = {}, label_and_value = %w(1 1), unchecked_value = '0') wrap_field(attribute, ) do if label_and_value.is_a?(Array) @template.check_box(@object_name, attribute, (.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, = {}, labels_and_values = [['', '1']], children_wrapper: check_box_wrapper, child_wrapper: check_box_child_wrapper) wrap_field(attribute, .merge(no_wrap: true)) do children_wrapper.(labels_and_values.map do |label_and_value| child_wrapper.(check_box(attribute, .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=() self.wrapper = [:wrapper] || default_wrapper self.invalid_wrapper = [:invalid_wrapper] || default_invalid_wrapper self.errors_wrapper = [:errors_wrapper] || default_errors_wrapper self.error_wrapper = ([: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, = {}) wrap_field(attribute, .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 (attribute, label_and_value, = {}) wrap_field(attribute, ) do if label_and_value.is_a?(Array) @template.(@object_name, attribute, label_and_value.last, (.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 (attribute, = {}, labels_and_values = [['', '1']], children_wrapper: , child_wrapper: ) wrap_field(attribute, .merge(no_wrap: true)) do children_wrapper.(labels_and_values.map do |label_and_value| child_wrapper.((attribute, label_and_value, .merge(no_errors: true)), attribute) end.join.html_safe, attribute) end end |
#remnant ⇒ Object
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 { || error_wrapper.(attribute, ) } }.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, = {}, = {}, &block) wrap_field(attribute, ) { 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, ={}) no_errors = .delete(:no_errors) no_wrap = .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 |