Class: Saviour::LifeCycle::FileUpdater
- Inherits:
-
Object
- Object
- Saviour::LifeCycle::FileUpdater
- Defined in:
- lib/saviour/life_cycle.rb
Instance Method Summary collapse
-
#initialize(current_path, file, column, connection) ⇒ FileUpdater
constructor
A new instance of FileUpdater.
- #upload ⇒ Object
- #uploader ⇒ Object
Constructor Details
#initialize(current_path, file, column, connection) ⇒ FileUpdater
31 32 33 34 35 36 |
# File 'lib/saviour/life_cycle.rb', line 31 def initialize(current_path, file, column, connection) @file = file @column = column @current_path = current_path @connection = connection end |
Instance Method Details
#upload ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/saviour/life_cycle.rb', line 38 def upload dup_temp_path = SecureRandom.hex dup_file = proc do uploader.storage.cp @current_path, dup_temp_path DbHelpers.run_after_commit(@connection) do uploader.storage.delete dup_temp_path end DbHelpers.run_after_rollback(@connection) do uploader.storage.mv dup_temp_path, @current_path end end @new_path = @file.write( before_write: ->(path) { dup_file.call if @current_path == path } ) return unless @new_path if @current_path && @current_path != @new_path DbHelpers.run_after_commit(@connection) do uploader.storage.delete(@current_path) end end # Delete the newly uploaded file only if it's an update in a different path if @current_path.nil? || @current_path != @new_path DbHelpers.run_after_rollback(@connection) do uploader.storage.delete(@new_path) end end [@column, @new_path] end |
#uploader ⇒ Object
75 76 77 |
# File 'lib/saviour/life_cycle.rb', line 75 def uploader @file.uploader end |