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

Fix wrong argument exception when &:block passed to the expose method #288

Merged
merged 1 commit into from
Jan 11, 2018
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#### Fixes

* [#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)

* Your contribution here.

### 0.6.1 (2017-01-09)
Expand Down
6 changes: 5 additions & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,11 @@ def serializable_hash(runtime_options = {})
end

def exec_with_object(options, &block)
instance_exec(object, options, &block)
if block.parameters.count == 1
instance_exec(object, &block)
else
instance_exec(object, options, &block)
end
end

def exec_with_attribute(attribute, &block)
Expand Down
24 changes: 24 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ class BogusEntity < Grape::Entity
end
end

context 'with block passed via &' do
it 'with does not pass options when block is passed via &' do
class SomeObject
def method_without_args
'result'
end
end

subject.expose :that_method_without_args do |object|
object.method_without_args
end

subject.expose :that_method_without_args_again, &:method_without_args

object = SomeObject.new

value = subject.represent(object).value_for(:that_method_without_args)
expect(value).to eq('result')

value2 = subject.represent(object).value_for(:that_method_without_args_again)
expect(value2).to eq('result')
end
end

context 'with no parameters passed to the block' do
it 'adds a nested exposure' do
subject.expose :awesome do
Expand Down