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



205
206
207
208
209
210
211
212
213
# File 'lib/asbru/form_builder.rb', line 205

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



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,
    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



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 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



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,
    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



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 create_collection_options(attribute,
    collection,
    value_method,
    text_method,
    _options,
    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 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

#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, 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



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,
    options = {},
    html_options = {})
  render_form_element(
      attribute, options.merge(html_options).merge(
      {
        type: 'select',
        options: create_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