2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
class Sequencer
|
|
|
|
class Unit
|
|
|
|
module Import
|
|
|
|
module Common
|
|
|
|
module Model
|
|
|
|
module Associations
|
|
|
|
class Extract < Sequencer::Unit::Base
|
2018-04-26 07:07:30 +00:00
|
|
|
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
|
|
|
|
|
|
|
|
skip_any_action
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
uses :model_class, :mapped
|
|
|
|
provides :associations
|
|
|
|
|
|
|
|
def process
|
|
|
|
state.provide(:associations) do
|
2021-06-24 07:05:39 +00:00
|
|
|
associations.filter_map do |association|
|
2017-08-30 15:12:28 +00:00
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "Checking association '#{association}'" }
|
2017-08-14 11:56:23 +00:00
|
|
|
next if !mapped.key?(association)
|
|
|
|
|
|
|
|
# remove from the mapped values if it's an association
|
|
|
|
value = mapped.delete(association)
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "Extracted association '#{association}' value '#{value.inspect}'" }
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
# skip if we don't track them
|
|
|
|
next if tracked_associations.exclude?(association)
|
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "Using value of association '#{association}'" }
|
2017-08-14 11:56:23 +00:00
|
|
|
[association, value]
|
2021-06-24 07:05:39 +00:00
|
|
|
end.to_h
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def associations
|
|
|
|
@associations ||= begin
|
|
|
|
# loop over all reflections
|
2017-08-30 15:12:28 +00:00
|
|
|
model_class.reflect_on_all_associations.each_with_object([]) do |reflection, associations|
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
# refection name is something like groups or organization (singular/plural)
|
|
|
|
associations.push(reflection.name)
|
|
|
|
|
|
|
|
# key is something like group_id or organization_id (singular)
|
|
|
|
key = reflection.klass.name.foreign_key
|
|
|
|
|
|
|
|
# add trailing 's' to get pluralized key
|
|
|
|
reflection_name = reflection.name.to_s
|
2017-08-30 15:12:28 +00:00
|
|
|
if reflection_name.singularize != reflection_name
|
2017-08-14 11:56:23 +00:00
|
|
|
key = "#{key}s"
|
|
|
|
end
|
|
|
|
|
|
|
|
# store _id/_ids name
|
|
|
|
associations.push(key.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tracked_associations
|
|
|
|
# track all associations by default
|
|
|
|
associations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|