diff --git a/CHANGELOG.md b/CHANGELOG.md index b754672..4b0e3f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/praxis-blueprints/renderer.rb b/lib/praxis-blueprints/renderer.rb index dddda6a..5936591 100644 --- a/lib/praxis-blueprints/renderer.rb +++ b/lib/praxis-blueprints/renderer.rb @@ -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, diff --git a/spec/praxis-blueprints/renderer_spec.rb b/spec/praxis-blueprints/renderer_spec.rb index 8fb4c11..049a3e2 100644 --- a/spec/praxis-blueprints/renderer_spec.rb +++ b/spec/praxis-blueprints/renderer_spec.rb @@ -151,7 +151,7 @@ end - context 'rendering collections of hashes' do + context 'rendering hashes' do let(:fields) do { id: true, @@ -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 diff --git a/spec/support/spec_blueprints.rb b/spec/support/spec_blueprints.rb index 18f3f09..017230b 100644 --- a/spec/support/spec_blueprints.rb +++ b/spec/support/spec_blueprints.rb @@ -102,3 +102,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