Skip to content

Inherit views (V2) #511

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

Open
wants to merge 5 commits into
base: release-2.0
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Don't allow redefining the default view
Signed-off-by: Jordan Hollinger <[email protected]>
  • Loading branch information
jhollinger committed Apr 29, 2025
commit c949f620305050ebca89cec8fefd5790e427ef8a
5 changes: 4 additions & 1 deletion lib/blueprinter/v2/view_builder.rb
Original file line number Diff line number Diff line change
@@ -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

#
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
@@ -60,4 +60,17 @@
keys = builder.each.map { |name, _| name }
expect(keys.sort).to eq %i(default foo bar).sort
end

it "doesn't 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 "throws 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