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
- #file_field(attribute = nil, options = {}) ⇒ Object
- #render_form_element(attribute, options) ⇒ Object
- #select(attribute, choices = nil, chosen = nil, options = {}, html_options = {}) ⇒ Object
Instance Method Details
#check_box(attribute, options = {}) ⇒ Object
271 272 273 274 275 276 277 278 279 |
# File 'lib/asbru/form_builder.rb', line 271 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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/asbru/form_builder.rb', line 215 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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/asbru/form_builder.rb', line 197 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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/asbru/form_builder.rb', line 249 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/asbru/form_builder.rb', line 92 def (attribute, collection, value_method, text_method, , multiple = false) collection.map do |item| { label: if [:translate_options] I18n.t( item.send(text_method), scope: "activerecord.attributes.#{@object.class.name.downcase}.#{attribute}_options" ) else item.send(text_method) end, value: item.send(value_method), name: [: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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/asbru/form_builder.rb', line 80 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 |
#file_field(attribute = nil, options = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/asbru/form_builder.rb', line 169 def file_field(attribute = nil, = {}) = { name: tag_name_for(attribute), errors: errors(attribute), errorIcon: error_icon, type: 'file', attachmentDeletionKey: tag_name_for("_delete_#{attribute}"), ** } if @object.present? && attribute.present? [:label] ||= I18n.t(attribute, scope: "activerecord.attributes.#{@object.class.name.downcase}") = @object.send(attribute) if .present? ['incommingFile'] = { name: .filename, size: .send('byte_size'), type: .content_type } end end Asbru::Components::Savage.send "#{Asbru.config.form_builder_prefix}_element", ** end |
#render_form_element(attribute, options) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/asbru/form_builder.rb', line 48 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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/asbru/form_builder.rb', line 234 def select(attribute, choices = nil, chosen = nil, = {}, = {}) render_form_element( attribute, .merge().merge( { type: 'select', options: (attribute, choices, chosen) } ) ) end |