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:
parent
19c48c2008
commit
4439ed8e84
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue