Class: Saviour::LifeCycle

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

Instance Method Summary collapse

Constructor Details

#initialize(model, persistence_klass) ⇒ LifeCycle

Returns a new instance of LifeCycle.

Raises:



3
4
5
6
7
8
# File 'lib/saviour/life_cycle.rb', line 3

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/saviour/life_cycle.rb', line 18

def create!
  attached_files.each do |column|
    next unless @model.send(column).changed?

    persistence_layer = @persistence_klass.new(@model)
    new_path = @model.send(column).write

    if new_path
      persistence_layer.write(column, new_path)

      DbHelpers.run_after_rollback do
        Config.storage.delete(new_path)
      end
    end
  end
end

#delete!Object



10
11
12
13
14
15
16
# File 'lib/saviour/life_cycle.rb', line 10

def delete!
  DbHelpers.run_after_commit do
    attached_files.each do |column|
      @model.send(column).delete
    end
  end
end

#update!Object



35
36
37
38
39
40
41
# File 'lib/saviour/life_cycle.rb', line 35

def update!
  attached_files.each do |column|
    next unless @model.send(column).changed?

    update_file(column)
  end
end