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
257 def call(tag, input)
258   unless id = input.opts[:id]
259     if key = input.opts[:key]
260       namespaces = input.form_opts[:namespace]
261       id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
262       if key_id = input.opts[:key_id]
263         id += "_#{key_id.to_s}"
264       end
265     end
266   end
267 
268   label_attr = input.opts[:label_attr]
269   label_attr = label_attr ? label_attr.dup : {}
270   
271   label_attr[:for] = label_attr[:for] === false ? nil : input.opts.fetch(:label_for, id)
272   label = input.opts[:label]
273   lpos = input.opts[:label_position] || ([:radio, :checkbox].include?(input.type) ? :after : :before)
274   
275   case input.type
276   when :checkbox, :radio
277     label = if lpos == :before
278       [label, ' ', tag]
279     else
280       [tag, ' ', label]
281     end
282     input.tag(:label, label_attr, label)
283   when :submit
284     [tag]
285   else
286     label = input.tag(:label, label_attr, [input.opts[:label]])
287     if lpos == :after
288       [tag, ' ', label]
289     else
290       [label, ' ', tag]
291     end
292   end
293 end