Skip to content

Commit

Permalink
Pass the right options when determining whether to expose an attribut…
Browse files Browse the repository at this point in the history
…e using a block

Fixes ruby-grape#378
  • Loading branch information
magni- committed Dec 13, 2023
1 parent 6267db4 commit 7fe04a6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/grape_entity/exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def compile_conditions(attribute, options)

def expose_nil_condition(attribute, options)
Condition.new_unless(
proc do |object, _options|
proc do |object, entity_options|
if options[:proc].nil?
delegator = Delegator.new(object)
if is_a?(Grape::Entity) && delegator.accepts_options?
Expand All @@ -63,7 +63,7 @@ def expose_nil_condition(attribute, options)
delegator.delegate(attribute).nil?
end
else
exec_with_object(options, &options[:proc]).nil?
exec_with_object(entity_options, &options[:proc]).nil?
end
end
)
Expand Down
10 changes: 5 additions & 5 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,21 @@ def initialize(a, b, c)

context 'when expose_nil option is false and block passed' do
it 'does not expose if block returns nil' do
subject.expose(:a, expose_nil: false) do |_obj, _options|
nil
subject.expose(:a, expose_nil: false) do |_obj, options|
options[:option_a]
end
subject.expose(:b)
subject.expose(:c)
expect(subject.represent(model).serializable_hash).to eq(b: nil, c: 'value')
end

it 'exposes is block returns a value' do
subject.expose(:a, expose_nil: false) do |_obj, _options|
100
subject.expose(:a, expose_nil: false) do |_obj, options|
options[:option_a]
end
subject.expose(:b)
subject.expose(:c)
expect(subject.represent(model).serializable_hash).to eq(a: 100, b: nil, c: 'value')
expect(subject.represent(model, option_a: 100).serializable_hash).to eq(a: 100, b: nil, c: 'value')
end
end
end
Expand Down

0 comments on commit 7fe04a6

Please sign in to comment.