Module: CMS::FormBuilder::Fields

Included in:
CMS::FormBuilder
Defined in:
lib/cms/form_builder.rb

Constant Summary collapse

LABEL_WIDTH =
'col-lg-2'
FIELD_WIDTH =
'col-lg-4'

Instance Method Summary collapse

Instance Method Details

#_apply_default_options(args, defaults) ⇒ Object

simple helper method for extracting and applying default options.



238
239
240
241
# File 'lib/cms/form_builder.rb', line 238

def _apply_default_options args, defaults
  options = args.extract_options!
  args << options.reverse_merge!(defaults)
end

#_apply_field_defaults(args) ⇒ Object

apply the default options for all fields.



244
245
246
# File 'lib/cms/form_builder.rb', line 244

def _apply_field_defaults args
  _apply_default_options args, field_options.reverse_merge(label: true, wrap_field: true, label_first: true)
end

#_extract_field_args(args) ⇒ Object

single use method for parsing options provided by the field helper



253
254
255
256
257
258
259
260
# File 'lib/cms/form_builder.rb', line 253

def _extract_field_args args
  args = _apply_field_defaults(args)
  options = args.extract_options!
  name = args.pop
  type = args.pop
  options[:label] = name.to_s.humanize if options[:label].is_a? TrueClass
  [type, name, options]
end

#_field_types(type) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/cms/form_builder.rb', line 262

def _field_types(type)
  case type
  when :string, :location
    :text_field
  when :file
    :file_field
  when :search
    :search_field
  when :text
    :text_area
  when :email
    :email_field
  when :password
    :password_field
  when :hidden, :slider
    :hidden_field
  when :boolean, :radio
    :radio_button
  when :boolean, :check
    :check_box
  when :choices
    :select
  end
end

#actions(options = {}) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/cms/form_builder.rb', line 132

def actions options = {}
  options.reverse_merge! save: 'Save', saving: 'Saving...', class: 'form-actions', save_class: 'btn btn-primary'
  @template.(:div, class: options.delete(:class)) do
    actions = ''.html_safe
    actions << submit(options[:save], disable_with: options[:saving], class: options[:save_class])
    actions << status
  end
end

#autocomplete(name, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cms/form_builder.rb', line 35

def autocomplete name, *args
  args = _apply_field_defaults(args)
  options = args.extract_options!
  options.reverse_merge!(waiting: 'Searching...')

  option_text = '<div class="options"><div class="inner">'.html_safe
  option_text.concat @template.(:div, options[:waiting], class: 'waiting')
  option_text.concat '<div class="results"></div>'.html_safe
  option_text.concat '</div></div>'.html_safe

  field_wrapper(:autocomplete, name) do
    out = ''.html_safe
    if options[:label] == true
      out.concat label(name)
    elsif options[:label]
      out.concat label(name, options[:label])
    end
    out.concat @template.link_to 'learn more', '#', class: 'tipsy', title: send("#{name}_help_text") if options[:help_text]
    options[:help_text] = false
    out.concat string(name, options.merge(autocomplete: false, label: false, append: option_text))
  end
end

#boolean(*args) ⇒ Object



69
70
71
# File 'lib/cms/form_builder.rb', line 69

def boolean *args
  field :boolean, *_apply_default_options(args, label_first: false)
end

#choices(attribute, choices, *args) ⇒ Object



101
102
103
# File 'lib/cms/form_builder.rb', line 101

def choices attribute, choices, *args
  field :choices, *_apply_default_options(args << attribute,  choices: choices)
end

#email(*args) ⇒ Object



89
90
91
# File 'lib/cms/form_builder.rb', line 89

def email *args
  field :email, *args
end

#error_messagesObject



287
288
289
290
291
# File 'lib/cms/form_builder.rb', line 287

def error_messages
  if object.errors.any?
    @template.render partial: 'shared/form/error_messages', object: object.errors
  end
end

#field(*args, &block) ⇒ Object

def _input_options options

[:autocomplete, :placeholder]

end



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cms/form_builder.rb', line 174

def field *args, &block
  type, name, options = _extract_field_args(args)
  out = ''.html_safe

  input_options = {}
  input_args = []
  input_classes = options[:class].try(:split, ' ') || []

  unless options[:autocomplete].nil?
    options.delete(:autocomplete)
    input_options[:autocomplete] = 'off'
  end

  unless options[:placeholder].nil?
    input_options[:placeholder] = if (placeholder = options.delete(:placeholder)) == true then name.to_s.humanize else placeholder end
  end

  unless options[:hidden].nil?
    input_classes << 'hidden' if options[:hidden] == true
  end

  unless options[:required].nil?
    input_options[:required] = 'required' if options[:required] == true
  end

  unless options[:choices].nil?
    input_args << options[:choices]
  end

  out.concat options[:prepend] if options[:prepend]

  label_html = label(name, options[:label], class: "#{LABEL_WIDTH} control-label")

  out.concat label_html if options[:label] && options[:label_first]

  if options[:help_text]
    help_text = send("#{name}_help_text")
    help_html = %Q(<a class="tipsy" title="#{help_text}" href="#">learn more</a>).html_safe
    out.concat help_html
  end

  input_classes << 'form-control'
  input_options[:class] = input_classes.join(' ')

  out.concat(@template.(:div, class: "#{FIELD_WIDTH} #{type}") do
    merged_input_args = input_args << input_options
    controls = send(_field_types(type), name, *merged_input_args)
    controls.concat @template.(:div, options[:help_block], class: 'help-block') if options[:help_block].present?
    controls
  end)

  out.concat label_html if options[:label] && !options[:label_first]

  out.concat options[:append] if options[:append]
  out.concat yield if block_given?

  if options[:wrap_field]
    field_wrapper(type, name) { out }
  else
    out
  end
end

#field_optionsObject



248
249
250
# File 'lib/cms/form_builder.rb', line 248

def field_options
  options[:fields] || {}
end

#field_wrapper(type, name, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/cms/form_builder.rb', line 160

def field_wrapper type, name, options = {}
  classes = "field #{type} #{name.to_s.dasherize} form-group"
  classes << options[:classes] if options[:classes]
  classes << ' error' if object.errors.include? name
  options.merge! class: classes
  @template. :div, options do
    yield
  end
end

#file(*args) ⇒ Object



77
78
79
# File 'lib/cms/form_builder.rb', line 77

def file *args
  field :file, *args
end

#hidden(*args) ⇒ Object



97
98
99
# File 'lib/cms/form_builder.rb', line 97

def hidden *args
  field :hidden, *_apply_default_options(args, label: false, wrap_field: false)
end

#location(name, options) ⇒ Object



31
32
33
# File 'lib/cms/form_builder.rb', line 31

def location name, options
  autocomplete name, options.merge(prepend: @template.(:div, '', class: 'geolocate'))
end


141
142
143
144
145
146
147
148
# File 'lib/cms/form_builder.rb', line 141

def modal_actions options = {}
  options.reverse_merge! save: 'Save', saving: 'Saving...', class: 'modal-footer'
  @template.(:div, class: options.delete(:class)) do
    actions = ''.html_safe
    actions << @template.link_to('Close', '#close', class: 'btn', data: {dismiss: 'modal'})
    actions << submit(options[:save], disable_with: options[:saving], class: 'btn btn-primary')
  end
end

#password(*args) ⇒ Object



93
94
95
# File 'lib/cms/form_builder.rb', line 93

def password *args
  field :password, *args
end

#radio(name, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cms/form_builder.rb', line 7

def radio name, *args
  args = _apply_field_defaults(args)
  options = args.extract_options!
  values = args
  field_wrapper :radio, name do
    out = ''.html_safe
    out.concat label(name, class: "#{LABEL_WIDTH} control-label") if options[:label]

    value_div = @template.(:div, class: "#{FIELD_WIDTH} radio") do
      values.map do |value|
        @template. :span, class: 'value' do
          if value.is_a? Array
            radio_button(name, value[1].html_safe) + label(name, value[0].html_safe, value: value[1])
          else
            radio_button(name, value) + label(name, value, value: value)
          end
        end
      end.join("\n").html_safe
    end

    out.concat value_div
  end
end

#search(*args) ⇒ Object



81
82
83
# File 'lib/cms/form_builder.rb', line 81

def search *args
  field :search, *args
end

#slider(name, *args) ⇒ Object

slider is weird enough that we will not use the default field helper. instead, we will construct our own field that looks like a regular field.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cms/form_builder.rb', line 107

def slider name, *args
  args = _apply_field_defaults(args)
  options = args.extract_options!
  out = ''.html_safe

  field_wrapper :slider, name do
    out.concat label(name, options[:label]) if options[:label]
    out.concat @template. :div, '', class: 'slider-edit'
    out.concat hidden(name)
  end
end

#status(options = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/cms/form_builder.rb', line 150

def status options = {}
  options.reverse_merge! success: 'Saved!', error: 'Failed!'
  out = @template.(:div, class: 'status') do
    status = ''.html_safe
    status << @template.(:div, '', class: 'spinner')
    # status << @template.content_tag(:div, options[:success], class: 'success')
    # status << @template.content_tag(:div, options[:error], class: 'error')
  end
end

#string(*args) ⇒ Object



73
74
75
# File 'lib/cms/form_builder.rb', line 73

def string *args
  field :string, *args
end

#text(*args) ⇒ Object



85
86
87
# File 'lib/cms/form_builder.rb', line 85

def text *args
  field :text, *args
end

#text_with_counter(name, *args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cms/form_builder.rb', line 58

def text_with_counter name, *args
  args = _apply_field_defaults(args)
  options = args.extract_options!

  unless options[:length].blank?
    counter = "<div class=\"counter\"><span class=\"count\">0</span>/<span class=\"maximum\">#{options[:length]}</span></div>".html_safe
  end

  text(name, options.merge(label: options[:label], append: counter))
end

#toggle(name, *args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cms/form_builder.rb', line 119

def toggle name, *args
  args = _apply_field_defaults(args)
  options = args.extract_options!
  out = ''.html_safe

  field_wrapper :toggle, name, :'data-default' => options[:default] do
    out.concat label(name) if options[:label]
    out.concat(@template.(:div, class: (object.send(name) ? 'visible' : 'hidden')) do
      check_box(name) << @template.label_tag(object.send(name) ? 'visible' : 'hidden')
    end)
  end
end