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.
|
* Report `:locals` as part of the data returned by ActionView render instrumentation.
|
||||||
|
|
||||||
Before:
|
Before:
|
||||||
|
|
|
@ -31,6 +31,8 @@ module ActionView
|
||||||
# automatically generated, following naming conventions encapsulated by the
|
# automatically generated, following naming conventions encapsulated by the
|
||||||
# RecordIdentifier methods #dom_id and #dom_class:
|
# RecordIdentifier methods #dom_id and #dom_class:
|
||||||
#
|
#
|
||||||
|
# dom_id(Post) # => "new_post"
|
||||||
|
# dom_class(Post) # => "post"
|
||||||
# dom_id(Post.new) # => "new_post"
|
# dom_id(Post.new) # => "new_post"
|
||||||
# dom_class(Post.new) # => "post"
|
# dom_class(Post.new) # => "post"
|
||||||
# dom_id(Post.find 42) # => "post_42"
|
# dom_id(Post.find 42) # => "post_42"
|
||||||
|
@ -80,17 +82,18 @@ module ActionView
|
||||||
# If no id is found, prefix with "new_" instead.
|
# If no id is found, prefix with "new_" instead.
|
||||||
#
|
#
|
||||||
# dom_id(Post.find(45)) # => "post_45"
|
# dom_id(Post.find(45)) # => "post_45"
|
||||||
# dom_id(Post.new) # => "new_post"
|
# 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:
|
# 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.find(45), :edit) # => "edit_post_45"
|
||||||
# dom_id(Post.new, :custom) # => "custom_post"
|
# dom_id(Post, :custom) # => "custom_post"
|
||||||
def dom_id(record, prefix = nil)
|
def dom_id(record_or_class, prefix = nil)
|
||||||
if record_id = record_key_for_dom_id(record)
|
record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
|
||||||
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
|
if record_id
|
||||||
|
"#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
|
||||||
else
|
else
|
||||||
dom_class(record, prefix || NEW)
|
dom_class(record_or_class, prefix || NEW)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -193,10 +193,13 @@ Car = Struct.new(:color)
|
||||||
|
|
||||||
class Plane
|
class Plane
|
||||||
attr_reader :to_key
|
attr_reader :to_key
|
||||||
|
delegate :model_name, to: :class
|
||||||
|
|
||||||
|
class << self
|
||||||
def model_name
|
def model_name
|
||||||
OpenStruct.new param_key: "airplane"
|
OpenStruct.new param_key: "airplane"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
@to_key = [1]
|
@to_key = [1]
|
||||||
|
|
|
@ -13,6 +13,10 @@ class RecordIdentifierTest < ActiveSupport::TestCase
|
||||||
@plural = "comments"
|
@plural = "comments"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_dom_id_with_class
|
||||||
|
assert_equal "new_#{@singular}", dom_id(@klass)
|
||||||
|
end
|
||||||
|
|
||||||
def test_dom_id_with_new_record
|
def test_dom_id_with_new_record
|
||||||
assert_equal "new_#{@singular}", dom_id(@record)
|
assert_equal "new_#{@singular}", dom_id(@record)
|
||||||
end
|
end
|
||||||
|
@ -53,7 +57,12 @@ class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
|
||||||
include ActionView::RecordIdentifier
|
include ActionView::RecordIdentifier
|
||||||
|
|
||||||
def setup
|
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
|
end
|
||||||
|
|
||||||
def test_dom_id_with_new_record
|
def test_dom_id_with_new_record
|
||||||
|
|
Loading…
Reference in a new issue