Skip to content

Commit

Permalink
Quickly delete elements when deleting a page version
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed Apr 9, 2021
1 parent 8b915e6 commit 0b3483c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/alchemy/page_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def self.public_on(time = Time.current)
"OR #{table_name}.public_until >= :time)", time: time)
end

before_destroy :delete_elements

# Determines if this version is public
#
# Takes the two timestamps +public_on+ and +public_until+
Expand All @@ -42,5 +44,11 @@ def already_public_for?(time = Time.current)
def still_public_for?(time = Time.current)
public_until.nil? || public_until >= time
end

private

def delete_elements
DeleteElements.new(self.elements).call
end
end
end
21 changes: 21 additions & 0 deletions spec/models/alchemy/page_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,26 @@

it { is_expected.to be(false) }
end

describe "dependent element destruction" do
let!(:parent_element) { create(:alchemy_element, :with_nestable_elements, :with_contents) }
let!(:nested_element) { parent_element.nested_elements.first }
let!(:normal_element) { create(:alchemy_element, :with_contents) }

let(:page_version) { create(:alchemy_page_version) }

before do
Alchemy::Element.update_all(page_version_id: page_version.id)
end

it "deletes all elements along with the page version" do
page_version.destroy!
expect(Alchemy::Element.count).to be_zero
expect(Alchemy::Content.count).to be_zero
expect(Alchemy::EssenceText.count).to be_zero
expect(Alchemy::EssencePicture.count).to be_zero
expect(Alchemy::EssenceRichtext.count).to be_zero
end
end
end
end

0 comments on commit 0b3483c

Please sign in to comment.