Class: Asbru::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
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

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, options = {})
  render_form_element(
      attribute, { type: 'hidden', value: 'false', label: false }
    ).concat(
    render_form_element(
        attribute, options.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,
    options = {},
    html_options = {})
  render_form_element(attribute, options.merge(html_options).merge(
    {
      type: 'checkbox',
      options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options,
          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 collection_radio_buttons(attribute,
    collection,
    value_method,
    text_method,
    options = {},
    html_options = {})
  render_form_element(attribute, options.merge(html_options).merge(
    {
      type: 'radio',
      options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options, 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,
    options = {},
    html_options = {})
  if options[:without_react].present?
    super
  else
    render_form_element(attribute, options.merge(html_options).merge(
      {
        type: 'select',
        options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options)
      }
    ))
  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 create_collection_options(attribute,
    collection,
    value_method,
    text_method,
    options,
    multiple = false)
  collection.map do |item|
    {
      label: if options[: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: options[: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 create_options(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.messages.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, options = {})
  options =  { name: tag_name_for(attribute),
      errors: errors(attribute),
      errorIcon: error_icon,
      type: 'file',
      attachmentDeletionKey: tag_name_for("_delete_#{attribute}"),
      **options
  }

  if @object.present? && attribute.present?
    options[:label] ||= I18n.t(attribute,
        scope: "activerecord.attributes.#{@object.class.name.downcase}")

    attachment = @object.send(attribute)
    if attachment.present?
      options['incommingFile'] = {
        name: attachment.filename,
        size: attachment.send('byte_size'),
        type: attachment.content_type
      }
    end
  end

  Asbru::Components::Savage.send "#{Asbru.config.form_builder_prefix}_element",
      **options
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, options)
  new_options = options.reverse_merge(
    {
      name: tag_name_for(attribute),
      errors: errors(attribute),
      errorIcon: error_icon,
    }
  )

  if @object.present? && attribute.present?
    if options[: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?
        new_options[:value] = I18n.l(@object.send(attribute),
            format: I18n.t('date_format.default'))
      else
        new_options[:value] = @object.send(attribute)
      end
    end
    if options[:label].nil?
      new_options[:label] = I18n.t(attribute,
          scope: "activerecord.attributes.#{@object.class.name.downcase}")
    end
  end

  Asbru::Components::Savage.send "#{Asbru.config.form_builder_prefix}_element",
      **new_options
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,
    options = {},
    html_options = {})
  render_form_element(
      attribute, options.merge(html_options).merge(
      {
        type: 'select',
        options: create_options(attribute, choices, chosen)
      }
    )
    )
end