diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b0e3f2..d34ada8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## next -* Ensure we call `object.dump` in Renderer when dumping a Attributor::Hash or collection of Attributor::Hash if no subfields were selected. +* Ensure we call `object.dump` in Renderer when fully dumping an instance (or array of instances) that have the Attributor::Dumpable module (i.e., when no subfields were selected) + ## 3.1 diff --git a/lib/praxis-blueprints/renderer.rb b/lib/praxis-blueprints/renderer.rb index 5936591..b2950ff 100644 --- a/lib/praxis-blueprints/renderer.rb +++ b/lib/praxis-blueprints/renderer.rb @@ -57,7 +57,7 @@ def render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT) def _render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT) if fields == true return case object - when Attributor::Hash + when Attributor::Dumpable object.dump else object @@ -82,7 +82,7 @@ def _render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT) if subfields == true hash[key] = case value - when Attributor::Hash + when Attributor::Dumpable value.dump else value diff --git a/spec/praxis-blueprints/renderer_spec.rb b/spec/praxis-blueprints/renderer_spec.rb index 049a3e2..80e675b 100644 --- a/spec/praxis-blueprints/renderer_spec.rb +++ b/spec/praxis-blueprints/renderer_spec.rb @@ -4,13 +4,21 @@ let(:address) { Address.example } let(:prior_addresses) { 2.times.collect { Address.example } } + let(:alias_one) { FullName.example } + let(:alias_two) { FullName.example } + let(:aliases) { [alias_one, alias_two] } + let(:metadata_hash) { { something: 'here' } } + let(:metadata) { Attributor::Hash.load( metadata_hash ) } + let(:person) do Person.example( address: address, email: nil, prior_addresses: prior_addresses, alive: false, - work_address: nil + work_address: nil, + aliases: aliases, + metadata: metadata ) end @@ -27,7 +35,9 @@ }, prior_addresses: [{name: true}], work_address: true, - alive: true + alive: true, + metadata: true, + aliases: [true] } end @@ -36,7 +46,7 @@ subject(:output) { renderer.render(person, fields) } it 'renders existing attributes' do - output.keys.should match_array([:name, :full_name, :alive, :address, :prior_addresses]) + output.keys.should match_array([:name, :full_name, :alive, :address, :prior_addresses, :metadata, :aliases]) output[:name].should eq person.name output[:full_name].should eq({first: person.full_name.first, last: person.full_name.last}) @@ -50,6 +60,23 @@ expected_prior_addresses = prior_addresses.collect { |addr| {name: addr.name} } output[:prior_addresses].should match_array(expected_prior_addresses) + + expected_aliases = aliases.collect { |the_alias| the_alias.dump } + output[:aliases].should match_array( expected_aliases ) + + output[:metadata].should eq( metadata.dump ) + end + + context 'calls dump for non-Blueprint, but still Dumpable instances' do + it 'when rendering them in full as array members' do + alias_one.should_receive(:dump).and_call_original + output[:aliases].first.should eq( first: alias_one.first, last: alias_one.last ) + end + it 'when rendering them in full as leaf object' do + metadata.should_receive(:dump).and_call_original + output[:metadata].should eq( metadata_hash ) + end + end it 'does not render attributes with nil values' do diff --git a/spec/support/spec_blueprints.rb b/spec/support/spec_blueprints.rb index 0c721e9..51444e5 100644 --- a/spec/support/spec_blueprints.rb +++ b/spec/support/spec_blueprints.rb @@ -22,6 +22,7 @@ class Person < Praxis::Blueprint attribute :alive, Attributor::Boolean, default: true attribute :myself, Person attribute :friends, Attributor::Collection.of(Person) + attribute :metadata, Attributor::Hash end view :default do