Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the right options when determining whether to expose an attribute using a block #381

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AllCops:
- vendor/**/*
- example/**/*
NewCops: enable
TargetRubyVersion: 3.2
TargetRubyVersion: 3.0
LeFnord marked this conversation as resolved.
Show resolved Hide resolved
SuggestExtensions: false

# Layout stuff
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#381](https://github.com/ruby-grape/grape-entity/pull/381): Fix `expose_nil: false` when using a block - [@magni-](https://github.com/magni-).
* Your contribution here.


Expand Down
4 changes: 2 additions & 2 deletions lib/grape_entity/condition/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Grape
class Entity
module Condition
class Base
def self.new(inverse, *args, &block)
super(inverse).tap { |e| e.setup(*args, &block) }
def self.new(inverse, ...)
super(inverse).tap { |e| e.setup(...) }
end

def initialize(inverse = false)
Expand Down
4 changes: 2 additions & 2 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ def self.merge_options(options)
if existing_val.is_a?(Hash) && new_val.is_a?(Hash)
existing_val.merge(new_val)
elsif new_val.is_a?(Hash)
(opts["#{key}_extras".to_sym] ||= []) << existing_val
(opts[:"#{key}_extras"] ||= []) << existing_val
new_val
else
(opts["#{key}_extras".to_sym] ||= []) << new_val
(opts[:"#{key}_extras"] ||= []) << new_val
existing_val
end
else
Expand Down
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?
LeFnord marked this conversation as resolved.
Show resolved Hide resolved
end
end
)
Expand Down
4 changes: 2 additions & 2 deletions lib/grape_entity/exposure/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module Exposure
class Base
attr_reader :attribute, :is_safe, :documentation, :override, :conditions, :for_merge

def self.new(attribute, options, conditions, *args, &block)
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
def self.new(attribute, options, conditions, ...)
super(attribute, options, conditions).tap { |e| e.setup(...) }
end

def initialize(attribute, options, conditions)
Expand Down
18 changes: 10 additions & 8 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

it 'makes sure that :format_with as a proc cannot be used with a block' do
# rubocop:disable Style/BlockDelimiters
# rubocop:disable Lint/Debugger
expect { subject.expose :name, format_with: proc {} do p 'hi' end }.to raise_error ArgumentError
# rubocop:enable Lint/Debugger
expect {
subject.expose :name, format_with: proc {} do
p 'hi'
end
}.to raise_error ArgumentError
# rubocop:enable Style/BlockDelimiters
end

Expand Down Expand Up @@ -131,21 +133,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