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

60 lines
1.8 KiB
Ruby
Raw Normal View History

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
uses :instance, :associations, :action, :dry_run
provides :action
def process
return if dry_run
return if instance.blank?
instance.assign_attributes(associations)
# execute associations check only if needed for performance reasons
return if action != :unchanged
return if !changed?
state.provide(:action, :changed)
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)
value
end
end
end
end
end
end
end
end