Skip to content

Commit f4f15c8

Browse files
committed
Allow passing a class to dom_id
You no longer need to call `new` when passing a class to `dom_id`. This makes `dom_id` behave like `dom_class` in this regard. Apart from saving a few keystrokes, it prevents Ruby from needing to instantiate a whole new object just to generate a string. Before: ```ruby dom_id(Post) # NoMethodError: undefined method `to_key' for Post:Class ``` After: ```ruby dom_id(Post) # "new_post" ``` You can still call `dom_id(Post.new)`.
1 parent fd7c773 commit f4f15c8

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

actionview/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
* Allow passing a class to `dom_id`.
2+
You no longer need to call `new` when passing a class to `dom_id`.
3+
This makes `dom_id` behave like `dom_class` in this regard.
4+
Apart from saving a few keystrokes, it prevents Ruby from needing
5+
to instantiate a whole new object just to generate a string.
6+
7+
Before:
8+
```ruby
9+
dom_id(Post) # => NoMethodError: undefined method `to_key' for Post:Class
10+
```
11+
12+
After:
13+
```ruby
14+
dom_id(Post) # => "new_post"
15+
```
16+
17+
*Goulven Champenois*
18+
119
* Report `:locals` as part of the data returned by ActionView render instrumentation.
220

321
Before:

actionview/lib/action_view/record_identifier.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module ActionView
3131
# automatically generated, following naming conventions encapsulated by the
3232
# RecordIdentifier methods #dom_id and #dom_class:
3333
#
34+
# dom_id(Post) # => "new_post"
35+
# dom_class(Post) # => "post"
3436
# dom_id(Post.new) # => "new_post"
3537
# dom_class(Post.new) # => "post"
3638
# dom_id(Post.find 42) # => "post_42"
@@ -79,18 +81,19 @@ def dom_class(record_or_class, prefix = nil)
7981
# The DOM id convention is to use the singular form of an object or class with the id following an underscore.
8082
# If no id is found, prefix with "new_" instead.
8183
#
82-
# dom_id(Post.find(45)) # => "post_45"
83-
# dom_id(Post.new) # => "new_post"
84+
# dom_id(Post.find(45)) # => "post_45"
85+
# dom_id(Post) # => "new_post"
8486
#
8587
# If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
8688
#
8789
# dom_id(Post.find(45), :edit) # => "edit_post_45"
88-
# dom_id(Post.new, :custom) # => "custom_post"
89-
def dom_id(record, prefix = nil)
90-
if record_id = record_key_for_dom_id(record)
91-
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
90+
# dom_id(Post, :custom) # => "custom_post"
91+
def dom_id(record_or_class, prefix = nil)
92+
record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
93+
if record_id
94+
"#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
9295
else
93-
dom_class(record, prefix || NEW)
96+
dom_class(record_or_class, prefix || NEW)
9497
end
9598
end
9699

actionview/test/lib/controller/fake_models.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ def each(&block)
193193

194194
class Plane
195195
attr_reader :to_key
196+
delegate :model_name, to: :class
196197

197-
def model_name
198-
OpenStruct.new param_key: "airplane"
198+
class << self
199+
def model_name
200+
OpenStruct.new param_key: "airplane"
201+
end
199202
end
200203

201204
def save

actionview/test/template/record_identifier_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def setup
1313
@plural = "comments"
1414
end
1515

16+
def test_dom_id_with_class
17+
assert_equal "new_#{@singular}", dom_id(@klass)
18+
end
19+
1620
def test_dom_id_with_new_record
1721
assert_equal "new_#{@singular}", dom_id(@record)
1822
end
@@ -53,7 +57,12 @@ class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
5357
include ActionView::RecordIdentifier
5458

5559
def setup
56-
@record = Plane.new
60+
@klass = Plane
61+
@record = @klass.new
62+
end
63+
64+
def test_dom_id_with_new_class
65+
assert_equal "new_airplane", dom_id(@klass)
5766
end
5867

5968
def test_dom_id_with_new_record

0 commit comments

Comments
 (0)