Skip to content

Commit 4557193

Browse files
authored
Pass the right options when determining whether to expose an attribute using a block (#381)
* Pass the right options when determining whether to expose an attribute using a block Fixes #378 * Autocorrect Rubocop offenses
1 parent 6267db4 commit 4557193

File tree

7 files changed

+20
-17
lines changed

7 files changed

+20
-17
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ AllCops:
55
- vendor/**/*
66
- example/**/*
77
NewCops: enable
8-
TargetRubyVersion: 3.2
8+
TargetRubyVersion: 3.0
99
SuggestExtensions: false
1010

1111
# Layout stuff

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

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

1112

lib/grape_entity/condition/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Grape
44
class Entity
55
module Condition
66
class Base
7-
def self.new(inverse, *args, &block)
8-
super(inverse).tap { |e| e.setup(*args, &block) }
7+
def self.new(inverse, ...)
8+
super(inverse).tap { |e| e.setup(...) }
99
end
1010

1111
def initialize(inverse = false)

lib/grape_entity/entity.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ def self.merge_options(options)
598598
if existing_val.is_a?(Hash) && new_val.is_a?(Hash)
599599
existing_val.merge(new_val)
600600
elsif new_val.is_a?(Hash)
601-
(opts["#{key}_extras".to_sym] ||= []) << existing_val
601+
(opts[:"#{key}_extras"] ||= []) << existing_val
602602
new_val
603603
else
604-
(opts["#{key}_extras".to_sym] ||= []) << new_val
604+
(opts[:"#{key}_extras"] ||= []) << new_val
605605
existing_val
606606
end
607607
else

lib/grape_entity/exposure.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def compile_conditions(attribute, options)
5454

5555
def expose_nil_condition(attribute, options)
5656
Condition.new_unless(
57-
proc do |object, _options|
57+
proc do |object, entity_options|
5858
if options[:proc].nil?
5959
delegator = Delegator.new(object)
6060
if is_a?(Grape::Entity) && delegator.accepts_options?
@@ -63,7 +63,7 @@ def expose_nil_condition(attribute, options)
6363
delegator.delegate(attribute).nil?
6464
end
6565
else
66-
exec_with_object(options, &options[:proc]).nil?
66+
exec_with_object(entity_options, &options[:proc]).nil?
6767
end
6868
end
6969
)

lib/grape_entity/exposure/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module Exposure
99
class Base
1010
attr_reader :attribute, :is_safe, :documentation, :override, :conditions, :for_merge
1111

12-
def self.new(attribute, options, conditions, *args, &block)
13-
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
12+
def self.new(attribute, options, conditions, ...)
13+
super(attribute, options, conditions).tap { |e| e.setup(...) }
1414
end
1515

1616
def initialize(attribute, options, conditions)

spec/grape_entity/entity_spec.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
it 'makes sure that :format_with as a proc cannot be used with a block' do
3232
# rubocop:disable Style/BlockDelimiters
33-
# rubocop:disable Lint/Debugger
34-
expect { subject.expose :name, format_with: proc {} do p 'hi' end }.to raise_error ArgumentError
35-
# rubocop:enable Lint/Debugger
33+
expect {
34+
subject.expose :name, format_with: proc {} do
35+
p 'hi'
36+
end
37+
}.to raise_error ArgumentError
3638
# rubocop:enable Style/BlockDelimiters
3739
end
3840

@@ -131,21 +133,21 @@ def initialize(a, b, c)
131133

132134
context 'when expose_nil option is false and block passed' do
133135
it 'does not expose if block returns nil' do
134-
subject.expose(:a, expose_nil: false) do |_obj, _options|
135-
nil
136+
subject.expose(:a, expose_nil: false) do |_obj, options|
137+
options[:option_a]
136138
end
137139
subject.expose(:b)
138140
subject.expose(:c)
139141
expect(subject.represent(model).serializable_hash).to eq(b: nil, c: 'value')
140142
end
141143

142144
it 'exposes is block returns a value' do
143-
subject.expose(:a, expose_nil: false) do |_obj, _options|
144-
100
145+
subject.expose(:a, expose_nil: false) do |_obj, options|
146+
options[:option_a]
145147
end
146148
subject.expose(:b)
147149
subject.expose(:c)
148-
expect(subject.represent(model).serializable_hash).to eq(a: 100, b: nil, c: 'value')
150+
expect(subject.represent(model, option_a: 100).serializable_hash).to eq(a: 100, b: nil, c: 'value')
149151
end
150152
end
151153
end

0 commit comments

Comments
 (0)