Class: Saviour::LifeCycle
- Inherits:
-
Object
- Object
- Saviour::LifeCycle
show all
- Defined in:
- lib/saviour/life_cycle.rb
Defined Under Namespace
Classes: FileCreator, FileUpdater
Constant Summary
collapse
- SHOULD_USE_INTERLOCK =
defined?(Rails)
Instance Method Summary
collapse
Constructor Details
#initialize(model, persistence_klass) ⇒ LifeCycle
Returns a new instance of LifeCycle.
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
105
106
107
|
# File 'lib/saviour/life_cycle.rb', line 105
def create!
process_upload(FileCreator)
end
|
#delete! ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# 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
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
futures.each(&:value!)
end
end
end
|
#update! ⇒ Object
109
110
111
|
# File 'lib/saviour/life_cycle.rb', line 109
def update!
process_upload(FileUpdater, touch: true)
end
|