2017-08-14 11:56:23 +00:00
|
|
|
require 'sequencer/unit/import/common/model/mixin/handle_failure'
|
|
|
|
|
|
|
|
class Sequencer
|
|
|
|
class Unit
|
|
|
|
module Import
|
|
|
|
module Common
|
|
|
|
module Model
|
|
|
|
module Associations
|
|
|
|
class Assign < Sequencer::Unit::Base
|
|
|
|
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
|
|
|
|
|
2017-12-21 11:19:30 +00:00
|
|
|
uses :instance, :associations, :action, :dry_run
|
|
|
|
provides :action
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
def process
|
|
|
|
return if dry_run
|
|
|
|
return if instance.blank?
|
|
|
|
|
|
|
|
instance.assign_attributes(associations)
|
|
|
|
|
|
|
|
# execute associations check only if needed for performance reasons
|
2017-12-21 11:19:30 +00:00
|
|
|
return if action != :unchanged
|
2017-08-14 11:56:23 +00:00
|
|
|
return if !changed?
|
2017-12-21 11:19:30 +00:00
|
|
|
state.provide(:action, :changed)
|
2017-08-14 11:56:23 +00:00
|
|
|
rescue => e
|
|
|
|
handle_failure(e)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def changed?
|
|
|
|
logger.debug("Changed instance associations: #{changes.inspect}")
|
|
|
|
changes.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def changes
|
|
|
|
@changes ||= begin
|
|
|
|
return {} if associations.blank?
|
|
|
|
associations.collect do |association, value|
|
|
|
|
before = compareable(instance.send(association))
|
|
|
|
after = compareable(value)
|
|
|
|
next if before == after
|
|
|
|
[association, [before, after]]
|
|
|
|
end.compact.to_h.with_indifferent_access
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compareable(value)
|
|
|
|
return nil if value.blank?
|
2017-08-30 15:10:55 +00:00
|
|
|
return value.sort if value.respond_to?(:sort)
|
2017-08-14 11:56:23 +00:00
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|