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.



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/forme.rb', line 1071

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