Skip to content

Commit

Permalink
Nullify referencing EssencePages on page deletion
Browse files Browse the repository at this point in the history
It's impossible to delete a page if it is referenced from an
EssencePage. Currently, we get a ForeignKeyConstraint error if we intend
to do this; but it would be much nicer if the essence would stay where
it is and stop referencing the page.
  • Loading branch information
mamhoff committed Apr 10, 2024
1 parent e4cbef5 commit 35e0a3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class Page < BaseRecord
has_one :draft_version, -> { drafts }, class_name: "Alchemy::PageVersion"
has_one :public_version, -> { published }, class_name: "Alchemy::PageVersion", autosave: -> { persisted? }

has_many :page_essences, class_name: "Alchemy::EssencePage", foreign_key: :page_id, inverse_of: :ingredient_association, dependent: :nullify

before_validation :set_language,
if: -> { language.nil? }

Expand Down
8 changes: 8 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ module Alchemy
it "destroys elements along with itself" do
expect { page.destroy! }.to change(Alchemy::Element, :count).from(3).to(0)
end

context "with an essence page pointing to the page" do
let!(:essence_page) { create(:alchemy_essence_page, page: page) }

it "nullifies the essence on destruction" do
expect { page.destroy! }.to change { essence_page.reload.page_id }.from(page.id).to(nil)
end
end
end
end

Expand Down

0 comments on commit 35e0a3c

Please sign in to comment.