Skip to content

Commit

Permalink
Nullify Ingredients::Page on Page destroy (#2831)
Browse files Browse the repository at this point in the history
When we delete a page, page ingredients that point to that page should
be nullified.

(cherry picked from commit d183256)

Co-authored-by: Martin Meyerhoff <[email protected]>
  • Loading branch information
alchemycms-bot and mamhoff authored Apr 11, 2024
1 parent f8153a3 commit b479806
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_ingredients, class_name: "Alchemy::Ingredients::Page", foreign_key: :related_object_id, 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 @@ -224,6 +224,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 a page ingredient pointing to the page" do
let!(:ingredient) { create(:alchemy_ingredient_page, page: page) }

it "nullifies the foreign key on the ingredient" do
expect { page.destroy! }.to change { ingredient.reload.related_object_id }.from(page.id).to(nil)
end
end
end
end

Expand Down

0 comments on commit b479806

Please sign in to comment.