Skip to content

Commit 98f8ba2

Browse files
author
James McCarthy
committed
Remove overridden :key attribute. And test key method.
1 parent cd38ec0 commit 98f8ba2

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/grape_entity/exposure/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
class Entity
55
module Exposure
66
class Base
7-
attr_reader :attribute, :key, :is_safe, :documentation, :conditions, :for_merge
7+
attr_reader :attribute, :is_safe, :documentation, :conditions, :for_merge
88

99
def self.new(attribute, options, conditions, *args, &block)
1010
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
@@ -106,7 +106,7 @@ def attr_path(entity, options)
106106
end
107107

108108
def key(entity = nil)
109-
@key.respond_to?(:call) ? @key.call(entity).try(:to_sym) : @key
109+
@key.respond_to?(:call) ? @key.call(entity) : @key
110110
end
111111

112112
def with_attr_path(entity, options)

spec/grape_entity/exposure_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626
describe '#key' do
2727
it 'returns the attribute if no :as is set' do
2828
fresh_class.expose :name
29-
expect(subject.key).to eq :name
29+
expect(subject.key(entity)).to eq :name
3030
end
3131

3232
it 'returns the :as alias if one exists' do
3333
fresh_class.expose :name, as: :nombre
34-
expect(subject.key).to eq :nombre
34+
expect(subject.key(entity)).to eq :nombre
35+
end
36+
37+
it 'returns the result if :as is a proc' do
38+
fresh_class.expose :name, as: -> (entity) { entity.object.name.reverse }
39+
expect(subject.key(entity)).to eq(model.name.reverse)
3540
end
3641
end
3742

0 commit comments

Comments
 (0)