Skip to content

Fix: allow usage of attributes with name 'key' if Hash objects are used #111

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

Merged
merged 1 commit into from
Mar 10, 2015
Merged
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
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 300
Max: 301

# Offense count: 4
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#111](https://github.com/intridea/grape-entity/pull/111): Fix: allow usage of attributes with name 'key' if `Hash` objects are used - [@croeck](https://github.com/croeck).
* [#110](https://github.com/intridea/grape-entity/pull/110): Fixed safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
Expand Down
18 changes: 9 additions & 9 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,17 @@ def delegate_attribute(attribute)
name = self.class.name_for(attribute)
if respond_to?(name, true)
send(name)
elsif object.is_a?(Hash)
object[name]
elsif object.respond_to?(name, true)
object.send(name)
elsif object.respond_to?(:fetch, true)
object.fetch(name)
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here, I think this can be collapsed into an elsif.

if object.respond_to?(name, true)
begin
object.send(name)
elsif object.respond_to?(:fetch, true)
object.fetch(name)
else
begin
object.send(name)
rescue NoMethodError
raise NoMethodError, "#{self.class.name} missing attribute `#{name}' on #{object}"
end
rescue NoMethodError
raise NoMethodError, "#{self.class.name} missing attribute `#{name}' on #{object}"
end
end
end
Expand Down
26 changes: 24 additions & 2 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,12 @@ class Parent < Person
email: '[email protected]',
birthday: Time.gm(2012, 2, 27),
fantasies: ['Unicorns', 'Double Rainbows', 'Nessy'],
characteristics: [
{ key: 'hair_color', value: 'brown' }
],
friends: [
double(name: 'Friend 1', email: '[email protected]', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
double(name: 'Friend 2', email: '[email protected]', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
double(name: 'Friend 1', email: '[email protected]', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
double(name: 'Friend 2', email: '[email protected]', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
]
}
end
Expand Down Expand Up @@ -913,6 +916,25 @@ class FriendEntity < Grape::Entity
expect(rep.serializable_hash).to be_nil
end

it 'passes through exposed entity with key and value attributes' do
module EntitySpec
class CharacteristicsEntity < Grape::Entity
root 'characteristics', 'characteristic'
expose :key, :value
end
end

fresh_class.class_eval do
expose :characteristics, using: EntitySpec::CharacteristicsEntity
end

rep = subject.send(:value_for, :characteristics)
expect(rep).to be_kind_of Array
expect(rep.reject { |r| r.is_a?(EntitySpec::CharacteristicsEntity) }).to be_empty
expect(rep.first.serializable_hash[:key]).to eq 'hair_color'
expect(rep.first.serializable_hash[:value]).to eq 'brown'
end

it 'passes through custom options' do
module EntitySpec
class FriendEntity < Grape::Entity
Expand Down