Module: BarkestCore::FormHelper

Defined in:
app/helpers/barkest_core/form_helper.rb

Overview

This module contains a number of utility functions to make generating forms even easier.

Instance Method Summary collapse

Instance Method Details

#checkbox_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a checkbox.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/helpers/barkest_core/form_helper.rb', line 321

def checkbox_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options({ class: 'checkbox', field_class: ''}.merge(options))

  if gopt[:h_align]
    gopt[:class] = gopt[:class].blank? ?
        "col-sm-#{12-gopt[:h_align]} col-sm-offset-#{gopt[:h_align]}" :
        "#{gopt[:class]} col-sm-#{12-gopt[:h_align]} col-sm-offset-#{gopt[:h_align]}"
  end

  lbl = f.label method do
    f.check_box(method, fopt) +
        h(lopt[:text] || method.to_s.humanize) +
        if lopt[:small_text]
          "<small>#{h lopt[:small_text]}</small>"
        else
          ''
        end.html_safe
  end

  "<div class=\"#{gopt[:h_align] ? 'row' : 'form-group'}\"><div class=\"#{gopt[:class]}\">#{lbl}</div></div>".html_safe
end

#currency_field(f, method, options = {}) ⇒ Object

Creates a currency input field.

Specify the currency_symbol if you want it to be something other than ‘$’. Additional options are passed on to the text field.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/helpers/barkest_core/form_helper.rb', line 191

def currency_field(f, method, options = {})
  # get the symbol for the field.
  sym = '$'
  if options[:currency_symbol]
    sym = options[:currency_symbol]
    options.except! :currency_symbol
  end

  # get the value
  if (val = f.object.send(method))
    options[:value] = number_with_precision val, precision: 2, delimiter: ','
  end

  # build the field
  fld = f.text_field(method, options)

  # return the value.
  "<div class=\"input-symbol\"><span>#{h sym}</span>#{fld}</div>".html_safe
end

#currency_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a currency field.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



264
265
266
267
268
269
# File 'app/helpers/barkest_core/form_helper.rb', line 264

def currency_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  fld = gopt[:wrap].call(currency_field(f, method, fopt))
  form_group lbl, fld, gopt
end

#date_picker_field(f, method, options = {}) ⇒ Object

Creates a date picker selection field using a bootstrap input group.

Valid options:

  • input_group_size Valid optional sizes are ‘small’ or ‘large’.

  • readonly Set to true to make the input field read only.

  • pre_calendar Set to true to put a calendar icon before the input field.

  • post_calendar Set to true to put a calendar icon after the input field. This is the default setting.

  • pre_label Set to a text value to put a label before the input field. Replaces pre_calendar if specified.

  • post_label Set to a text value to put a label after the input field. Replaces post_calendar if specified.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
79
80
81
82
# File 'app/helpers/barkest_core/form_helper.rb', line 22

def date_picker_field(f, method, options = {})
  options = {
      class:            'form-control',
      read_only:        false,
      pre_calendar:     false,
      pre_label:        nil,
      post_calendar:    true,
      post_label:       false,
      attrib_val:       { },
      style:            { },
      input_group_size: ''
  }.merge(options)

  style = ''
  options[:style].each { |k,v| style += "#{k}: #{v};" }

  attrib = options[:attrib_val]
  attrib[:class] = options[:class]
  attrib[:style] = style
  attrib[:readonly] = 'readonly' if options[:read_only]

  if options[:input_group_size] == 'sm' || options[:input_group_size] == 'small' || options[:input_group_size] == 'input-group-sm'
    options[:input_group_size] = 'input-group-sm'
  elsif options[:input_group_size] == 'lg' || options[:input_group_size] == 'large' || options[:input_group_size] == 'input-group-lg'
    options[:input_group_size] = 'input-group-lg'
  else
    options[:input_group_size] = ''
  end


  attrib[:value] = f.object.send(method).to_s(:date4) if f.object.send(method)
  fld = f.text_field(method, attrib)

  # must have at least one attachment, default to post-calendar.
  options[:post_calendar] = true unless options[:pre_calendar] || options[:pre_label] || options[:post_label]

  # labels override calendars.
  options[:pre_calendar] = false if options[:pre_label]
  options[:post_calendar] = false if options[:post_label]

  # construct the prefix
  if options[:pre_calendar]
    pre = '<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>'
  elsif options[:pre_label]
    pre = "<span class=\"input-group-addon\">#{h options[:pre_label]}</span>"
  else
    pre = ''
  end

  # construct the postfix
  if options[:post_calendar]
    post = '<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>'
  elsif options[:post_label]
    post = "<span class=\"input-group-addon\">#{h options[:post_label]}</span>"
  else
    post = ''
  end

  # and then the return value.
  "<div class=\"input-group date #{options[:input_group_size]}\">#{pre}#{fld}#{post}</div>".html_safe
end

#date_picker_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a date picker field.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



291
292
293
294
295
296
# File 'app/helpers/barkest_core/form_helper.rb', line 291

def date_picker_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  fld = gopt[:wrap].call(date_picker_field(f, method, fopt))
  form_group lbl, fld, gopt
end

#label_with_small(f, method, text = nil, options = {}, &block) ⇒ Object

Creates a label followed by small text.

Set the :small_text option to include small text.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/helpers/barkest_core/form_helper.rb', line 215

def label_with_small(f, method, text=nil, options = {}, &block)
  if text.is_a?(Hash)
    options = text
    text = nil
  end
  small_text = options.delete(:small_text)
  text = text || options.delete(:text) || method.to_s.humanize.capitalize
  lbl = f.label(method, text, options, &block) #do
  #  contents = text
  #  contents += "<small>#{h small_text}</small>" if small_text
  #  contents += yield if block_given?
  #  contents.html_safe
  #end
  lbl += "&nbsp;<small>(#{h small_text})</small>".html_safe if small_text
  lbl.html_safe
end

#multi_input_field(f, methods, options = {}) ⇒ Object

Creates an input group containing multiple input fields.

Valid options:

  • input_group_size Valid optional sizes are ‘small’ or ‘large’.

  • readonly Set to true to make the input field read only.

  • width Set to any value to set the width of the overall field in percent. Default is 100%.

  • width_1width_N Set the width of the child field to a specific percent. Defaults to equal amount for all children.

  • placeholder_1placeholder_N Set the placeholder of the child field. Defaults to the method name.

Raises:

  • (ArgumentError)


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/helpers/barkest_core/form_helper.rb', line 98

def multi_input_field(f, methods, options = {})
  raise ArgumentError.new('methods must respond to :count') unless methods.respond_to?(:count)

  options = {
      class:            'form-control',
      read_only:        false,
      attrib_val:       { },
      style:            { },
      input_group_size: ''
  }.merge(options)

  style = ''
  options[:style].each do |k,v|
    if k.to_s == 'width'
      options[:input_group_width] = "width: #{v};"
    else
      style += "#{k}: #{v};"
    end
  end

  options[:input_group_width] ||= 'width: 100%;'

  attrib = options[:attrib_val]
  attrib[:class] = options[:class]
  attrib[:readonly] = 'readonly' if options[:read_only]

  if options[:input_group_size] == 'sm' || options[:input_group_size] == 'small' || options[:input_group_size] == 'input-group-sm'
    options[:input_group_size] = 'input-group-sm'
  elsif options[:input_group_size] == 'lg' || options[:input_group_size] == 'large' || options[:input_group_size] == 'input-group-lg'
    options[:input_group_size] = 'input-group-lg'
  else
    options[:input_group_size] = ''
  end

  fld = []

  remaining_width = 100.0
  width = (100.0 / methods.count).round(2)

  builder = Proc.new do |method,label,index|
    num = index + 1
    # if user specified width, use it.
    width = options[:"width_#{num}"].to_s.to_f

    # otherwise compute the width.
    if width <= 0
      width = (remaining_width / (methods.count - index)).round(2)
      width = remaining_width if width > remaining_width || index == methods.count - 1
    end

    remaining_width -= width
    attrib[:style] = style + "width: #{width}%;"
    attrib[:value] = f.object.send(method)

    ph = options[:"placeholder_#{num}"] || (label.blank? ? method.to_s.humanize : label)
    attrib[:placeholder] = ph

    options.each do |opt_key, opt_val|
      attr_key,_,fld_num = opt_key.to_s.rpartition('_')
      if fld_num.to_i == num
        unless %w(value width placeholder).include?(attr_key)
          if %w(class style).include?(attr_key)
            attrib[:class] += ' ' + opt_val.to_s
          else
            attrib[attr_key.to_sym] = opt_val.to_s
          end
        end
      end
    end

    fld << f.text_field(method, attrib)
  end

  if methods.is_a?(Array)
    methods.each_with_index do |method, index|
      builder.call method, method.to_s.humanize, index
    end
  elsif methods.is_a?(Hash)
    methods.each_with_index do |(method,label), index|
      builder.call method, label.to_s, index
    end
  else
    raise ArgumentError.new('methods must either be an array or a hash')
  end

  "<div class=\"input-group #{options[:input_group_size]}\" style=\"#{options[:input_group_width]}\">#{fld.join}</div>".html_safe
end

#multi_input_form_group(f, methods, options = {}) ⇒ Object

Creates a form group including a label and a multiple input field.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



304
305
306
307
308
309
310
311
312
313
# File 'app/helpers/barkest_core/form_helper.rb', line 304

def multi_input_form_group(f, methods, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lopt[:text] ||= gopt[:label]
  if lopt[:text].blank?
    lopt[:text] = methods.map {|k,_| k.to_s.humanize }.join(', ')
  end
  lbl = f.label_with_small methods.map{|k,_| k}.first, lopt[:text], lopt
  fld = gopt[:wrap].call(multi_input_field(f, methods, fopt))
  form_group lbl, fld, gopt
end

#select_form_group(f, method, collection, value_method = nil, text_method = nil, options = {}) ⇒ Object

Creates a form group including a label and a collection select field.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'app/helpers/barkest_core/form_helper.rb', line 349

def select_form_group(f, method, collection, value_method = nil, text_method = nil, options = {})
  gopt, lopt, fopt = split_form_group_options({ field_include_blank: true }.merge(options))
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  value_method ||= :to_s
  text_method ||= :to_s
  opt = {}
  [:include_blank, :prompt, :include_hidden].each do |attr|
    if fopt[attr] != nil
      opt[attr] = fopt[attr]
      fopt.except! attr
    end
  end
  fld = gopt[:wrap].call(f.collection_select(method, collection, value_method, text_method, opt, fopt))
  form_group lbl, fld, gopt
end

#static_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a static field. The static field is simple a readonly input field with no name.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



278
279
280
281
282
283
# File 'app/helpers/barkest_core/form_helper.rb', line 278

def static_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  fld = gopt[:wrap].call("<input type=\"text\" class=\"form-control disabled\" readonly=\"readonly\" value=\"#{h(fopt[:value] || f.object.send(method))}\">")
  form_group lbl, fld, gopt
end

#text_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a text field.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



238
239
240
241
242
243
# File 'app/helpers/barkest_core/form_helper.rb', line 238

def text_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  fld = gopt[:wrap].call(f.text_field(method, fopt))
  form_group lbl, fld, gopt
end

#textarea_form_group(f, method, options = {}) ⇒ Object

Creates a form group including a label and a text area.

To specify a label, set the label_text option, otherwise the method name will be used. Prefix field options with field_ and label options with label_. Any additional options are passed to the form group.



251
252
253
254
255
256
# File 'app/helpers/barkest_core/form_helper.rb', line 251

def textarea_form_group(f, method, options = {})
  gopt, lopt, fopt = split_form_group_options(options)
  lbl = f.label_with_small method, lopt.delete(:text), lopt
  fld = gopt[:wrap].call(f.text_area(method, fopt))
  form_group lbl, fld, gopt
end