diff --git a/app/models/alchemy/page/page_elements.rb b/app/models/alchemy/page/page_elements.rb index 48fbb41f99..2488916800 100644 --- a/app/models/alchemy/page/page_elements.rb +++ b/app/models/alchemy/page/page_elements.rb @@ -23,6 +23,10 @@ module Page::PageElements -> { order(:position).fixed.available }, class_name: 'Alchemy::Element', inverse_of: :page + has_many :dependent_destroyable_elements, + -> { not_nested }, + class_name: 'Alchemy::Element', + dependent: :destroy has_many :contents, through: :elements has_and_belongs_to_many :to_be_swept_elements, -> { distinct }, class_name: 'Alchemy::Element', diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index bb75746d1b..c1664ebe9d 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -338,6 +338,14 @@ module Alchemy expect(news_page.elements.pluck(:name)).to include('contactform') end end + + context 'destruction' do + let!(:page) { create(:alchemy_page, autogenerate_elements: true) } + + it 'destroys elements along with itself' do + expect { page.destroy! }.to change(Alchemy::Element, :count) + end + end end # ClassMethods (a-z)