class Forme::Labeler

  1. lib/forme/bs3.rb
  2. lib/forme/transformers/labeler.rb
  3. show all
Superclass: Object

Default labeler used by the library, using implicit labels (where the label tag encloses the other tag).

Registered as :default.

Methods

Public Instance

  1. call

Public Instance methods

call (tag, input)

Return a label tag wrapping the given tag. For radio and checkbox inputs, the label occurs directly after the tag, for all other types, the label occurs before the tag.

[show source]
# File lib/forme/transformers/labeler.rb, line 14
def call(tag, input)
  label = input.opts[:label]
  label_position = input.opts[:label_position]
  if [:radio, :checkbox].include?(input.type)
    if input.type == :checkbox && tag.is_a?(Array) && tag.length == 2 && tag.first.attr[:type].to_s == 'hidden' 
      t = if label_position == :before
        [label, ' ', tag.last]
      else
        [tag.last, ' ', label]
      end
      return [tag.first , input.tag(:label, input.opts[:label_attr]||{}, t)]
    elsif label_position == :before
      t = [label, ' ', tag]
    else
      t = [tag, ' ', label]
    end
  elsif label_position == :after
    t = [tag, ' ', label]
  else
    t = [label, ": ", tag]
  end
  input.tag(:label, input.opts[:label_attr]||{}, t)
end