module GlobalRegistry::Bindings::Model::PushRelationship

Public Instance Methods

delete_relationships_from_global_registry_async(*types) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 25
def delete_relationships_from_global_registry_async(*types)
  types = types.empty? ? self.class.global_registry_relationship_types : types
  types.each do |type|
    next unless global_registry_relationship(type).id_value?
    next if global_registry_relationship(type).condition?(:if)
    next unless global_registry_relationship(type).condition?(:unless)
    ::GlobalRegistry::Bindings::Workers::DeleteEntityWorker.perform_async(
      global_registry_relationship(type).id_value
    )
  end
end
push_relationships_to_global_registry_async(*types) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 17
def push_relationships_to_global_registry_async(*types)
  types = types.empty? ? self.class.global_registry_relationship_types : types
  types.each do |type|
    action = global_registry_relationship_change_action(type)
    send("global_registry_relationship_async_#{action}".to_sym, type)
  end
end

Protected Instance Methods

global_registry_relationship_async_delete(type) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 58
def global_registry_relationship_async_delete(type)
  return if global_registry_relationship(type).condition?(:if)
  return unless global_registry_relationship(type).condition?(:unless)
  delete_relationships_from_global_registry_async(type)
  update_column( # rubocop:disable Rails/SkipsModelValidations
    global_registry_relationship(type).id_column, nil
  )
end
global_registry_relationship_async_ignore(_type) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 67
def global_registry_relationship_async_ignore(_type); end
global_registry_relationship_async_push(type) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 39
def global_registry_relationship_async_push(type)
  return if global_registry_relationship(type).condition?(:if)
  return unless global_registry_relationship(type).condition?(:unless)
  ::GlobalRegistry::Bindings::Workers::PushRelationshipWorker.perform_async(self.class, id, type)
end
global_registry_relationship_async_replace(type) click to toggle source
# File lib/global_registry_bindings/model/push_relationship.rb, line 45
def global_registry_relationship_async_replace(type)
  return if global_registry_relationship(type).condition?(:if)
  return unless global_registry_relationship(type).condition?(:unless)
  # Replace deletes GR relationship immediately before scheduling an async update
  ::GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(
    global_registry_relationship(type).id_value
  )
  update_column( # rubocop:disable Rails/SkipsModelValidations
    global_registry_relationship(type).id_column, nil
  )
  ::GlobalRegistry::Bindings::Workers::PushRelationshipWorker.perform_async(self.class, id, type)
end
global_registry_relationship_change_action(type) click to toggle source

rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/AbcSize

# File lib/global_registry_bindings/model/push_relationship.rb, line 71
def global_registry_relationship_change_action(type)
  [global_registry_relationship(type).primary_foreign_key,
   global_registry_relationship(type).related_foreign_key].each do |key|
    if previous_changes.key?(key)
      # Delete if changed from anything to nil
      return :delete if previous_changes[key].last.nil?
      # Replace if value changed
      return :replace if previous_changes[key].first != previous_changes[key].last &&
                         !previous_changes[key].first.nil?
    elsif key.present? && send(key).nil?
      # Ignore if value didn't change and foreign_key is nil
      return :ignore
    end
  end
  # otherwise Create/Update
  :push
end