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 (#381)

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

Fixes #378

* Autocorrect Rubocop offenses
  • Loading branch information
magni- authored Dec 13, 2023
1 parent 6267db4 commit 4557193
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
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
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?
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

0 comments on commit 4557193

Please sign in to comment.