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.



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/forme.rb', line 1002

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