Skip to content

Fix for #215. Allow delegate_attribute for derived classes #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#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).
* [#219](https://github.com/ruby-grape/grape-entity/pull/219): Fix: double pass options in serializable_hash - [@sbatykov](https://github.com/sbatykov).
* [#226](https://github.com/ruby-grape/grape-entity/pull/226): Add fetch method to fetch from opts_hash - [@alanjcfs](https://github.com/alanjcfs).
* [#231](https://github.com/ruby-grape/grape-entity/pull/231): Fix for [#215](https://github.com/ruby-grape/grape-entity/pull/217) allow delegate_attribute for derived entity - [@sbatykov](https://github.com/sbatykov).
* Your contribution here.

0.5.1 (2016-4-4)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def value_for(key, options = Options.new)
end

def delegate_attribute(attribute)
if respond_to?(attribute, true) && method(attribute).owner == self.class
if respond_to?(attribute, true) && Grape::Entity > method(attribute).owner
send(attribute)
else
delegator.delegate(attribute)
Expand Down
38 changes: 23 additions & 15 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1523,12 +1523,13 @@ class FriendEntity < Grape::Entity
expect(subject.value_for(:fantasies)).to eq ['Nessy', 'Double Rainbows', 'Unicorns']
end

it 'tries instance methods on the entity first' do
context 'delegate_attribute' do
module EntitySpec
class DelegatingEntity < Grape::Entity
root 'friends', 'friend'
expose :name
expose :email
expose :system

private

Expand All @@ -1538,26 +1539,33 @@ def name
end
end

friend = double('Friend', name: 'joe', email: '[email protected]')
rep = EntitySpec::DelegatingEntity.new(friend)
expect(rep.value_for(:name)).to eq 'cooler name'
expect(rep.value_for(:email)).to eq '[email protected]'
it 'tries instance methods on the entity first' do
friend = double('Friend', name: 'joe', email: '[email protected]')
rep = EntitySpec::DelegatingEntity.new(friend)
expect(rep.value_for(:name)).to eq 'cooler name'
expect(rep.value_for(:email)).to eq '[email protected]'

another_friend = double('Friend', email: '[email protected]')
rep = EntitySpec::DelegatingEntity.new(another_friend)
expect(rep.value_for(:name)).to eq 'cooler name'
end
another_friend = double('Friend', email: '[email protected]')
rep = EntitySpec::DelegatingEntity.new(another_friend)
expect(rep.value_for(:name)).to eq 'cooler name'
end

it 'does not delegate Kernel methods' do
foo = double 'Foo', system: 'System'
rep = EntitySpec::DelegatingEntity.new foo
expect(rep.value_for(:system)).to eq 'System'
end

it 'does not delegate to Kernel methods' do
module EntitySpec
class DelegatingEntity < Grape::Entity
expose :system
class DerivedEntity < DelegatingEntity
end
end

foo = double 'Foo', system: 'System'
rep = EntitySpec::DelegatingEntity.new foo
expect(rep.value_for(:system)).to eq 'System'
it 'derived entity get methods from base entity' do
foo = double 'Foo', name: 'joe'
rep = EntitySpec::DerivedEntity.new foo
expect(rep.value_for(:name)).to eq 'cooler name'
end
end

context 'using' do
Expand Down