39 lines
1.0 KiB
Ruby
39 lines
1.0 KiB
Ruby
module RecordsUploads
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
after :store, :record_upload
|
|
before :remove, :destroy_upload
|
|
end
|
|
|
|
private
|
|
|
|
# After storing an attachment, create a corresponding Upload record
|
|
#
|
|
# NOTE: We're ignoring the argument passed to this callback because we want
|
|
# the `SanitizedFile` object from `CarrierWave::Uploader::Base#file`, not the
|
|
# `Tempfile` object the callback gets.
|
|
#
|
|
# Called `after :store`
|
|
def record_upload(_tempfile)
|
|
return unless file_storage?
|
|
return unless file.exists?
|
|
|
|
Upload.record(self)
|
|
end
|
|
|
|
# When removing an attachment, destroy any Upload records at the same path
|
|
#
|
|
# Note: this _will not work_ for Uploaders which relativize paths, such as
|
|
# `FileUploader`, but because that uploader uses different paths for every
|
|
# upload, that's an acceptable caveat.
|
|
#
|
|
# Called `before :remove`
|
|
def destroy_upload(*args)
|
|
return unless file_storage?
|
|
return unless file
|
|
|
|
Upload.remove_path(relative_path)
|
|
end
|
|
end
|