Skip to content

Commit 985e75f

Browse files
authored
Merge pull request rails#55204 from bensheldon/dom_target
Add `dom_target` helper to create `dom_id`-like strings from an unlimited number of objects
2 parents b9a7fa7 + c8e4856 commit 985e75f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add `dom_target` helper to create `dom_id`-like strings from an unlimited
2+
number of objects.
3+
4+
*Ben Sheldon*
5+
16
* Respect `html_options[:form]` when `collection_checkboxes` generates the
27
hidden `<input>`.
38

actionview/lib/action_view/record_identifier.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ def dom_id(record_or_class, prefix = nil)
101101
end
102102
end
103103

104+
# The DOM target convention is to concatenate any number of parameters into a string.
105+
# Records are passed through dom_id, while string and symbols are retained.
106+
#
107+
# dom_target(Post.find(45)) # => "post_45"
108+
# dom_target(Post.find(45), :edit) # => "post_45_edit"
109+
# dom_target(Post.find(45), :edit, :special) # => "post_45_edit_special"
110+
# dom_target(Post.find(45), Comment.find(1)) # => "post_45_comment_1"
111+
def dom_target(*objects)
112+
objects.map! do |object|
113+
case object
114+
when Symbol, String
115+
object
116+
when Class
117+
dom_class(object)
118+
else
119+
dom_id(object)
120+
end
121+
end
122+
objects.join(JOIN)
123+
end
124+
104125
private
105126
# Returns a string representation of the key attribute(s) that is suitable for use in an HTML DOM id.
106127
# This can be overwritten to customize the default generated string representation if desired.

actionview/test/template/record_identifier_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ def test_dom_id_as_singleton_method
5656
def test_dom_class_as_singleton_method
5757
assert_equal @singular, ActionView::RecordIdentifier.dom_class(@record)
5858
end
59+
60+
def test_dom_target_with_multiple_objects
61+
@record.save
62+
assert_equal "foo_bar_comment_comment_1_new_comment", dom_target(:foo, "bar", @klass, @record, @klass.new)
63+
end
64+
65+
def test_dom_target_as_singleton_method
66+
@record.save
67+
assert_equal "#{@singular}_#{@record.id}", ActionView::RecordIdentifier.dom_target(@record)
68+
end
5969
end
6070

6171
class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
@@ -110,4 +120,14 @@ def test_dom_id_as_singleton_method
110120
def test_dom_class_as_singleton_method
111121
assert_equal "airplane", ActionView::RecordIdentifier.dom_class(@record)
112122
end
123+
124+
def test_dom_target_with_multiple_objects
125+
@record.save
126+
assert_equal "foo_bar_airplane_airplane_1_new_airplane", dom_target(:foo, "bar", @klass, @record, @klass.new)
127+
end
128+
129+
def test_dom_target_as_singleton_method
130+
@record.save
131+
assert_equal "airplane_1", ActionView::RecordIdentifier.dom_target(@record)
132+
end
113133
end

0 commit comments

Comments
 (0)