Skip to content

Commit 22033a6

Browse files
Batykov Stepandblock
authored andcommitted
Fix #215: allow delegate_attribute for derived entity.
1 parent 38e36d0 commit 22033a6

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
============
33

44
* [#215](https://github.com/ruby-grape/grape-entity/pull/217): Fix: `#delegate_attribute` no longer delegates to methods included with `Kernel` - [@maltoe](https://github.com/maltoe).
5-
* [#219](https://github.com/ruby-grape/grape-entity/pull/219): Fix: double pass options in serializable_hash - [@sbatykov](https://github.com/sbatykov).
6-
* [#226](https://github.com/ruby-grape/grape-entity/pull/226): Added fetch method to fetch from opts_hash - [@alanjcfs](https://github.com/alanjcfs).
5+
* [#219](https://github.com/ruby-grape/grape-entity/pull/219): Fix: double pass options in `serializable_hash` - [@sbatykov](https://github.com/sbatykov).
6+
* [#226](https://github.com/ruby-grape/grape-entity/pull/226): Added `fetch` from `opts_hash` - [@alanjcfs](https://github.com/alanjcfs).
77
* [#232](https://github.com/ruby-grape/grape-entity/pull/232), [#213](https://github.com/ruby-grape/grape-entity/issues/213): Added `#kind_of?` and `#is_a?` to `OutputBuilder` to get an exact class of an `output` object - [@avyy](https://github.com/avyy).
88
* [#234](https://github.com/ruby-grape/grape-entity/pull/234), [#233](https://github.com/ruby-grape/grape-entity/issues/233): Added ruby version checking in `Gemfile` to install needed gems versions for supporting old rubies too - [@avyy](https://github.com/avyy).
99
* [#237](https://github.com/ruby-grape/grape-entity/pull/237): Added Danger, PR linter - [@dblock](https://github.com/dblock).
10+
* [#226](https://github.com/ruby-grape/grape-entity/pull/226): Add fetch method to fetch from opts_hash - [@alanjcfs](https://github.com/alanjcfs).
11+
* [#231](https://github.com/ruby-grape/grape-entity/pull/231), [#215](https://github.com/ruby-grape/grape-entity/issues/215): Fix: allow `delegate_attribute` for derived entity - [@sbatykov](https://github.com/sbatykov).
1012
* Your contribution here.
1113

1214
0.5.1 (2016-4-4)

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) && method(attribute).owner == self.class
481+
if respond_to?(attribute, true) && Grape::Entity > method(attribute).owner
482482
send(attribute)
483483
else
484484
delegator.delegate(attribute)

spec/grape_entity/entity_spec.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,12 +1524,13 @@ class FriendEntity < Grape::Entity
15241524
expect(subject.value_for(:fantasies)).to eq ['Nessy', 'Double Rainbows', 'Unicorns']
15251525
end
15261526

1527-
it 'tries instance methods on the entity first' do
1527+
context 'delegate_attribute' do
15281528
module EntitySpec
15291529
class DelegatingEntity < Grape::Entity
15301530
root 'friends', 'friend'
15311531
expose :name
15321532
expose :email
1533+
expose :system
15331534

15341535
private
15351536

@@ -1539,26 +1540,33 @@ def name
15391540
end
15401541
end
15411542

1542-
friend = double('Friend', name: 'joe', email: '[email protected]')
1543-
rep = EntitySpec::DelegatingEntity.new(friend)
1544-
expect(rep.value_for(:name)).to eq 'cooler name'
1545-
expect(rep.value_for(:email)).to eq '[email protected]'
1543+
it 'tries instance methods on the entity first' do
1544+
friend = double('Friend', name: 'joe', email: '[email protected]')
1545+
rep = EntitySpec::DelegatingEntity.new(friend)
1546+
expect(rep.value_for(:name)).to eq 'cooler name'
1547+
expect(rep.value_for(:email)).to eq '[email protected]'
15461548

1547-
another_friend = double('Friend', email: '[email protected]')
1548-
rep = EntitySpec::DelegatingEntity.new(another_friend)
1549-
expect(rep.value_for(:name)).to eq 'cooler name'
1550-
end
1549+
another_friend = double('Friend', email: '[email protected]')
1550+
rep = EntitySpec::DelegatingEntity.new(another_friend)
1551+
expect(rep.value_for(:name)).to eq 'cooler name'
1552+
end
1553+
1554+
it 'does not delegate Kernel methods' do
1555+
foo = double 'Foo', system: 'System'
1556+
rep = EntitySpec::DelegatingEntity.new foo
1557+
expect(rep.value_for(:system)).to eq 'System'
1558+
end
15511559

1552-
it 'does not delegate to Kernel methods' do
15531560
module EntitySpec
1554-
class DelegatingEntity < Grape::Entity
1555-
expose :system
1561+
class DerivedEntity < DelegatingEntity
15561562
end
15571563
end
15581564

1559-
foo = double 'Foo', system: 'System'
1560-
rep = EntitySpec::DelegatingEntity.new foo
1561-
expect(rep.value_for(:system)).to eq 'System'
1565+
it 'derived entity get methods from base entity' do
1566+
foo = double 'Foo', name: 'joe'
1567+
rep = EntitySpec::DerivedEntity.new foo
1568+
expect(rep.value_for(:name)).to eq 'cooler name'
1569+
end
15621570
end
15631571

15641572
context 'using' do

0 commit comments

Comments
 (0)