Skip to content

Commit

Permalink
Don't allow redefining the default view
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Hollinger <[email protected]>
  • Loading branch information
jhollinger committed Mar 3, 2025
1 parent 69f3ab8 commit f60bb0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/blueprinter/v2/view_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def initialize(parent)
# @param definition [Proc]
#
def []=(name, definition)
@pending[name.to_sym] = definition
name = name.to_sym
raise Errors::InvalidBlueprint, "You may not redefine the default view" if name == :default

@pending[name] = definition
end

#
Expand Down
15 changes: 14 additions & 1 deletion spec/v2/view_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe "Blueprinter::V2::ViewBuilder" do
describe Blueprinter::V2::ViewBuilder do
let(:builder) do
Blueprinter::V2::ViewBuilder.new(blueprint)
end
Expand Down Expand Up @@ -60,4 +60,17 @@
keys = builder.each.map { |name, _| name }
expect(keys.sort).to eq %i(default foo bar).sort
end

it "should not throw an error if you try to redefine an existing view" do
builder[:foo] = proc { field :description }
expect do
builder[:foo] = proc { field :description }
end.to_not raise_error
end

it "should throw an error if you try to define the default view" do
expect {
builder[:default] = proc { field :description }
}.to raise_error Blueprinter::Errors::InvalidBlueprint
end
end

0 comments on commit f60bb0c

Please sign in to comment.