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, line 256
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