Class: Bulky::Updater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, bulk_update_id) ⇒ Updater

Returns a new instance of Updater.



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

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

Instance Attribute Details

#bulk_updateObject (readonly)

Returns the value of attribute bulk_update.



4
5
6
# File 'lib/bulky/updater.rb', line 4

def bulk_update
  @bulk_update
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/bulky/updater.rb', line 4

def model
  @model
end

Instance Method Details

#logObject



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

def log
  @log ||= bulk_update.updated_records.build { |r| r.updatable = model }
end

#strong_updatesObject



15
16
17
# File 'lib/bulky/updater.rb', line 15

def strong_updates
  ActionController::Parameters.new(bulk_update.updates)
end

#update!Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bulky/updater.rb', line 23

def update!
  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

#updatesObject



19
20
21
# File 'lib/bulky/updater.rb', line 19

def updates
  @updates ||= strong_updates.permit(*model.bulky_attributes)
end