trabajo-afectivo/lib/sequencer/unit/import/common/model/update.rb

57 lines
1.6 KiB
Ruby
Raw Normal View History

class Sequencer
class Unit
module Import
module Common
module Model
class Update < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnProvidedInstanceAction
uses :instance, :mapped
provides :instance_action
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
state.provide(:instance_action, action)
end
rescue => e
handle_failure(e)
end
private
def changed?
logger.debug("Changed instance attributes: #{changes.inspect}")
changes.present?
end
def changes
@changes ||= begin
if instance.changed?
# dry run
instance.changes
else
# live run
instance.previous_changes
end
end
end
end
end
end
end
end
end