Skip to content
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
11 changes: 10 additions & 1 deletion app/models/edit.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Edit < ApplicationRecord
belongs_to :leaf
delegated_type :leafable, types: Leafable::TYPES, dependent: :destroy
delegated_type :leafable, types: Leafable::TYPES

enum :action, %w[ revision trash ].index_by(&:itself)

after_destroy :destroy_superseded_leafable, if: :revision?

scope :sorted, -> { order(created_at: :desc) }
scope :before, ->(edit) { where("created_at < ?", edit.created_at) }
scope :after, ->(edit) { where("created_at > ?", edit.created_at) }
Expand All @@ -15,4 +17,11 @@ def previous
def next
leaf.edits.after(self).first
end

private
# A revision holds the leafable it superseded, which nothing else references. A trash
# edit shares the leaf's current leafable, and that one is the leaf's to destroy.
def destroy_superseded_leafable
leafable&.destroy
end
end
2 changes: 1 addition & 1 deletion app/models/leaf/editable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Leaf::Editable
MINIMUM_TIME_BETWEEN_VERSIONS = 10.minutes

included do
has_many :edits, dependent: :delete_all
has_many :edits, dependent: :destroy

after_update :record_moved_to_trash, if: :was_trashed?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_ext/action_text_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module ActionText::Markdown::Uploads
extend ActiveSupport::Concern

included do
has_many_attached :uploads, dependent: :destroy
has_many_attached :uploads
end
end

Expand Down
44 changes: 44 additions & 0 deletions test/models/book_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "test_helper"

class BookTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

test "slug is generated from title" do
book = Book.create!(title: "Hello, World!")
assert_equal "hello-world", book.slug
Expand Down Expand Up @@ -40,4 +42,46 @@ class BookTest < ActiveSupport::TestCase
test "markable returns empty string for book with no leaves" do
assert_equal "", books(:manual).markable
end

test "destroying a book removes superseded revisions along with their content and files" do
page = leaves(:welcome_page).page
page.body.uploads.attach io: file_fixture("pixel.bmp").open, filename: "pixel.bmp", content_type: "image/bmp"
upload_blob = page.body.uploads.blobs.sole
leaves(:welcome_page).edit leafable_params: { body: "Revised body" }

picture = leaves(:reading_picture).picture
image_blob = picture.image.blob
leaves(:reading_picture).edit leafable_params: { caption: "Revised caption" }

assert_equal page, leaves(:welcome_page).edits.revision.sole.leafable
assert_equal picture, leaves(:reading_picture).edits.revision.sole.leafable

perform_enqueued_jobs do
books(:handbook).destroy
end

assert_not Edit.exists?(leaf_id: leaves(:welcome_page, :reading_picture).map(&:id))
assert_not Page.exists?(page.id)
assert_not ActionText::Markdown.exists?(record: page)
assert_not Picture.exists?(picture.id)
assert_not ActiveStorage::Attachment.exists?(blob: [ upload_blob, image_blob ])
assert_not ActiveStorage::Blob.exists?(upload_blob.id)
assert_not ActiveStorage::Blob.exists?(image_blob.id)
assert_not ActiveStorage::Blob.service.exist?(upload_blob.key)
assert_not ActiveStorage::Blob.service.exist?(image_blob.key)
end

test "destroying a book removes the content a trashed leaf shares with its trash edit" do
page = leaves(:welcome_page).page
leaves(:welcome_page).trashed!

assert_equal page, leaves(:welcome_page).edits.trash.sole.leafable

books(:handbook).destroy

assert_not Leaf.exists?(leaves(:welcome_page).id)
assert_not Edit.exists?(leaf_id: leaves(:welcome_page).id)
assert_not Page.exists?(page.id)
assert_not ActionText::Markdown.exists?(record: page)
end
end
Loading