Skip to content

Commit 4b2b55d

Browse files
author
Michael Bleigh
committed
Merge pull request ruby-grape#151 from bobbytables/frontier-exposure-inheritance
Exposure Inheritance
2 parents 65333c6 + 67e199f commit 4b2b55d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/grape/entity.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,17 @@ def self.expose(*args, &block)
7878
end
7979
end
8080

81-
# Returns a hash of exposures that have been declared for this Entity. The keys
81+
# Returns a hash of exposures that have been declared for this Entity or ancestors. The keys
8282
# are symbolized references to methods on the containing object, the values are
8383
# the options that were passed into expose.
8484
def self.exposures
85-
(@exposures ||= {})
85+
@exposures ||= {}
86+
87+
if superclass.respond_to? :exposures
88+
@exposures = superclass.exposures.merge(@exposures)
89+
end
90+
91+
@exposures
8692
end
8793

8894
# This allows you to set a root element name for your representation.

spec/grape/entity_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@
3939
subject.exposures[:name][:proc].should == block
4040
end
4141
end
42+
43+
context 'inherited exposures' do
44+
it 'should return exposures from an ancestor' do
45+
subject.expose :name, :email
46+
child_class = Class.new(subject)
47+
48+
child_class.exposures.should eq(subject.exposures)
49+
end
50+
51+
it 'should return exposures from multiple ancestor' do
52+
subject.expose :name, :email
53+
parent_class = Class.new(subject)
54+
child_class = Class.new(parent_class)
55+
56+
child_class.exposures.should eq(subject.exposures)
57+
end
58+
59+
it 'should return descendant exposures as a priotity' do
60+
subject.expose :name, :email
61+
child_class = Class.new(subject)
62+
child_class.expose :name do |n|
63+
'foo'
64+
end
65+
66+
subject.exposures[:name].should_not have_key :proc
67+
child_class.exposures[:name].should have_key :proc
68+
end
69+
end
4270
end
4371

4472
describe '.represent' do

0 commit comments

Comments
 (0)