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   id = id_for_input(input)
54   label_attr = input.opts[:label_attr]
55   label_attr = label_attr ? label_attr.dup : {}
56   label_attr[:for] ||= input.opts.fetch(:label_for, id)
57   lpos = input.opts[:label_position] || ([:radio, :checkbox].include?(input.type) ? :after : :before)
58 
59   Forme.attr_classes(label_attr, "label-#{lpos}")
60   label = input.tag(:label, label_attr, [input.opts[:label]])
61 
62   t = if lpos == :before
63     [label, tag]
64   else
65     [tag, label]
66   end
67 
68   t
69 end