From eaf097c2627736bb6c9f37949f8619ba0489bbd8 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 8 Jan 2018 16:26:15 +0100 Subject: [PATCH] Refactoring: Splitted ExternalSync and Lookup namespaces. --- .../import/exchange/folder_contact.rb | 4 +- .../common/model/external_sync/create.rb | 29 --------- .../common/model/external_sync/integrity.rb | 51 +++++++++++++++ .../common/model/external_sync/local.rb | 63 ------------------- .../import/common/model/lookup/attributes.rb | 60 ++++++++++++++++++ .../lookup.rb => lookup/external_sync.rb} | 8 ++- .../external_sync_spec.rb} | 2 +- 7 files changed, 119 insertions(+), 98 deletions(-) delete mode 100644 lib/sequencer/unit/import/common/model/external_sync/create.rb create mode 100644 lib/sequencer/unit/import/common/model/external_sync/integrity.rb delete mode 100644 lib/sequencer/unit/import/common/model/external_sync/local.rb create mode 100644 lib/sequencer/unit/import/common/model/lookup/attributes.rb rename lib/sequencer/unit/import/common/model/{external_sync/lookup.rb => lookup/external_sync.rb} (85%) rename spec/lib/sequencer/unit/import/common/model/{external_sync/lookup_spec.rb => lookup/external_sync_spec.rb} (85%) diff --git a/lib/sequencer/sequence/import/exchange/folder_contact.rb b/lib/sequencer/sequence/import/exchange/folder_contact.rb index d475920ca..77f004cf8 100644 --- a/lib/sequencer/sequence/import/exchange/folder_contact.rb +++ b/lib/sequencer/sequence/import/exchange/folder_contact.rb @@ -10,7 +10,7 @@ class Sequencer 'Import::Exchange::FolderContact::Mapping', 'Import::Common::Model::Skip::Blank::Mapped', 'Import::Exchange::FolderContact::StaticAttributes', - 'Import::Common::Model::ExternalSync::Lookup', + 'Import::Common::Model::Lookup::ExternalSync', 'Import::Common::Model::Associations::Extract', 'Import::Common::User::Attributes::Downcase', 'Import::Common::User::Email::CheckValidity', @@ -19,7 +19,7 @@ class Sequencer 'Import::Common::Model::Create', 'Import::Common::Model::Associations::Assign', 'Import::Common::Model::Save', - 'Import::Common::Model::ExternalSync::Create', + 'Import::Common::Model::ExternalSync::Integrity', 'Import::Exchange::FolderContact::HttpLog', 'Import::Exchange::FolderContact::Statistics::Diff', 'Import::Common::ImportJob::Statistics::Update', diff --git a/lib/sequencer/unit/import/common/model/external_sync/create.rb b/lib/sequencer/unit/import/common/model/external_sync/create.rb deleted file mode 100644 index b1354a454..000000000 --- a/lib/sequencer/unit/import/common/model/external_sync/create.rb +++ /dev/null @@ -1,29 +0,0 @@ -class Sequencer - class Unit - module Import - module Common - module Model - module ExternalSync - class Create < Sequencer::Unit::Base - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance - - uses :instance, :instance_action, :remote_id, :dry_run, :external_sync_source, :model_class - - def process - return if dry_run - return if instance_action != :created - - ::ExternalSync.create( - source: external_sync_source, - source_id: remote_id, - object: model_class.name, - o_id: instance.id - ) - end - end - end - end - end - end - end -end diff --git a/lib/sequencer/unit/import/common/model/external_sync/integrity.rb b/lib/sequencer/unit/import/common/model/external_sync/integrity.rb new file mode 100644 index 000000000..ffc7535b7 --- /dev/null +++ b/lib/sequencer/unit/import/common/model/external_sync/integrity.rb @@ -0,0 +1,51 @@ +class Sequencer + class Unit + module Import + module Common + module Model + module ExternalSync + class Integrity < Sequencer::Unit::Base + uses :instance, :remote_id, :dry_run, :external_sync_source, :model_class + + def process + return if dry_run + return if instance.blank? + return if instance.id.blank? + return if up_to_date? + create + end + + private + + def up_to_date? + return false if entry.blank? + return true if entry.source_id == remote_id + entry.update!(source_id: remote_id) + true + end + + def entry + @entry ||= begin + ::ExternalSync.find_by( + source: external_sync_source, + object: model_class.name, + o_id: instance.id + ) + end + end + + def create + ::ExternalSync.create( + source: external_sync_source, + source_id: remote_id, + object: model_class.name, + o_id: instance.id + ) + end + end + end + end + end + end + end +end diff --git a/lib/sequencer/unit/import/common/model/external_sync/local.rb b/lib/sequencer/unit/import/common/model/external_sync/local.rb deleted file mode 100644 index 5d4c6f1f8..000000000 --- a/lib/sequencer/unit/import/common/model/external_sync/local.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'sequencer/unit/import/common/model/mixin/handle_failure' - -class Sequencer - class Unit - module Import - module Common - module Model - module ExternalSync - class Local < Sequencer::Unit::Base - include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance - - uses :mapped, :remote_id, :model_class, :external_sync_source, :instance_action - provides :instance - - def process - return if state.provided?(:instance) - - return if value.blank? - return if instance.blank? - - create_external_sync - - state.provide(:instance, instance) - end - - private - - def attribute - raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'" - end - - def value - mapped[attribute] - end - - def instance - @instance ||= begin - model_class.where(attribute => value).find do |local| - !ExternalSync.exists?( - source: external_sync_source, - object: model_class.name, - o_id: local.id - ) - end - end - end - - def create_external_sync - ExternalSync.create( - source: external_sync_source, - source_id: remote_id, - object: import_class.name, - o_id: instance.id - ) - end - end - end - end - end - end - end -end diff --git a/lib/sequencer/unit/import/common/model/lookup/attributes.rb b/lib/sequencer/unit/import/common/model/lookup/attributes.rb new file mode 100644 index 000000000..f645929c1 --- /dev/null +++ b/lib/sequencer/unit/import/common/model/lookup/attributes.rb @@ -0,0 +1,60 @@ +class Sequencer + class Unit + module Import + module Common + module Model + module Lookup + class Attributes < Sequencer::Unit::Base + include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + + skip_instance_action :skipped + + uses :mapped, :model_class + provides :instance + + def process + return if state.provided?(:instance) + return if existing_instance.blank? + + state.provide(:instance, existing_instance) + end + + private + + def attribute + raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'" + end + alias attributes attribute + + def existing_instance + @existing_instance ||= begin + Array(attributes).find do |attribute| + + value = mapped[attribute] + next if value.blank? + + existing_instance = lookup( + attribute: attribute, + value: value + ) + + next if existing_instance.blank? + + # https://stackoverflow.com/a/24901650/7900866 + break existing_instance + end + end + end + + def lookup(attribute:, value:) + return model_class.identify(value) if model_class.respond_to?(:identify) + model_class.find_by(attribute => value) + end + end + end + end + end + end + end +end diff --git a/lib/sequencer/unit/import/common/model/external_sync/lookup.rb b/lib/sequencer/unit/import/common/model/lookup/external_sync.rb similarity index 85% rename from lib/sequencer/unit/import/common/model/external_sync/lookup.rb rename to lib/sequencer/unit/import/common/model/lookup/external_sync.rb index e9894d794..995094a60 100644 --- a/lib/sequencer/unit/import/common/model/external_sync/lookup.rb +++ b/lib/sequencer/unit/import/common/model/lookup/external_sync.rb @@ -3,10 +3,12 @@ class Sequencer module Import module Common module Model - module ExternalSync - class Lookup < Sequencer::Unit::Base + module Lookup + class ExternalSync < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + + skip_instance_action :skipped uses :remote_id, :model_class, :external_sync_source provides :instance diff --git a/spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb b/spec/lib/sequencer/unit/import/common/model/lookup/external_sync_spec.rb similarity index 85% rename from spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb rename to spec/lib/sequencer/unit/import/common/model/lookup/external_sync_spec.rb index 58c33b2bd..087ee215b 100644 --- a/spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb +++ b/spec/lib/sequencer/unit/import/common/model/lookup/external_sync_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe Sequencer::Unit::Import::Common::Model::ExternalSync::Lookup, sequencer: :unit do +RSpec.describe Sequencer::Unit::Import::Common::Model::Lookup::ExternalSync, sequencer: :unit do it 'finds model_class instances by remote_id' do user = create(:user)