2017-08-14 11:56:23 +00:00
|
|
|
class Sequencer
|
|
|
|
class Unit
|
|
|
|
module Import
|
|
|
|
module Common
|
|
|
|
module Model
|
|
|
|
class Update < Sequencer::Unit::Base
|
|
|
|
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
|
2017-12-21 11:19:30 +00:00
|
|
|
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
|
2017-09-01 12:02:12 +00:00
|
|
|
|
2017-12-21 11:19:30 +00:00
|
|
|
skip_any_action
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
uses :instance, :mapped
|
2017-12-21 11:19:30 +00:00
|
|
|
provides :action
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
def process
|
|
|
|
# check if no instance is given - so we can't update it
|
|
|
|
return if !instance
|
|
|
|
|
|
|
|
# lock the current instance for write access
|
|
|
|
instance.with_lock do
|
|
|
|
# delete since we have an update and
|
|
|
|
# the record is already created
|
|
|
|
mapped.delete(:created_by_id)
|
|
|
|
|
|
|
|
# assign regular attributes
|
|
|
|
instance.assign_attributes(mapped)
|
|
|
|
|
|
|
|
action = changed? ? :updated : :unchanged
|
2017-12-21 11:19:30 +00:00
|
|
|
state.provide(:action, action)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
handle_failure(e)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def changed?
|
|
|
|
logger.debug("Changed instance attributes: #{changes.inspect}")
|
|
|
|
changes.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def changes
|
|
|
|
@changes ||= begin
|
2017-09-23 06:25:55 +00:00
|
|
|
if instance.has_changes_to_save?
|
2017-08-14 11:56:23 +00:00
|
|
|
# dry run
|
2017-09-23 06:25:55 +00:00
|
|
|
instance.changes_to_save
|
2017-08-14 11:56:23 +00:00
|
|
|
else
|
|
|
|
# live run
|
|
|
|
instance.previous_changes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|