diff options
author | Anton Khamets <colorfulfool@gmail.com> | 2017-08-13 01:23:17 +0300 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2017-08-12 18:23:17 -0400 |
commit | e6c310b3f8144e9e08bc20a16d9889974d890030 (patch) | |
tree | b05891804e5a20234cbe42d4ea9ab042b3015012 | |
parent | 98360a96cc1e0bb6ab9eb31f421a36439e66eefc (diff) | |
download | rails-e6c310b3f8144e9e08bc20a16d9889974d890030.tar.gz rails-e6c310b3f8144e9e08bc20a16d9889974d890030.tar.bz2 rails-e6c310b3f8144e9e08bc20a16d9889974d890030.zip |
Test for the new exception of delegate_missing_to (#30191)
* Add test for the new exception of delegate_missing_to
* Add a changelog entry
* Only check for nil if NoMethodError was raised
* Make method private
* Have to pass both target name and value
* Inline the re-raise
[Rafael Mendonça França + Anton Khamets]
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 8 |
3 files changed, 22 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index d5087f67af..ac1043df78 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* `Module#delegate_missing_to` now raises `DelegationError` if target is nil, + similar to `Module#delegate`. + + *Anton Khamets* + * Update `String#camelize` to provide feedback when wrong option is passed `String#camelize` was returning nil without any feedback when an diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index c979af9de3..1840dc942f 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -273,10 +273,16 @@ class Module def method_missing(method, *args, &block) if #{target}.respond_to?(method) #{target}.public_send(method, *args, &block) - elsif #{target}.nil? - raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil" else - super + begin + super + rescue NoMethodError + if #{target}.nil? + raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil" + else + raise + end + end end end RUBY diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 69c8a312d8..e918823074 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -374,6 +374,14 @@ class ModuleTest < ActiveSupport::TestCase assert_match(/undefined method `my_fake_method' for/, e.message) end + def test_delegate_missing_to_raises_delegation_error_if_target_nil + e = assert_raises(Module::DelegationError) do + DecoratedTester.new(nil).name + end + + assert_equal "name delegated to client, but client is nil", e.message + end + def test_delegate_missing_to_affects_respond_to assert DecoratedTester.new(@david).respond_to?(:name) assert_not DecoratedTester.new(@david).respond_to?(:private_name) |