Module: GlobalRegistry::Bindings::Entity::PushRelationshipMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- Workers::PushRelationshipWorker
- Defined in:
- lib/global_registry_bindings/entity/push_relationship_methods.rb
Instance Method Summary collapse
- #create_relationship_in_global_registry ⇒ Object
- #delete_relationship_from_global_registry(and_retry = true) ⇒ Object
- #ensure_related_entities_have_global_registry_ids! ⇒ Object
- #global_registry_relationship_entity_id_from_entity(entity) ⇒ Object
- #push_primary_to_global_registry ⇒ Object
- #push_related_to_global_registry ⇒ Object
- #push_relationship_to_global_registry ⇒ Object
- #put_relationship_to_global_registry ⇒ Object
- #relationship ⇒ Object
- #relationship_entity ⇒ Object
- #valid_update? ⇒ Boolean
Instance Method Details
#create_relationship_in_global_registry ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 38 def create_relationship_in_global_registry entity = put_relationship_to_global_registry relationship.id_value = global_registry_relationship_entity_id_from_entity entity model.update_column( # rubocop:disable Rails/SkipsModelValidations relationship.id_column, relationship.id_value ) end |
#delete_relationship_from_global_registry(and_retry = true) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 116 def delete_relationship_from_global_registry(and_retry = true) GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(relationship.id_value) model.update_column( # rubocop:disable Rails/SkipsModelValidations relationship.id_column, nil ) return unless and_retry raise GlobalRegistry::Bindings::RelatedEntityExistsWithCID, "#{model.class.name}(#{model.id}) #{relationship.}" \ ":relationship already exists with client_integration_id(" \ "#{relationship.client_integration_id}). Will delete and retry." end |
#ensure_related_entities_have_global_registry_ids! ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 60 def return if relationship.primary_id_value && relationship. # Enqueue push_entity worker for related entities missing global_registry_id and retry relationship push names = [] unless relationship.primary_id_value names << push_primary_to_global_registry end unless relationship. names << end raise GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId, "#{model.class.name}(#{model.id}) has related entities [#{names.compact.join ", "}] missing " \ "global_registry_id; will retry." end |
#global_registry_relationship_entity_id_from_entity(entity) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 47 def global_registry_relationship_entity_id_from_entity(entity) relationships = Array.wrap entity.dig( "entity", relationship.primary_type.to_s, "#{relationship.}:relationship" ) relationships.detect do |rel| cid = rel["client_integration_id"] cid = cid["value"] if cid.is_a?(Hash) cid == relationship.client_integration_id.to_s end&.dig("relationship_entity_id") end |
#push_primary_to_global_registry ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 75 def push_primary_to_global_registry model = relationship.primary if relationship.primary_binding == :entity model.push_entity_to_global_registry_async else model.push_relationships_to_global_registry_async(relationship.primary_binding) end "#{model.class.name}(#{model.id})" end |
#push_related_to_global_registry ⇒ Object
85 86 87 88 89 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 85 def model = relationship. model.push_entity_to_global_registry_async "#{model.class.name}(#{model.id})" end |
#push_relationship_to_global_registry ⇒ Object
16 17 18 19 20 21 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 16 def push_relationship_to_global_registry return unless valid_update? push_global_registry_relationship_type create_relationship_in_global_registry end |
#put_relationship_to_global_registry ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 100 def put_relationship_to_global_registry GlobalRegistry::Entity.put( relationship.primary_id_value, relationship_entity, params: { full_response: true, fields: "#{relationship.}:relationship" } ) rescue RestClient::BadRequest => e response = JSON.parse(e.response.body) raise unless /^Validation failed:.*already exists$/i.match?(response["error"]) # Delete relationship entity and retry on 400 Bad Request (client_integration_id already exists) delete_relationship_from_global_registry end |
#relationship ⇒ Object
12 13 14 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 12 def relationship global_registry_relationship(type) end |
#relationship_entity ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 91 def relationship_entity {entity: {relationship.primary_type => { "#{relationship.}:relationship" => model.relationship_attributes_to_push(type) .merge(relationship. => relationship.) }, :client_integration_id => relationship.primary.id}} end |
#valid_update? ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 23 def valid_update? # We can't update relationship if related model is missing, but we may need to delete if relationship..nil? if relationship..nil? && relationship.id_value # Delete relationship if it exists and the related id_value is missing delete_relationship_from_global_registry(false) return false end # Do nothing if related model is missing and related_binding is :entity, :remote binding allows # empty related model return false if relationship. == :entity end true end |