Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickly delete elements when deleting a page version #2064

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(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