未验证 提交 e6d6d73d 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #32133 from bogdanvlviv/delegate-with-private

Add separate test to ensure that `delegate` with `:private` option returns correct value
## Rails 6.0.0.alpha (Unreleased) ## ## Rails 6.0.0.alpha (Unreleased) ##
* Add `private: true` option to ActiveSupport's `delegate`. * Add `:private` option to ActiveSupport's `Module#delegate`
in order to delegate methods as private:
In order to delegate methods as private methods:
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_one :profile has_one :profile
...@@ -17,8 +16,6 @@ ...@@ -17,8 +16,6 @@
# User.new.date_of_birth # User.new.date_of_birth
# => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340> # => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
More information in #31944.
*Tomas Valent* *Tomas Valent*
* `String#truncate_bytes` to truncate a string to a maximum bytesize without * `String#truncate_bytes` to truncate a string to a maximum bytesize without
......
...@@ -114,7 +114,7 @@ class DelegationError < NoMethodError; end ...@@ -114,7 +114,7 @@ class DelegationError < NoMethodError; end
# invoice.customer_name # => 'John Doe' # invoice.customer_name # => 'John Doe'
# invoice.customer_address # => 'Vimmersvej 13' # invoice.customer_address # => 'Vimmersvej 13'
# #
# If you want the delegated method to be a private method, # If you want the delegate methods to be a private,
# use the <tt>:private</tt> option. # use the <tt>:private</tt> option.
# #
# class User < ActiveRecord::Base # class User < ActiveRecord::Base
...@@ -127,10 +127,10 @@ class DelegationError < NoMethodError; end ...@@ -127,10 +127,10 @@ class DelegationError < NoMethodError; end
# end # end
# end # end
# #
# User.new.age # 2 # User.new.age # => 2
# User.new.first_name # Tomas # User.new.first_name # => "Tomas"
# User.new.date_of_birth # NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340> # User.new.date_of_birth # => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
# User.new.religion # NoMethodError: private method `religion' called for #<User:0x00000008221340> # User.new.religion # => NoMethodError: private method `religion' called for #<User:0x00000008221340>
# #
# If the target is +nil+ and does not respond to the delegated method a # If the target is +nil+ and does not respond to the delegated method a
# +Module::DelegationError+ is raised. If you wish to instead return +nil+, # +Module::DelegationError+ is raised. If you wish to instead return +nil+,
......
...@@ -482,19 +482,29 @@ def test_private_delegate_prefixed_with_private_option ...@@ -482,19 +482,29 @@ def test_private_delegate_prefixed_with_private_option
def initialize(place) def initialize(place)
@place = place @place = place
end end
end
assert_equal %i(the_street the_city), delegate(:street, :city, to: :@place, prefix: :the, private: true)
location.delegate(:street, :city, to: :@place, prefix: :the, private: true) end
place = location.new(Somewhere.new("Such street", "Sad city")) place = location.new(Somewhere.new("Such street", "Sad city"))
assert_not_respond_to place, :street
assert_not_respond_to place, :city
assert_not_respond_to place, :the_street assert_not_respond_to place, :the_street
assert place.respond_to?(:the_street, true) assert place.respond_to?(:the_street, true)
assert_not_respond_to place, :the_city assert_not_respond_to place, :the_city
assert place.respond_to?(:the_city, true) assert place.respond_to?(:the_city, true)
end end
def test_delegate_with_private_option_returns_names_of_delegate_methods
location = Class.new do
def initialize(place)
@place = place
end
end
assert_equal [:street, :city],
location.delegate(:street, :city, to: :@place, private: true)
assert_equal [:the_street, :the_city],
location.delegate(:street, :city, to: :@place, prefix: :the, private: true)
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册