Skip to content

Commit 2693258

Browse files
committed
Allow global settings on Grape::Entity.
This fixes a regression introduced after merging a ruby-grape#134. It's something undocumented before but it worked before and I don't see anything harmful in the presence of this feature.
1 parent 7235364 commit 2693258

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

lib/grape_entity/entity.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,18 @@ class << self
112112
attr_accessor :nested_exposures
113113
end
114114

115+
@exposures = {}
116+
@root_exposures = {}
117+
@nested_exposures = {}
118+
@nested_attribute_names = {}
119+
@formatters = {}
120+
115121
def self.inherited(subclass)
116-
subclass.exposures = exposures.try(:dup) || {}
117-
subclass.root_exposures = root_exposures.try(:dup) || {}
118-
subclass.nested_exposures = nested_exposures.try(:dup) || {}
119-
subclass.nested_attribute_names = nested_attribute_names.try(:dup) || {}
120-
subclass.formatters = formatters.try(:dup) || {}
122+
subclass.exposures = exposures.try(:dup)
123+
subclass.root_exposures = root_exposures.try(:dup)
124+
subclass.nested_exposures = nested_exposures.try(:dup)
125+
subclass.nested_attribute_names = nested_attribute_names.try(:dup)
126+
subclass.formatters = formatters.try(:dup)
121127
end
122128

123129
# This method is the primary means by which you will declare what attributes
@@ -183,7 +189,10 @@ def self.expose(*args, &block)
183189
end
184190

185191
def self.unexpose(attribute)
192+
root_exposures.delete(attribute)
186193
exposures.delete(attribute)
194+
nested_exposures.delete(attribute)
195+
nested_attribute_names.delete(attribute)
187196
end
188197

189198
# Set options that will be applied to any exposures declared inside the block.

spec/grape_entity/entity_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ class Parent < Person
278278
subject.expose(:size, format_with: :size_formatter)
279279
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
280280
end
281+
282+
it 'works global on Grape::Entity' do
283+
Grape::Entity.format_with :size_formatter do |_date|
284+
self.object.class.to_s
285+
end
286+
object = {}
287+
288+
subject.expose(:size, format_with: :size_formatter)
289+
expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s
290+
end
291+
end
292+
293+
it 'works global on Grape::Entity' do
294+
Grape::Entity.expose :x
295+
object = { x: 11, y: 22 }
296+
expect(Grape::Entity.represent(object).send(:value_for, :x)).to eq 11
297+
subject.expose :y
298+
expect(subject.represent(object).send(:value_for, :x)).to eq 11
299+
expect(subject.represent(object).send(:value_for, :y)).to eq 22
300+
Grape::Entity.unexpose :x
281301
end
282302
end
283303

@@ -310,6 +330,13 @@ class Parent < Person
310330
end
311331
end
312332
end
333+
334+
it 'works global on Grape::Entity' do
335+
Grape::Entity.expose :x
336+
expect(Grape::Entity.exposures).to eq(x: {})
337+
Grape::Entity.unexpose :x
338+
expect(Grape::Entity.exposures).to eq({})
339+
end
313340
end
314341

315342
describe '.with_options' do

0 commit comments

Comments
 (0)