Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2017-02-15 21:11:44 +0300
committerRobert Speicher <rspeicher@gmail.com>2017-03-06 22:41:09 +0300
commit3a0be1c5fca6b80c75f728f7751b7c7614ab1bc0 (patch)
tree783932a4881ab39a4bd69adab15e7fa0376b2334 /app/uploaders/records_uploads.rb
parent4c622b71fd284058deee483bf0009f8179b792bc (diff)
Add `RecordsUploads` module to record Upload records via callbacks
Diffstat (limited to 'app/uploaders/records_uploads.rb')
-rw-r--r--app/uploaders/records_uploads.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/app/uploaders/records_uploads.rb b/app/uploaders/records_uploads.rb
new file mode 100644
index 00000000000..7a0424b5adf
--- /dev/null
+++ b/app/uploaders/records_uploads.rb
@@ -0,0 +1,38 @@
+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