Class: Bulky::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/bulky/updater.rb

Constant Summary collapse

QUEUE =
@queue = :bulky_updates

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, bulk_update_id) ⇒ Updater

Returns a new instance of Updater.



11
12
13
14
15
# File 'lib/bulky/updater.rb', line 11

def initialize(model, bulk_update_id)
  @bulk_update = Bulky::BulkUpdate.find(bulk_update_id)
  @model       = model
  @updates     = @bulk_update.updates
end

Class Method Details

.perform(model_name, update_id, bulk_update_id) ⇒ Object



6
7
8
9
# File 'lib/bulky/updater.rb', line 6

def self.perform(model_name, update_id, bulk_update_id)
  model = model_name.constantize.find(update_id)
  new(model, bulk_update_id).update!
end

Instance Method Details

#update!Object



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

def update!
  @model.tap do
    @log = @bulk_update.updated_records.build { |r| r.updatable = @model }

    begin
      @model.attributes = @updates
      @log.updatable_changes = @model.changes
      @model.save!
    rescue => e
      @log.error_message   = e.message
      @log.error_backtrace = e.backtrace.join("\n")
      raise e
    ensure
      @log.save!
    end
  end
end