Class: Forme::Labeler

Inherits:
Object
  • Object
show all
Defined in:
lib/forme/transformers/labeler.rb

Overview

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

Registered as :default.

Defined Under Namespace

Classes: Bootstrap3, Explicit, Legend, Span

Instance Method Summary collapse

Instance Method Details

#call(tag, input) ⇒ Object

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.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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