class Forme::Labeler::Bootstrap3

  1. lib/forme/bs3.rb
Superclass: Object

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.

Methods

Public Instance

  1. call

Public Instance methods

call(tag, input)

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.

[show source]
    # File lib/forme/bs3.rb
274 def call(tag, input)
275   unless id = input.opts[:id]
276     if key = input.opts[:key]
277       namespaces = input.form_opts[:namespace]
278       id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
279       if key_id = input.opts[:key_id]
280         id += "_#{key_id.to_s}"
281       end
282     end
283   end
284 
285   label_attr = input.opts[:label_attr]
286   label_attr = label_attr ? label_attr.dup : {}
287   
288   label_attr[:for] = label_attr[:for] == false ? nil : input.opts.fetch(:label_for, id)
289   label = input.opts[:label]
290   lpos = input.opts[:label_position] || ([:radio, :checkbox].include?(input.type) ? :after : :before)
291   
292   case input.type
293   when :checkbox, :radio
294     label = if lpos == :before
295       [label, ' ', tag]
296     else
297       [tag, ' ', label]
298     end
299     input.tag(:label, label_attr, label)
300   when :submit
301     [tag]
302   else
303     label = input.tag(:label, label_attr, [input.opts[:label]])
304     if lpos == :after
305       [tag, ' ', label]
306     else
307       [label, ' ', tag]
308     end
309   end
310 end