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