fix: Check only object's private method when preventing delegati… (#875)

https://github.com/drapergem/draper/pull/849 introduced a valid fix to prevent calling object's public method when a decorator overwrites it through a private one. There is one caveat, though. `private_methods` methods has `all` parameter set to `true` by default (https://ruby-doc.org/core-2.7.0/Object.html#method-i-private_methods)`. As a result all private methods are returned, instead of only the ones defined on the object.

This commit fixes the above issue by setting the `all` parameter to `false`.
This commit is contained in:
Igor Springer 2020-02-11 17:47:49 +01:00 committed by GitHub
parent 19c48c2008
commit 4439ed8e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -20,7 +20,7 @@ module Draper
# @private
def delegatable?(method)
return if private_methods.include?(method)
return if private_methods(false).include?(method)
object.respond_to?(method)
end

View File

@ -684,6 +684,18 @@ module Draper
expect{ decorator.hello_world }.to raise_error NoMethodError
end
end
context 'when delegated method has the same name as private method defined on another object' do
let(:decorator_class) { Class.new(Decorator) }
let(:object) { Class.new { def print; end }.new }
it 'delegates the public method defined on the object' do
decorator = decorator_class.new(object)
# `print` private method is defined on `Object`
expect{ decorator.print }.not_to raise_error NoMethodError
end
end
end
context ".method_missing" do