Skip to content

Commit 96afd88

Browse files
serbiantLeFnord
andauthored
Option expose_nil doesn't work when block is passed (#329)
* Makes expose_nil option work well with blocks. * Updated CHANGELOG.md Co-authored-by: peter scholz <[email protected]>
1 parent 54f68b2 commit 96afd88

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#### Fixes
1111

1212
* Your contribution here.
13+
* [#329](https://github.com/ruby-grape/grape-entity/pull/329): Option expose_nil doesn't work when block is passed - [@serbiant](https://github.com/serbiant).
1314
* [#320](https://github.com/ruby-grape/grape-entity/pull/320): Gemspec: drop eol'd property rubyforge_project - [@olleolleolle](https://github.com/olleolleolle).
1415
* [#307](https://github.com/ruby-grape/grape-entity/pull/307): Allow exposures to call methods defined in modules included in an entity - [@robertoz-01](https://github.com/robertoz-01).
1516

lib/grape_entity/exposure.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ def compile_conditions(attribute, options)
4747
options[:unless]
4848
].compact.flatten.map { |cond| Condition.new_unless(cond) }
4949

50-
unless_conditions << expose_nil_condition(attribute) if options[:expose_nil] == false
50+
unless_conditions << expose_nil_condition(attribute, options) if options[:expose_nil] == false
5151

5252
if_conditions + unless_conditions
5353
end
5454

55-
def expose_nil_condition(attribute)
55+
def expose_nil_condition(attribute, options)
5656
Condition.new_unless(
57-
proc { |object, _options| Delegator.new(object).delegate(attribute).nil? }
57+
proc do |object, _options|
58+
if options[:proc].nil?
59+
Delegator.new(object).delegate(attribute).nil?
60+
else
61+
exec_with_object(options, &options[:proc]).nil?
62+
end
63+
end
5864
)
5965
end
6066

spec/grape_entity/entity_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ def initialize(a, b, c)
126126
expect { subject.expose(:a, :b, :c, expose_nil: false) }.to raise_error ArgumentError
127127
end
128128
end
129+
130+
context 'when expose_nil option is false and block passed' do
131+
it 'does not expose if block returns nil' do
132+
subject.expose(:a, expose_nil: false) do |_obj, _options|
133+
nil
134+
end
135+
subject.expose(:b)
136+
subject.expose(:c)
137+
expect(subject.represent(model).serializable_hash).to eq(b: nil, c: 'value')
138+
end
139+
140+
it 'exposes is block returns a value' do
141+
subject.expose(:a, expose_nil: false) do |_obj, _options|
142+
100
143+
end
144+
subject.expose(:b)
145+
subject.expose(:c)
146+
expect(subject.represent(model).serializable_hash).to eq(a: 100, b: nil, c: 'value')
147+
end
148+
end
129149
end
130150

131151
context 'when model is a hash' do

0 commit comments

Comments
 (0)