Class: Asbru::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Asbru::FormBuilder
- Defined in:
- lib/asbru/form_builder.rb
Defined Under Namespace
Modules: Helper
Constant Summary collapse
- BASIC_FORM_FIELDS =
[ { submit: { text: 'Submit', type: 'submit' } }, { date_field: { type: 'datepicker' } }, { password_field: { type: 'password' } }, { text_area: { type: 'textarea' } }, { email_field: { type: 'email' } }, { check_box: { type: 'checkbox' } }, { file_field: { type: 'file' } }, { text_field: {} }, { telephone_field: { type: 'tel'}} ].freeze
Instance Method Summary collapse
- #check_box(attribute, options = {}) ⇒ Object
- #collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #create_collection_options(attribute, collection, value_method, text_method, _options, multiple = false) ⇒ Object
- #create_options(attribute, choices, chosen) ⇒ Object
- #errors(name) ⇒ Object
- #render_form_element(attribute, options) ⇒ Object
- #select(attribute, choices = nil, chosen = nil, options = {}, html_options = {}) ⇒ Object
- #tag_name_for(method, multiple = false) ⇒ Object
Instance Method Details
#check_box(attribute, options = {}) ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/asbru/form_builder.rb', line 205 def check_box(attribute, = {}) render_form_element( attribute, { type: 'hidden', value: 'false', label: false } ).concat( render_form_element( attribute, .merge({ type: 'checkbox' }) ) ) end |
#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/asbru/form_builder.rb', line 149 def collection_check_boxes(attribute, collection, value_method, text_method, = {}, = {}) render_form_element(attribute, .merge().merge( { type: 'checkbox', options: (attribute, collection, value_method, text_method, , true) } )) end |
#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/asbru/form_builder.rb', line 131 def (attribute, collection, value_method, text_method, = {}, = {}) render_form_element(attribute, .merge().merge( { type: 'radio', options: (attribute, collection, value_method, text_method, , false) } )) end |
#collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/asbru/form_builder.rb', line 183 def collection_select(attribute, collection, value_method, text_method, = {}, = {}) if [:without_react].present? super else render_form_element(attribute, .merge().merge( { type: 'select', options: (attribute, collection, value_method, text_method, ) } )) end end |
#create_collection_options(attribute, collection, value_method, text_method, _options, multiple = false) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/asbru/form_builder.rb', line 100 def (attribute, collection, value_method, text_method, , multiple = false) collection.map do |item| { label: item.send(text_method), value: item.send(value_method), name: tag_name_for(attribute, multiple), checked: if @object.nil? false else if @object.send(attribute).is_a?(Array) @object.send(attribute).include?(item.send(value_method)) else @object.send(attribute).to_s == item.send(value_method).to_s end end } end end |
#create_options(attribute, choices, chosen) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/asbru/form_builder.rb', line 88 def (attribute, choices, chosen) choices.to_h.map do |key, value| { label: key, value: value, name: tag_name_for(attribute), checked: @object.nil? ? false : @object.send(attribute) == chosen } end end |
#errors(name) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/asbru/form_builder.rb', line 41 def errors(name) if object.respond_to?(:errors) && !(name.nil? || object.errors[name].empty?) object.errors..dig(name) end end |
#render_form_element(attribute, options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/asbru/form_builder.rb', line 56 def render_form_element(attribute, ) = .reverse_merge( { name: tag_name_for(attribute), errors: errors(attribute), errorIcon: error_icon } ) if @object.present? && attribute.present? if [:value].nil? # if type of column is date, use default date format if @object.class.try(:columns_hash).present? && @object.class .columns_hash[attribute.to_s] .try(:type) == :date && @object.send(attribute).present? [:value] = I18n.l(@object.send(attribute), format: I18n.t('date_format.default')) else [:value] = @object.send(attribute) end end if [:label].nil? [:label] = I18n.t(attribute, scope: "activerecord.attributes.#{@object.class.name.downcase}") end end Asbru::Components::Savage.send "#{Asbru.config.form_builder_prefix}_element", ** end |
#select(attribute, choices = nil, chosen = nil, options = {}, html_options = {}) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/asbru/form_builder.rb', line 168 def select(attribute, choices = nil, chosen = nil, = {}, = {}) render_form_element( attribute, .merge().merge( { type: 'select', options: (attribute, choices, chosen) } ) ) end |
#tag_name_for(method, multiple = false) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/asbru/form_builder.rb', line 48 def tag_name_for(method, multiple = false) name = ActionView::Helpers::Tags::TextField.new(object_name, method, {}) .send(:tag_name) name += '[]' if multiple name end |