1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add separate test to ensure that delegate with :private option returns correct value

Remove extra comments `# Asking for private method` in activesupport/test/core_ext/module_test.rb
Improve docs of using `delegate` with `:private`
Update changelog of #31944
This commit is contained in:
bogdanvlviv 2018-02-28 00:05:46 +02:00
parent 3a7a21edb2
commit d0c697e9b1
No known key found for this signature in database
GPG key ID: E4ACD76A6DB6DFDD
3 changed files with 23 additions and 16 deletions

View file

@ -1,8 +1,7 @@
## Rails 6.0.0.alpha (Unreleased) ##
* Add `private: true` option to ActiveSupport's `delegate`.
In order to delegate methods as private methods:
* Add `:private` option to ActiveSupport's `Module#delegate`
in order to delegate methods as private:
class User < ActiveRecord::Base
has_one :profile
@ -17,8 +16,6 @@
# User.new.date_of_birth
# => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
More information in #31944.
*Tomas Valent*
* `String#truncate_bytes` to truncate a string to a maximum bytesize without

View file

@ -114,7 +114,7 @@ class Module
# invoice.customer_name # => 'John Doe'
# 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.
#
# class User < ActiveRecord::Base
@ -127,10 +127,10 @@ class Module
# end
# end
#
# User.new.age # 2
# User.new.first_name # Tomas
# 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.age # => 2
# User.new.first_name # => "Tomas"
# 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>
#
# 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+,

View file

@ -482,19 +482,29 @@ class ModuleTest < ActiveSupport::TestCase
def initialize(place)
@place = place
end
delegate(:street, :city, to: :@place, prefix: :the, private: true)
end
assert_equal %i(the_street the_city),
location.delegate(:street, :city, to: :@place, prefix: :the, private: true)
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 place.respond_to?(:the_street, true)
assert_not_respond_to place, :the_city
assert place.respond_to?(:the_city, true)
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