class Forme::Labeler

  1. lib/forme/bs3.rb
  2. lib/forme/bs5.rb
  3. lib/forme/transformers/labeler.rb
  4. 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
14 def call(tag, input)
15   label = input.opts[:label]
16   label_position = input.opts[:label_position]
17   if [:radio, :checkbox].include?(input.type)
18     if input.type == :checkbox && tag.is_a?(Array) && tag.length == 2 && tag.first.attr[:type].to_s == 'hidden' 
19       t = if label_position == :before
20         [label, ' ', tag.last]
21       else
22         [tag.last, ' ', label]
23       end
24       return [tag.first , input.tag(:label, input.opts[:label_attr]||{}, t)]
25     elsif label_position == :before
26       t = [label, ' ', tag]
27     else
28       t = [tag, ' ', label]
29     end
30   elsif label_position == :after
31     t = [tag, ' ', label]
32   else
33     t = [label, ": ", tag]
34   end
35   input.tag(:label, input.opts[:label_attr]||{}, t)
36 end