Class: Forme::Labeler::Bootstrap3

Inherits:
Object
  • Object
show all
Defined in:
lib/forme/bs3.rb

Overview

Labeler that creates BS3 label tags referencing the the given tag’s id using a for attribute. Requires that all tags with labels have id fields.

Registered as :bs3.

Instance Method Summary collapse

Instance Method Details

#call(tag, input) ⇒ Object

Return an array with a label tag as the first entry and tag as a second entry. If the input has a :label_for option, use that, otherwise use the input’s :id option. If neither the :id or :label_for option is used, the label created will not be associated with an input.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/forme/bs3.rb', line 274

def call(tag, input)
  unless id = input.opts[:id]
    if key = input.opts[:key]
      namespaces = input.form_opts[:namespace]
      id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
      if key_id = input.opts[:key_id]
        id += "_#{key_id.to_s}"
      end
    end
  end

  label_attr = input.opts[:label_attr]
  label_attr = label_attr ? label_attr.dup : {}
  
  label_attr[:for] = label_attr[:for] == false ? nil : input.opts.fetch(:label_for, id)
  label = input.opts[:label]
  lpos = input.opts[:label_position] || ([:radio, :checkbox].include?(input.type) ? :after : :before)
  
  case input.type
  when :checkbox, :radio
    label = if lpos == :before
      [label, ' ', tag]
    else
      [tag, ' ', label]
    end
    input.tag(:label, label_attr, label)
  when :submit
    [tag]
  else
    label = input.tag(:label, label_attr, [input.opts[:label]])
    if lpos == :after
      [tag, ' ', label]
    else
      [label, ' ', tag]
    end
  end
end