Class: Forme::Labeler

Inherits:
Object
  • Object
show all
Defined in:
lib/forme.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: Explicit

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.



1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'lib/forme.rb', line 1125

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