2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-02 16:21:42 +00:00
|
|
|
# Mounted uploaders are destroyed by carrierwave's after_commit
|
|
|
|
# hook. This hook fetches upload location (local vs remote) from
|
2018-10-30 10:53:01 +00:00
|
|
|
# Upload model. So it's necessary to make sure that during that
|
2018-05-02 16:21:42 +00:00
|
|
|
# after_commit hook model's associated uploads are not deleted yet.
|
2018-05-09 19:43:23 +00:00
|
|
|
# IOW we can not use dependent: :destroy :
|
2018-05-02 16:21:42 +00:00
|
|
|
# has_many :uploads, as: :model, dependent: :destroy
|
|
|
|
#
|
|
|
|
# And because not-mounted uploads require presence of upload's
|
|
|
|
# object model when destroying them (FileUploader's `build_upload` method
|
|
|
|
# references `model` on delete), we can not use after_commit hook for these
|
|
|
|
# uploads.
|
|
|
|
#
|
|
|
|
# Instead FileUploads are destroyed in before_destroy hook and remaining uploads
|
|
|
|
# are destroyed by the carrierwave's after_commit hook.
|
|
|
|
|
|
|
|
module WithUploads
|
|
|
|
extend ActiveSupport::Concern
|
2018-10-07 18:31:08 +00:00
|
|
|
include FastDestroyAll::Helpers
|
2018-12-04 19:48:21 +00:00
|
|
|
include FeatureGate
|
2018-05-02 16:21:42 +00:00
|
|
|
|
|
|
|
# Currently there is no simple way how to select only not-mounted
|
|
|
|
# uploads, it should be all FileUploaders so we select them by
|
|
|
|
# `uploader` class
|
|
|
|
FILE_UPLOADERS = %w(PersonalFileUploader NamespaceFileUploader FileUploader).freeze
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :uploads, as: :model
|
2018-12-04 19:48:21 +00:00
|
|
|
has_many :file_uploads, -> { where(uploader: FILE_UPLOADERS) }, class_name: 'Upload', as: :model
|
|
|
|
|
|
|
|
# TODO: when feature flag is removed, we can use just dependent: destroy
|
|
|
|
# option on :file_uploads
|
|
|
|
before_destroy :remove_file_uploads
|
2018-05-02 16:21:42 +00:00
|
|
|
|
2018-10-07 18:31:08 +00:00
|
|
|
use_fast_destroy :file_uploads
|
2018-05-02 16:21:42 +00:00
|
|
|
end
|
2018-04-19 16:22:23 +00:00
|
|
|
|
2018-12-04 19:48:21 +00:00
|
|
|
def perform_fast_destroy(subject)
|
|
|
|
super if fast_destroy_enabled?
|
|
|
|
end
|
|
|
|
|
2018-04-19 16:22:23 +00:00
|
|
|
def retrieve_upload(_identifier, paths)
|
|
|
|
uploads.find_by(path: paths)
|
|
|
|
end
|
2018-12-04 19:48:21 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# mounted uploads are deleted in carrierwave's after_commit hook,
|
|
|
|
# but FileUploaders which are not mounted must be deleted explicitly and
|
|
|
|
# it can not be done in after_commit because FileUploader requires loads
|
|
|
|
# associated model on destroy (which is already deleted in after_commit)
|
|
|
|
def remove_file_uploads
|
|
|
|
fast_destroy_enabled? ? delete_uploads : destroy_uploads
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_uploads
|
|
|
|
file_uploads.delete_all(:delete_all)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_uploads
|
|
|
|
file_uploads.find_each do |upload|
|
|
|
|
upload.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fast_destroy_enabled?
|
|
|
|
Feature.enabled?(:fast_destroy_uploads, self)
|
|
|
|
end
|
2018-05-02 16:21:42 +00:00
|
|
|
end
|