Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch instead of delete from options hash #453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/blueprinter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions lib/blueprinter/helpers/base_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions spec/integrations/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down