From 4dc39b15dcfdfda6f583621ec8a5880346df49c1 Mon Sep 17 00:00:00 2001 From: Ryan McCarthy Date: Thu, 8 Aug 2024 14:02:56 -0700 Subject: [PATCH 1/3] Fetch instead of delete from options hash Signed-off-by: Ryan McCarthy --- lib/blueprinter/base.rb | 8 ++++---- lib/blueprinter/helpers/base_helpers.rb | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index b37961c0..772059a4 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -153,10 +153,10 @@ 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, + name: options.fetch(:name) { method }, + extractor: options.fetch(:extractor) { AssociationExtractor.new }, + blueprint: options.fetch(:blueprint), + view: options.fetch(:view, :default), options: options.merge(block: block) ) end diff --git a/lib/blueprinter/helpers/base_helpers.rb b/lib/blueprinter/helpers/base_helpers.rb index 7e094ff8..da3562ed 100644 --- a/lib/blueprinter/helpers/base_helpers.rb +++ b/lib/blueprinter/helpers/base_helpers.rb @@ -15,9 +15,9 @@ 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) end From e01fb21ac4e5bd4dfe659d605b2636b0c2bd3a40 Mon Sep 17 00:00:00 2001 From: Ryan McCarthy Date: Mon, 26 Aug 2024 10:14:48 -0700 Subject: [PATCH 2/3] Address PR comments Signed-off-by: Ryan McCarthy --- lib/blueprinter/base.rb | 7 ++++++- lib/blueprinter/helpers/base_helpers.rb | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index 772059a4..fcf7438a 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -157,7 +157,12 @@ def self.association(method, options = {}, &block) extractor: options.fetch(:extractor) { AssociationExtractor.new }, blueprint: options.fetch(:blueprint), view: options.fetch(:view, :default), - options: options.merge(block: block) + 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 da3562ed..73ab7711 100644 --- a/lib/blueprinter/helpers/base_helpers.rb +++ b/lib/blueprinter/helpers/base_helpers.rb @@ -19,7 +19,17 @@ def prepare_for_render(object, options) 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) From 28f3fd79425a4fbb5dc0082568adfbb781e24303 Mon Sep 17 00:00:00 2001 From: Ryan McCarthy Date: Mon, 26 Aug 2024 13:13:20 -0700 Subject: [PATCH 3/3] Add spec Signed-off-by: Ryan McCarthy --- spec/integrations/base_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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