Module: CMS::FormBuilder::Fields

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

Instance Method Summary collapse

Instance Method Details

#_apply_default_options(args, defaults) ⇒ Object

simple helper method for extracting and applying default options.



226
227
228
229
# File 'lib/cms/form_builder.rb', line 226

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.



232
233
234
# File 'lib/cms/form_builder.rb', line 232

def _apply_field_defaults args
  _apply_default_options args, 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



237
238
239
240
241
242
243
244
# File 'lib/cms/form_builder.rb', line 237

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cms/form_builder.rb', line 246

def _field_types(type)
  case type
  when :string, :location
    :text_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
  end
end

#actions(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/cms/form_builder.rb', line 129

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cms/form_builder.rb', line 40

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



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

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

#email(*args) ⇒ Object



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

def email *args
  field :email, *args
end

#error_messagesObject



267
268
269
270
271
# File 'lib/cms/form_builder.rb', line 267

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



171
172
173
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
# File 'lib/cms/form_builder.rb', line 171

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

  input_options = {}

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

  unless options[:placeholder].nil?
    input_options[:placeholder] = options.delete(:placeholder)
  end

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

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

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


  label_html = label(name, options[:label], class: 'control-label')

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

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

  out.concat(@template.(:div, class: 'controls') do
    controls = send(_field_types(type), name, input_options)
    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_wrapper(type, name, options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/cms/form_builder.rb', line 157

def field_wrapper type, name, options = {}
  classes = "field #{type} #{name.to_s.dasherize} control-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

#hidden(*args) ⇒ Object



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

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

#location(name, options) ⇒ Object



36
37
38
# File 'lib/cms/form_builder.rb', line 36

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


138
139
140
141
142
143
144
145
# File 'lib/cms/form_builder.rb', line 138

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



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

def password *args
  field :password, *args
end

#radio(name, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cms/form_builder.rb', line 13

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) if options[:label]

    value_div = @template.(:span, class: 'values') do
      values.map do |value|
        @template. :span, class: 'value' do
          if value.is_a? Array
            radio_button(name, value[1]) + label(name, value[0], 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



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

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.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cms/form_builder.rb', line 104

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



147
148
149
150
151
152
153
154
155
# File 'lib/cms/form_builder.rb', line 147

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



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

def string *args
  field :string, *args
end

#text(*args) ⇒ Object



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

def text *args
  field :text, *args
end

#text_with_counter(name, *args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/cms/form_builder.rb', line 63

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



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cms/form_builder.rb', line 116

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