Skip to content

Commit

Permalink
Merge pull request #472 from procore-oss/js/nil-view-fix
Browse files Browse the repository at this point in the history
[Bug Fix] Fallback to :default view if nil is provided on reder
  • Loading branch information
lessthanjacob authored Oct 3, 2024
2 parents 0e7f160 + 795953b commit 0212025
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/blueprinter/helpers/base_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module SingletonMethods
private

def prepare_for_render(object, options)
view_name = options.fetch(:view, :default)
view_name = options.fetch(:view, :default) || :default
root = options[:root]
meta = options[:meta]
validate_root_and_meta!(root, meta)
Expand Down
24 changes: 24 additions & 0 deletions spec/integrations/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@
describe '::render' do
subject { blueprint.render(obj) }

context 'when providing a view' do
let(:blueprint) do
Class.new(Blueprinter::Base) do
identifier :id
field :first_name

view :extended do
field :last_name
end
end
end
it 'renders the data based on the view definition' do
expect(blueprint.render(object_with_attributes, view: :extended)).
to eq('{"id":1,"first_name":"Meg","last_name":"Ryan"}')
end
context 'and the value is nil' do
it 'falls back to the :default view' do
expect(blueprint.render(object_with_attributes, view: nil)).
to eq(blueprint.render(object_with_attributes))
end
end
end

context 'Outside Rails project' do
context 'Given passed object has dot notation accessible attributes' do
let(:obj) { object_with_attributes }
Expand Down Expand Up @@ -544,6 +567,7 @@ def self.has_cars?(_field_name, object, local_options)
end
end
end

describe '::render_as_hash' do
subject { blueprint_with_block.render_as_hash(object_with_attributes) }
context 'Outside Rails project' do
Expand Down

0 comments on commit 0212025

Please sign in to comment.