mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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)`.
This commit is contained in:
parent
fd7c773387
commit
f4f15c86a2
4 changed files with 43 additions and 10 deletions
|
@ -1,3 +1,21 @@
|
|||
* 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"
|
||||
```
|
||||
|
||||
*Goulven Champenois*
|
||||
|
||||
* Report `:locals` as part of the data returned by ActionView render instrumentation.
|
||||
|
||||
Before:
|
||||
|
|
|
@ -31,6 +31,8 @@ module ActionView
|
|||
# automatically generated, following naming conventions encapsulated by the
|
||||
# RecordIdentifier methods #dom_id and #dom_class:
|
||||
#
|
||||
# dom_id(Post) # => "new_post"
|
||||
# dom_class(Post) # => "post"
|
||||
# dom_id(Post.new) # => "new_post"
|
||||
# dom_class(Post.new) # => "post"
|
||||
# dom_id(Post.find 42) # => "post_42"
|
||||
|
@ -79,18 +81,19 @@ module ActionView
|
|||
# The DOM id convention is to use the singular form of an object or class with the id following an underscore.
|
||||
# If no id is found, prefix with "new_" instead.
|
||||
#
|
||||
# dom_id(Post.find(45)) # => "post_45"
|
||||
# dom_id(Post.new) # => "new_post"
|
||||
# dom_id(Post.find(45)) # => "post_45"
|
||||
# dom_id(Post) # => "new_post"
|
||||
#
|
||||
# If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
|
||||
#
|
||||
# dom_id(Post.find(45), :edit) # => "edit_post_45"
|
||||
# dom_id(Post.new, :custom) # => "custom_post"
|
||||
def dom_id(record, prefix = nil)
|
||||
if record_id = record_key_for_dom_id(record)
|
||||
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
|
||||
# dom_id(Post, :custom) # => "custom_post"
|
||||
def dom_id(record_or_class, prefix = nil)
|
||||
record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
|
||||
if record_id
|
||||
"#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
|
||||
else
|
||||
dom_class(record, prefix || NEW)
|
||||
dom_class(record_or_class, prefix || NEW)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -193,9 +193,12 @@ Car = Struct.new(:color)
|
|||
|
||||
class Plane
|
||||
attr_reader :to_key
|
||||
delegate :model_name, to: :class
|
||||
|
||||
def model_name
|
||||
OpenStruct.new param_key: "airplane"
|
||||
class << self
|
||||
def model_name
|
||||
OpenStruct.new param_key: "airplane"
|
||||
end
|
||||
end
|
||||
|
||||
def save
|
||||
|
|
|
@ -13,6 +13,10 @@ class RecordIdentifierTest < ActiveSupport::TestCase
|
|||
@plural = "comments"
|
||||
end
|
||||
|
||||
def test_dom_id_with_class
|
||||
assert_equal "new_#{@singular}", dom_id(@klass)
|
||||
end
|
||||
|
||||
def test_dom_id_with_new_record
|
||||
assert_equal "new_#{@singular}", dom_id(@record)
|
||||
end
|
||||
|
@ -53,7 +57,12 @@ class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
|
|||
include ActionView::RecordIdentifier
|
||||
|
||||
def setup
|
||||
@record = Plane.new
|
||||
@klass = Plane
|
||||
@record = @klass.new
|
||||
end
|
||||
|
||||
def test_dom_id_with_new_class
|
||||
assert_equal "new_airplane", dom_id(@klass)
|
||||
end
|
||||
|
||||
def test_dom_id_with_new_record
|
||||
|
|
Loading…
Reference in a new issue