Skip to content

Commit

Permalink
Add dependent: :destroy to Page#elements relations
Browse files Browse the repository at this point in the history
When I delete a page, I do expect its elements to be gone afterwards,
too.
  • Loading branch information
mamhoff committed Nov 26, 2019
1 parent 0bb88bf commit 903ae58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
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 @@ -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)
Expand Down

0 comments on commit 903ae58

Please sign in to comment.