Class: Saviour::LifeCycle

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/life_cycle.rb

Defined Under Namespace

Classes: FileCreator, FileUpdater

Instance Method Summary collapse

Constructor Details

#initialize(model, persistence_klass) ⇒ LifeCycle

Returns a new instance of LifeCycle.

Raises:



78
79
80
81
82
83
# File 'lib/saviour/life_cycle.rb', line 78

def initialize(model, persistence_klass)
  raise ConfigurationError, "Please provide an object compatible with Saviour." unless model.class.respond_to?(:attached_files)

  @persistence_klass = persistence_klass
  @model = model
end

Instance Method Details

#create!Object



110
111
112
# File 'lib/saviour/life_cycle.rb', line 110

def create!
  process_upload(FileCreator)
end

#delete!Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/saviour/life_cycle.rb', line 85

def delete!
  DbHelpers.run_after_commit do
    pool = Concurrent::FixedThreadPool.new(Saviour::Config.concurrent_workers)

    futures = attached_files.map do |column|
      Concurrent::Future.execute(executor: pool) {
        file = @model.send(column)
        path = file.persisted_path
        file.uploader.storage.delete(path) if path
        file.delete
      }
    end

    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      futures.each do |future|
        future.value
        raise(future.reason) if future.rejected?
      end
    end

    pool.shutdown
    pool.wait_for_termination
  end
end

#update!Object



114
115
116
# File 'lib/saviour/life_cycle.rb', line 114

def update!
  process_upload(FileUpdater, touch: true)
end