Skip to content

Commit e1815d7

Browse files
committed
Fix: allow usage of attributes with name 'key' if Hash objects are used
1 parent c3a1acd commit e1815d7

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Metrics/AbcSize:
1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 300
15+
Max: 301
1616

1717
# Offense count: 4
1818
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Next Release
22
============
33

44
* Your contribution here.
5+
* [#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).
56
* [#110](https://github.com/intridea/grape-entity/pull/110): Fixed safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
67
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
78
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).

lib/grape_entity/entity.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,17 +556,17 @@ def delegate_attribute(attribute)
556556
name = self.class.name_for(attribute)
557557
if respond_to?(name, true)
558558
send(name)
559+
elsif object.is_a?(Hash)
560+
object[name]
561+
elsif object.respond_to?(name, true)
562+
object.send(name)
563+
elsif object.respond_to?(:fetch, true)
564+
object.fetch(name)
559565
else
560-
if object.respond_to?(name, true)
566+
begin
561567
object.send(name)
562-
elsif object.respond_to?(:fetch, true)
563-
object.fetch(name)
564-
else
565-
begin
566-
object.send(name)
567-
rescue NoMethodError
568-
raise NoMethodError, "#{self.class.name} missing attribute `#{name}' on #{object}"
569-
end
568+
rescue NoMethodError
569+
raise NoMethodError, "#{self.class.name} missing attribute `#{name}' on #{object}"
570570
end
571571
end
572572
end

spec/grape_entity/entity_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,12 @@ class Parent < Person
649649
650650
birthday: Time.gm(2012, 2, 27),
651651
fantasies: ['Unicorns', 'Double Rainbows', 'Nessy'],
652+
characteristics: [
653+
{ key: 'hair_color', value: 'brown' }
654+
],
652655
friends: [
653-
double(name: 'Friend 1', email: '[email protected]', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
654-
double(name: 'Friend 2', email: '[email protected]', fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
656+
double(name: 'Friend 1', email: '[email protected]', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
657+
double(name: 'Friend 2', email: '[email protected]', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
655658
]
656659
}
657660
end
@@ -913,6 +916,25 @@ class FriendEntity < Grape::Entity
913916
expect(rep.serializable_hash).to be_nil
914917
end
915918

919+
it 'passes through exposed entity with key and value attributes' do
920+
module EntitySpec
921+
class CharacteristicsEntity < Grape::Entity
922+
root 'characteristics', 'characteristic'
923+
expose :key, :value
924+
end
925+
end
926+
927+
fresh_class.class_eval do
928+
expose :characteristics, using: EntitySpec::CharacteristicsEntity
929+
end
930+
931+
rep = subject.send(:value_for, :characteristics)
932+
expect(rep).to be_kind_of Array
933+
expect(rep.reject { |r| r.is_a?(EntitySpec::CharacteristicsEntity) }).to be_empty
934+
expect(rep.first.serializable_hash[:key]).to eq 'hair_color'
935+
expect(rep.first.serializable_hash[:value]).to eq 'brown'
936+
end
937+
916938
it 'passes through custom options' do
917939
module EntitySpec
918940
class FriendEntity < Grape::Entity

0 commit comments

Comments
 (0)