Class: Forme::Labeler::Explicit

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

Overview

Explicit labeler that creates a separate label tag that references the given tag’s id using a for attribute. Requires that all tags with labels have id fields.

Registered as :explicit.

Instance Method Summary collapse

Instance Method Details

#call(tag, input) ⇒ Object

Return an array with a label tag as the first entry and tag as a second entry. If the input has a :label_for option, use that, otherwise use the input’s :id option. If neither the :id or :label_for option is used, the label created will not be associated with an input.



1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
# File 'lib/forme.rb', line 1109

def call(tag, input)
  unless id = input.opts[:id]
    if key = input.opts[:key]
      namespaces = input.form_opts[:namespace]
      id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
      if key_id = input.opts[:key_id]
        id << "_#{key_id.to_s}"
      end
    end
  end
  if [:radio, :checkbox].include?(input.type)
    t = [tag, input.tag(:label, {:for=>input.opts.fetch(:label_for, id)}.merge(input.opts[:label_attr]||{}), [input.opts[:label]])]
    pos = :before
  else
    t = [input.tag(:label, {:for=>input.opts.fetch(:label_for, id)}.merge(input.opts[:label_attr]||{}), [input.opts[:label]]), tag]
    pos = :after
  end

  if input.opts[:label_position] == pos
    t.reverse
  else
    t
  end
end