Skip to content

raising ArgumentError if exposure_options[:using] does not respond to represent #99

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#99](https://github.com/intridea/grape-entity/pull/99): raising ArgumentError if exposure_options[:using] does not respond to represent - [@themusicman](https://github.com/themusicman).
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).

Expand Down Expand Up @@ -62,4 +63,3 @@ Next Release
==================

* Initial public release - [@agileanimal](https://github.com/agileanimal).

5 changes: 5 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,13 @@ def value_for(attribute, options = {})
nested_exposures = self.class.nested_exposures_for(attribute)

if exposure_options[:using]

exposure_options[:using] = exposure_options[:using].constantize if exposure_options[:using].respond_to? :constantize

unless exposure_options[:using].respond_to? :represent
raise ArgumentError, "The value you pass as the using option must respond to represent. It should be a class that inherits from Grape::Entity. [#{exposure_options[:using].inspect}]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just re-read this. It doesn't have to be a class that inherits Grape::Entity, that is just one possible class. I think it should say something like "#{exposure_options[:using].class} must implement represent.", what do you think?

end

using_options = options.dup
using_options.delete(:collection)
using_options[:root] = nil
Expand Down
12 changes: 12 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,18 @@ class UserEntity < Grape::Entity
expect(rep.size).to eq 2
expect(rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }).to be true
end

it 'makes sure represent can be called on the using option' do

class NotAnEntity; end

fresh_class.class_eval do
expose :not_an_entity, using: NotAnEntity
end

expect { subject.send(:value_for, :not_an_entity) }.to raise_error ArgumentError
end

end
end

Expand Down