Skip to content

Commit be144f7

Browse files
DmitryTsepelevLeFnord
authored andcommitted
[#288] Entity#exec_with_object should not call &block with options when block accepts only one argument (#288)
1 parent 064ecfb commit be144f7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#### Fixes
1414

15+
* [#288](https://github.com/ruby-grape/grape-entity/pull/288) Fix wrong argument exception when &:block passed to the expose method - [@DmitryTsepelev](https://github.com/DmitryTsepelev)
16+
1517
* Your contribution here.
1618

1719
### 0.6.1 (2017-01-09)

lib/grape_entity/entity.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ def serializable_hash(runtime_options = {})
492492
end
493493

494494
def exec_with_object(options, &block)
495-
instance_exec(object, options, &block)
495+
if block.parameters.count == 1
496+
instance_exec(object, &block)
497+
else
498+
instance_exec(object, options, &block)
499+
end
496500
end
497501

498502
def exec_with_attribute(attribute, &block)

spec/grape_entity/entity_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ class BogusEntity < Grape::Entity
114114
end
115115
end
116116

117+
context 'with block passed via &' do
118+
it 'with does not pass options when block is passed via &' do
119+
class SomeObject
120+
def method_without_args
121+
'result'
122+
end
123+
end
124+
125+
subject.expose :that_method_without_args do |object|
126+
object.method_without_args
127+
end
128+
129+
subject.expose :that_method_without_args_again, &:method_without_args
130+
131+
object = SomeObject.new
132+
133+
value = subject.represent(object).value_for(:that_method_without_args)
134+
expect(value).to eq('result')
135+
136+
value2 = subject.represent(object).value_for(:that_method_without_args_again)
137+
expect(value2).to eq('result')
138+
end
139+
end
140+
117141
context 'with no parameters passed to the block' do
118142
it 'adds a nested exposure' do
119143
subject.expose :awesome do

0 commit comments

Comments
 (0)