Skip to content

Commit 954bbee

Browse files
committed
Add Labeler::Explicit#id_for_input private method to allow subclasses to easily get the id
1 parent a3f3415 commit 954bbee

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Add Labeler::Explicit#id_for_input private method to allow subclasses to easily get the id (jeremyevans)
4+
35
* Support Sequel::Model#forme_required_abbr_title to control the content of the title attribute for abbr tags (jeremyevans)
46

57
* Add Serializer#self_closing_tag? private method for easier overriding in subclasses (jeremyevans)

lib/forme/transformers/labeler.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@ class Labeler::Explicit
5050
# :label_for option is used, the label created will not be
5151
# associated with an input.
5252
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-
53+
id = id_for_input(input)
6354
label_attr = input.opts[:label_attr]
6455
label_attr = label_attr ? label_attr.dup : {}
6556
label_attr[:for] ||= input.opts.fetch(:label_for, id)
@@ -76,6 +67,23 @@ def call(tag, input)
7667

7768
t
7869
end
70+
71+
private
72+
73+
# Determine the appropriate id for the input, to use in the for attribute
74+
def id_for_input(input)
75+
unless id = input.opts[:id]
76+
if key = input.opts[:key]
77+
namespaces = input.form_opts[:namespace]
78+
id = "#{namespaces.join('_')}#{'_' unless namespaces.empty?}#{key}"
79+
if key_id = input.opts[:key_id]
80+
id += "_#{key_id.to_s}"
81+
end
82+
end
83+
end
84+
85+
id
86+
end
7987
end
8088

8189
class Labeler::Span

0 commit comments

Comments
 (0)