Skip to content

Commit

Permalink
Merge pull request #23 from aliscott/render_hash_dump
Browse files Browse the repository at this point in the history
Properly render collections of Attributor::Hash when fields=true
  • Loading branch information
blanquer committed Jan 12, 2016
2 parents 32b57ed + 9e0da42 commit 9962d0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## next

* Ensure we call `object.dump` in Renderer when dumping a Attributor::Hash if no subfields were selected.
* Ensure we call `object.dump` in Renderer when dumping a Attributor::Hash or collection of Attributor::Hash if no subfields were selected.

## 3.1

Expand Down
9 changes: 8 additions & 1 deletion lib/praxis-blueprints/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ def render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
end

def _render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
return object if fields == true
if fields == true
return case object
when Attributor::Hash
object.dump
else
object
end
end

notification_payload = {
blueprint: object,
Expand Down
25 changes: 24 additions & 1 deletion spec/praxis-blueprints/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@

end

context 'rendering collections of hashes' do
context 'rendering hashes' do
let(:fields) do
{
id: true,
Expand All @@ -170,4 +170,27 @@
its([:hash]) { should be_kind_of(Hash) }
end

context 'rendering collections of hashes' do
let(:fields) do
{
id: true,
hash_collection: [true]
}
end

let(:data) { {id: 10, hash_collection: [{foo: 'bar'}]} }
let(:object) { SimpleHashCollection.load(data)}
let(:renderer) { Praxis::Renderer.new }

subject(:output) { renderer.render(object, fields) }

its([:id]) { should eq data[:id] }
its([:hash_collection]) { should eq data[:hash_collection] }
its([:hash_collection]) { should be_kind_of(Array) }

it 'renders the hashes' do
expect(output[:hash_collection].first).to be_kind_of(Hash)
end
end

end
8 changes: 8 additions & 0 deletions spec/support/spec_blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ class SimpleHash < Attributor::Model
attribute :hash, Hash
end
end


class SimpleHashCollection < Attributor::Model
attributes do
attribute :id, Integer
attribute :hash_collection, Attributor::Collection.of(Hash)
end
end

0 comments on commit 9962d0d

Please sign in to comment.