From 758193d3d7e5d082e9121715b2d549d7aa882c8f Mon Sep 17 00:00:00 2001 From: Thomas Brewer Date: Thu, 20 Nov 2014 23:22:56 -0500 Subject: [PATCH 1/3] raising ArgumentError if exposure_options[:using] does not respond to represent --- lib/grape_entity/entity.rb | 5 +++++ spec/grape_entity/entity_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 9bd644b1..c495152b 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -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}]" + end + using_options = options.dup using_options.delete(:collection) using_options[:root] = nil diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 2d19a656..6680b871 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -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 From a15021af64e549e339a4538700c704a247dee834 Mon Sep 17 00:00:00 2001 From: Thomas Brewer Date: Fri, 21 Nov 2014 18:37:36 -0500 Subject: [PATCH 2/3] fixing trailing whitespace issue --- spec/grape_entity/entity_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 6680b871..9b12d7f3 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -984,7 +984,7 @@ 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 From 7517366d99af39f3c74de971f05e4db8911308d5 Mon Sep 17 00:00:00 2001 From: Thomas Brewer Date: Fri, 21 Nov 2014 18:41:35 -0500 Subject: [PATCH 3/3] updating CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59ba4848..d94d8f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). @@ -62,4 +63,3 @@ Next Release ================== * Initial public release - [@agileanimal](https://github.com/agileanimal). -