Class: Saviour::LifeCycle

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

Defined Under Namespace

Classes: FileCreator, FileUpdater

Constant Summary collapse

SHOULD_USE_INTERLOCK =
defined?(Rails) && !Rails.env.test?

Instance Method Summary collapse

Constructor Details

#initialize(model, persistence_klass) ⇒ LifeCycle

Returns a new instance of LifeCycle.

Raises:



80
81
82
83
84
85
# File 'lib/saviour/life_cycle.rb', line 80

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



103
104
105
# File 'lib/saviour/life_cycle.rb', line 103

def create!
  process_upload(FileCreator)
end

#delete!Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/saviour/life_cycle.rb', line 87

def delete!
  DbHelpers.run_after_commit do
    pool = Concurrent::Throttle.new Saviour::Config.concurrent_workers

    futures = attached_files.map do |column|
      pool.future(@model.send(column)) do |file|
        path = file.persisted_path
        file.uploader.storage.delete(path) if path
        file.delete
      end
    end

    futures.each(&:value!)
  end
end

#update!Object



107
108
109
# File 'lib/saviour/life_cycle.rb', line 107

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