Skip to content

Commit 07bdc59

Browse files
author
Malte Rohde
committed
Do not delegate to Kernel methods.
This patch prevents Entity from delegating attributes to Kernel methods. This is accomplished by testing the methods the Entity class responds to for ownership by the Entity class itself. Fixes #215.
1 parent 50ccc90 commit 07bdc59

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/grape_entity/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def value_for(key, options = Options.new)
478478
end
479479

480480
def delegate_attribute(attribute)
481-
if respond_to?(attribute, true)
481+
if respond_to?(attribute, true) && method(attribute).owner == self.class
482482
send(attribute)
483483
else
484484
delegator.delegate(attribute)

spec/grape_entity/entity_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,18 @@ def name
15481548
expect(rep.value_for(:name)).to eq 'cooler name'
15491549
end
15501550

1551+
it 'does not delegate to Kernel methods' do
1552+
module EntitySpec
1553+
class DelegatingEntity < Grape::Entity
1554+
expose :system
1555+
end
1556+
end
1557+
1558+
foo = double 'Foo', system: 'System'
1559+
rep = EntitySpec::DelegatingEntity.new foo
1560+
expect(rep.value_for(:system)).to eq 'System'
1561+
end
1562+
15511563
context 'using' do
15521564
before do
15531565
module EntitySpec

0 commit comments

Comments
 (0)