Skip to content

Commit

Permalink
Merge pull request #307 from brandonweiss/presenting-arrays
Browse files Browse the repository at this point in the history
Fix #305: presenting arrays of objects when the entity class isn't explicitly passed into present
  • Loading branch information
dblock committed Jan 7, 2013
2 parents 14262f3 + 42e29da commit f75b310
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.2.5 (Next Release)
====================

* [#305](https://github.com/intridea/grape/issues/305): Fix: presenting arrays of objects didn't work when declaring representations via `represent` or when auto-detecting an `Entity` constant in the objects being presented - [@brandonweiss](https://github.com/brandonweiss).
* Your Contribution Here

0.2.4 (01/06/2013)
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,12 @@ def body(value = nil)
def present(object, options = {})
entity_class = options.delete(:with)

object.class.ancestors.each do |potential|
(object.is_a?(Array) ? object.first : object).class.ancestors.each do |potential|
entity_class ||= (settings[:representations] || {})[potential]
end

entity_class ||= object.class.const_get(:Entity) if object.class.const_defined?(:Entity)
entity_class ||= object.first.class.const_get(:Entity) if object.is_a?(Array) && object.first.class.const_defined?(:Entity)

root = options.delete(:root)

Expand Down
34 changes: 31 additions & 3 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def app; subject end
describe '#initialize' do
it 'takes a settings stack, options, and a block' do
p = Proc.new {}
expect {
expect {
Grape::Endpoint.new(Grape::Util::HashStack.new, {
:path => '/',
:method => :get
}, &p)
}, &p)
}.not_to raise_error
end
end
Expand Down Expand Up @@ -437,6 +437,20 @@ def memoized
last_response.body.should == 'Hiya'
end

it 'pulls a representation from the class options if the presented object is a collection of objects' do
entity = Class.new(Grape::Entity)
entity.stub!(:represent).and_return("Hiya")

class TestObject; end

subject.represent TestObject, :with => entity
subject.get '/example' do
present [TestObject.new]
end
get '/example'
last_response.body.should == "Hiya"
end

it 'pulls a representation from the class ancestor if it exists' do
entity = Class.new(Grape::Entity)
entity.stub!(:represent).and_return("Hiya")
Expand Down Expand Up @@ -465,6 +479,20 @@ def memoized
last_response.body.should == 'Auto-detect!'
end

it 'automatically uses Klass::Entity based on the first object in the collection being presented' do
some_model = Class.new
entity = Class.new(Grape::Entity)
entity.stub!(:represent).and_return("Auto-detect!")

some_model.const_set :Entity, entity

subject.get '/example' do
present [some_model.new]
end
get '/example'
last_response.body.should == 'Auto-detect!'
end

it 'adds a root key to the output if one is given' do
subject.get '/example' do
present({:abc => 'def'}, :root => :root)
Expand Down Expand Up @@ -606,5 +634,5 @@ def initialize(id)
last_response.body.should == "http://example.org/api/v1/url"
end
end

end

0 comments on commit f75b310

Please sign in to comment.