Module: GlobalRegistry::Bindings::Entity::RelationshipTypeMethods

Extended by:
ActiveSupport::Concern
Included in:
Workers::PushRelationshipWorker
Defined in:
lib/global_registry_bindings/entity/relationship_type_methods.rb

Instance Method Summary collapse

Instance Method Details

#primary_associated_entity_type_idObject

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 44

def primary_associated_entity_type_id
  primary_worker =
    GlobalRegistry::Bindings::Workers::PushEntityWorker.new global_registry_relationship(type).primary
  entity_type = primary_worker.send(:push_entity_type_to_global_registry)
  unless entity_type
    primary_type = global_registry_relationship(type).primary_type
    entity_type = GlobalRegistry::EntityType.get(
      "filters[name]" => primary_type
    )["entity_types"]&.first
  end
  entity_type&.dig("id")
end

#push_global_registry_relationship_typeObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 13

def push_global_registry_relationship_type
  return unless global_registry_relationship(type).ensure_type?
  primary_entity_type_id = primary_associated_entity_type_id
  related_entity_type_id = related_associated_entity_type_id

  relationship_type = Rails.cache.fetch(relationship_type_cache_key, expires_in: 1.hour) do
    rel = GlobalRegistry::RelationshipType.get(
      "filters[between]" => "#{primary_entity_type_id},#{related_entity_type_id}"
    )["relationship_types"].detect do |r|
      r["relationship1"]["relationship_name"] ==
        global_registry_relationship(type).primary_name.to_s
    end
    unless rel
      rel = GlobalRegistry::RelationshipType.post(
        relationship_type: {
          entity_type1_id: primary_entity_type_id,
          entity_type2_id: related_entity_type_id,
          relationship1: global_registry_relationship(type).primary_name,
          relationship2: global_registry_relationship(type).related_name
        }
      )["relationship_type"]
      rename_relationship_entity_type(rel)
    end
    rel
  end
  push_global_registry_relationship_type_fields(relationship_type)
  relationship_type
end

#push_global_registry_relationship_type_fields(relationship_type) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 76

def push_global_registry_relationship_type_fields(relationship_type)
  existing_fields = relationship_type["fields"]&.collect { |f| f["name"].to_sym } || []
  fields = model.relationship_columns_to_push(type)
    .except(*existing_fields)
    .map { |name, type| {name: name, field_type: type} }
  return if fields.empty?
  relationship_type = GlobalRegistry::RelationshipType.put(relationship_type["id"],
    relationship_type: {fields: fields})
    &.dig("relationship_type")
  Rails.cache.write(relationship_type_cache_key, relationship_type, expires_in: 1.hour) if relationship_type
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 57

def related_associated_entity_type_id
  unless global_registry_relationship(type).related
    related_type = global_registry_relationship(type).related_type
    # remote foreign_key doesn't have a model class in rails. Short-circuit and fetch entity_type by name
    entity_type = GlobalRegistry::EntityType.get(
      "filters[name]" => related_type
    )["entity_types"]&.first
    unless entity_type
      raise GlobalRegistry::Bindings::RelatedEntityTypeMissing,
        "#{model.class.name}(#{model.id}) has unknown related entity_type(#{related_type}) in " \
        "global_registry. Entity Type must exist in Global Registry for remote foreign_key relationship."
    end
    return entity_type&.dig("id")
  end
  related_worker =
    GlobalRegistry::Bindings::Workers::PushEntityWorker.new global_registry_relationship(type).related
  related_worker.send(:push_entity_type_to_global_registry)&.dig("id")
end

#relationship_type_cache_keyObject



95
96
97
98
99
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 95

def relationship_type_cache_key
  "GlobalRegistry::Bindings::RelationshipType::#{global_registry_relationship(type).primary_type}::" \
    "#{global_registry_relationship(type).related_type}::" \
    "#{global_registry_relationship(type).primary_name}"
end

#rename_relationship_entity_type(relationship_type) ⇒ Object



88
89
90
91
92
93
# File 'lib/global_registry_bindings/entity/relationship_type_methods.rb', line 88

def rename_relationship_entity_type(relationship_type)
  return unless global_registry_relationship(type).rename_entity_type?
  entity_type_id = relationship_type&.dig("relationship_entity_type_id")
  GlobalRegistry::EntityType.put(entity_type_id, entity_type: {id: entity_type_id,
                                                               name: global_registry_relationship(type).type})
end