class Forme::Labeler::Explicit

  1. lib/forme/transformers/labeler.rb
Superclass: Object

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.

Methods

Public Instance

  1. call

Public Instance methods

call(tag, input)

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.

[show source]
   # File lib/forme/transformers/labeler.rb
52 def call(tag, input)
53   unless id = input.opts[:id]
54     if key = input.opts[:key]
55       namespaces = input.form_opts[:namespace]
56       id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
57       if key_id = input.opts[:key_id]
58         id += "_#{key_id.to_s}"
59       end
60     end
61   end
62 
63   label_attr = input.opts[:label_attr]
64   label_attr = label_attr ? label_attr.dup : {}
65   label_attr[:for] ||= input.opts.fetch(:label_for, id)
66   lpos = input.opts[:label_position] || ([:radio, :checkbox].include?(input.type) ? :after : :before)
67 
68   Forme.attr_classes(label_attr, "label-#{lpos}")
69   label = input.tag(:label, label_attr, [input.opts[:label]])
70 
71   t = if lpos == :before
72     [label, tag]
73   else
74     [tag, label]
75   end
76 
77   t
78 end