1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Default to purging later when the owning record is destroyed

This commit is contained in:
David Heinemeier Hansson 2017-07-05 18:31:49 +02:00
parent c2fa570e2e
commit e3ade5fd2d
5 changed files with 56 additions and 6 deletions

View file

@ -1,15 +1,23 @@
module ActiveVault::Attached::Macros
def has_one_attached(name)
def has_one_attached(name, dependent: :purge_later)
define_method(name) do
instance_variable_get("@active_vault_attached_#{name}") ||
instance_variable_set("@active_vault_attached_#{name}", ActiveVault::Attached::One.new(name, self))
end
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
end
def has_many_attached(name)
def has_many_attached(name, dependent: :purge_later)
define_method(name) do
instance_variable_get("@active_vault_attached_#{name}") ||
instance_variable_set("@active_vault_attached_#{name}", ActiveVault::Attached::Many.new(name, self))
end
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
end
end

View file

@ -23,4 +23,8 @@ class ActiveVault::Attachment < ActiveRecord::Base
blob.purge
destroy
end
def purge_later
ActiveVault::PurgeJob.perform_later(self)
end
end

View file

@ -1,5 +1,6 @@
require "active_vault/site"
require "active_vault/filename"
require "active_vault/purge_job"
# Schema: id, key, filename, content_type, metadata, byte_size, checksum, created_at
class ActiveVault::Blob < ActiveRecord::Base

View file

@ -1,7 +1,10 @@
class ActiveVault::PurgeJob < ActiveJob::Base
retry_on ActiveVault::StorageException
require "active_job"
def perform(blob)
blob.purge
class ActiveVault::PurgeJob < ActiveJob::Base
# FIXME: Limit this to a custom ActiveVault error
retry_on StandardError
def perform(attachment_or_blob)
attachment_or_blob.purge
end
end

View file

@ -2,6 +2,10 @@ require "test_helper"
require "database/setup"
require "active_vault/blob"
require "active_job"
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.logger = nil
# ActiveRecord::Base.logger = Logger.new(STDOUT)
class User < ActiveRecord::Base
@ -10,6 +14,8 @@ class User < ActiveRecord::Base
end
class ActiveVault::AttachmentsTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
setup { @user = User.create!(name: "DHH") }
teardown { ActiveVault::Blob.all.each(&:purge) }
@ -33,6 +39,19 @@ class ActiveVault::AttachmentsTest < ActiveSupport::TestCase
assert_not ActiveVault::Blob.site.exist?(avatar_key)
end
test "purge attached blob later when the record is destroyed" do
@user.avatar.attach create_blob(filename: "funky.jpg")
avatar_key = @user.avatar.key
perform_enqueued_jobs do
@user.destroy
assert_nil ActiveVault::Blob.find_by(key: avatar_key)
assert_not ActiveVault::Blob.site.exist?(avatar_key)
end
end
test "attach existing blobs" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
@ -58,4 +77,19 @@ class ActiveVault::AttachmentsTest < ActiveSupport::TestCase
assert_not ActiveVault::Blob.site.exist?(highlight_keys.first)
assert_not ActiveVault::Blob.site.exist?(highlight_keys.second)
end
test "purge attached blobs later when the record is destroyed" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
highlight_keys = @user.highlights.collect(&:key)
perform_enqueued_jobs do
@user.destroy
assert_nil ActiveVault::Blob.find_by(key: highlight_keys.first)
assert_not ActiveVault::Blob.site.exist?(highlight_keys.first)
assert_nil ActiveVault::Blob.find_by(key: highlight_keys.second)
assert_not ActiveVault::Blob.site.exist?(highlight_keys.second)
end
end
end