Skip to content

Commit 3790bc2

Browse files
author
James McCarthy
committed
Remove overridden :key attribute. And test key method.
1 parent cd1b727 commit 3790bc2

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
@@ -2,7 +2,7 @@ module Grape
22
class Entity
33
module Exposure
44
class Base
5-
attr_reader :attribute, :key, :is_safe, :documentation, :conditions, :for_merge
5+
attr_reader :attribute, :is_safe, :documentation, :conditions, :for_merge
66

77
def self.new(attribute, options, conditions, *args, &block)
88
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
@@ -104,7 +104,7 @@ def attr_path(entity, options)
104104
end
105105

106106
def key(entity = nil)
107-
@key.respond_to?(:call) ? @key.call(entity).try(:to_sym) : @key
107+
@key.respond_to?(:call) ? @key.call(entity) : @key
108108
end
109109

110110
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
@@ -24,12 +24,17 @@
2424
describe '#key' do
2525
it 'returns the attribute if no :as is set' do
2626
fresh_class.expose :name
27-
expect(subject.key).to eq :name
27+
expect(subject.key(entity)).to eq :name
2828
end
2929

3030
it 'returns the :as alias if one exists' do
3131
fresh_class.expose :name, as: :nombre
32-
expect(subject.key).to eq :nombre
32+
expect(subject.key(entity)).to eq :nombre
33+
end
34+
35+
it 'returns the result if :as is a proc' do
36+
fresh_class.expose :name, as: -> (entity) { entity.object.name.reverse }
37+
expect(subject.key(entity)).to eq(model.name.reverse)
3338
end
3439
end
3540

0 commit comments

Comments
 (0)