diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index b37961c0..fcf7438a 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -153,11 +153,16 @@ def self.association(method, options = {}, &block) current_view << Association.new( method: method, - name: options.delete(:name) || method, - extractor: options.delete(:extractor) || AssociationExtractor.new, - blueprint: options.delete(:blueprint), - view: options.delete(:view) || :default, - options: options.merge(block: block) + name: options.fetch(:name) { method }, + extractor: options.fetch(:extractor) { AssociationExtractor.new }, + blueprint: options.fetch(:blueprint), + view: options.fetch(:view, :default), + options: options.except( + :name, + :extractor, + :blueprint, + :view + ).merge(block: block) ) end diff --git a/lib/blueprinter/helpers/base_helpers.rb b/lib/blueprinter/helpers/base_helpers.rb index 7e094ff8..73ab7711 100644 --- a/lib/blueprinter/helpers/base_helpers.rb +++ b/lib/blueprinter/helpers/base_helpers.rb @@ -15,11 +15,21 @@ module SingletonMethods private def prepare_for_render(object, options) - view_name = options.delete(:view) || :default - root = options.delete(:root) - meta = options.delete(:meta) + view_name = options.fetch(:view, :default) + root = options[:root] + meta = options[:meta] validate_root_and_meta!(root, meta) - prepare(object, view_name: view_name, local_options: options, root: root, meta: meta) + prepare( + object, + view_name: view_name, + local_options: options.except( + :view, + :root, + :meta + ), + root: root, + meta: meta + ) end def prepare_data(object, view_name, local_options) diff --git a/spec/integrations/base_spec.rb b/spec/integrations/base_spec.rb index 7eb95f98..a731706d 100644 --- a/spec/integrations/base_spec.rb +++ b/spec/integrations/base_spec.rb @@ -478,6 +478,27 @@ def extract(association_name, object, _local_options, _options={}) after { reset_blueprinter_config! } it('returns the expected result') { should eq(result) } + + context 'Given options containing `view` and rendered multiple times (as in batching)' do + let(:blueprint) do + Class.new(Blueprinter::Base) do + field :id + view :with_make do + field :make + end + end + end + + let(:options) { { view: :with_make } } + + subject do + obj.map do |vehicle| + blueprint.render_as_hash(vehicle, options) + end.to_json + end + + it('returns the expected result') { should eq(result) } + end end end end