global-registry-bindings 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -4
  3. data/lib/global_registry_bindings/entity/push_relationship_methods.rb +53 -29
  4. data/lib/global_registry_bindings/entity/relationship_type_methods.rb +17 -8
  5. data/lib/global_registry_bindings/global_registry_bindings.rb +21 -14
  6. data/lib/global_registry_bindings/model/entity.rb +13 -9
  7. data/lib/global_registry_bindings/model/push_relationship.rb +6 -0
  8. data/lib/global_registry_bindings/model/relationship.rb +12 -10
  9. data/lib/global_registry_bindings/options/entity_class_options.rb +4 -0
  10. data/lib/global_registry_bindings/options/entity_instance_options.rb +1 -0
  11. data/lib/global_registry_bindings/options/entity_options_parser.rb +1 -0
  12. data/lib/global_registry_bindings/options/relationship_class_options.rb +9 -0
  13. data/lib/global_registry_bindings/options/relationship_instance_options.rb +16 -6
  14. data/lib/global_registry_bindings/options/relationship_options_parser.rb +5 -5
  15. data/lib/global_registry_bindings/version.rb +1 -1
  16. data/lib/global_registry_bindings/workers/push_relationship_worker.rb +2 -2
  17. data/spec/acceptance/global_registry_bindings_spec.rb +11 -9
  18. data/spec/factories/factories.rb +2 -0
  19. data/spec/fixtures/get_entity_types_fancy_org_assignment.json +35 -0
  20. data/spec/fixtures/post_relationship_types_assigned_by.json +16 -0
  21. data/spec/fixtures/put_entities_fancy_org_assignment_assigned_by.json +20 -0
  22. data/spec/internal/app/models/area.rb +2 -1
  23. data/spec/internal/app/models/assignment.rb +12 -2
  24. data/spec/internal/app/models/community.rb +2 -1
  25. data/spec/internal/app/models/namespaced/person.rb +6 -2
  26. data/spec/internal/app/models/organization.rb +3 -1
  27. data/spec/internal/db/schema.rb +2 -0
  28. data/spec/internal/log/test.log +28818 -0
  29. data/spec/workers/push_relationship_worker_spec.rb +133 -33
  30. metadata +10 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b416b8fb7f5710f5f4081a29bed3b1f246af6927
4
- data.tar.gz: 9051d30c9c92f2576edaa2c75ca47326c7b0c2c4
3
+ metadata.gz: fefecc675882ff5a75b69de5c5e928b41ca696e4
4
+ data.tar.gz: 70c7dfd4708fbb01c3824f4ad6b29cc31c5335ff
5
5
  SHA512:
6
- metadata.gz: d6755d3cd7c313cdb5e4ee38f8ebeb5d0a05169f553912068dfa0814cce964e863f5cd91b05bc6989370ca14c18ad475a457b325256b73118947ff09db86dbf3
7
- data.tar.gz: 4568cb20f2c87dbe69bddf01a5f5af5645a5fb03d1a24ba78d367ddeb822f108e68bc4cf59fe7cf7ae51a3c380e5c09b499d9eac45172f215b4128964aa541d3
6
+ metadata.gz: a80c913940d88cdf05b151c75a5fbe4bf09ad41441274c9a1672c8087b42c9ffc4fe67f46fbde9859359c10745d401b32272bfbad2f1a7103028f6362c722a07
7
+ data.tar.gz: 6538b6af58dc790cd1b164ddc4033360a1ca50c70488c62fe6fefdffc70fe177b0ca33f2c8b4c39be992f6c00d9f7945aaa3a19d1eafe8409c341b73f989f26f
data/README.md CHANGED
@@ -71,6 +71,10 @@ hierarchy or to push child entity types. (Ex: person -> address) (default: `nil`
71
71
  to determine the parent class. This can happen if parent is defined by another gem, like `ancestry`.
72
72
  (default: `nil`) **[`:entity`]**
73
73
 
74
+ * `:primary_binding`: Determines what type of global-registry-binding the primary association points to. Defaults
75
+ to `:entity`, but can be set to a `:relationship` type name (ex: `:assignment`) to create a relationship_type
76
+ between a relationship and an entity. (default: `:entity`) **[`:relationship`]**
77
+
74
78
  * `:primary_association`: Name of the Active Record primary association. Must be defined before calling
75
79
  global_registry_bindings in order to determine foreign_key for use in exclude_fields. (default: `nil`)
76
80
  **[`:relationship`]**
@@ -94,11 +98,11 @@ used to determine the related class. (default: `nil`) **[`:relationship`]**
94
98
  not be determined from `:primary_association`. (default: `:primary_association.foreign_key`)
95
99
  **[`:relationship`]**
96
100
 
97
- * `:primary_relationship_name`: Name of primary relationship role. Required if `:ensure_relationship_type` is
98
- `true`. (default: `nil`) **[`:relationship`]**
101
+ * `:primary_relationship_name`: **Required** Name of primary relationship. Should be unique to prevent
102
+ ambiguous relationship names. (default: `nil`) **[`:relationship`]**
99
103
 
100
- * `:related_relationship_name`: Name of the related relationship role. Required if `:ensure_relationship_type`
101
- is `true`. (default: `nil`) **[`:relationship`]**
104
+ * `:related_relationship_name`: **Required** Name of the related relationship. Should be unique to prevent
105
+ ambiguous relationship names (default: `nil`) **[`:relationship`]**
102
106
 
103
107
  * `:related_association_type`: Name of the related association entity_type. Required if unable to determined
104
108
  `:type` from related. (default: `nil`) **[`:relationship`]**
@@ -115,6 +119,10 @@ should cache this as it may be requested multiple times. (default: `nil`) **[`:r
115
119
  * `:client_integration_id`: Client Integration ID for relationship. Proc or Symbol.
116
120
  (default: `:primary_association.id`) **[`:relationship`]**
117
121
 
122
+ * `:include_all_columns`: Include all model columns in the fields to push to Global Registry. If `false`, fields must
123
+ be defined in the `:extra_fields` option. (default: `true`)
124
+ **[`:entity`, `:relationship`]**
125
+
118
126
  * `:exclude_fields`: Array, Proc or Symbol. Array of Model fields (as symbols) to exclude when pushing to Global
119
127
  Registry. Array Will additionally include `:mdm_id_column` and `:parent_association` foreign key when defined.
120
128
  If Proc, is passed type and model instance and should return an Array of the fields to exclude. If Symbol,
@@ -9,7 +9,16 @@ module GlobalRegistry #:nodoc:
9
9
  module PushRelationshipMethods
10
10
  extend ActiveSupport::Concern
11
11
 
12
+ def relationship
13
+ global_registry_relationship(type)
14
+ end
15
+
12
16
  def push_relationship_to_global_registry
17
+ # Delete relationship if it exists and the related id_value is missing
18
+ if relationship.related.nil? && relationship.related_id_value.nil? && relationship.id_value
19
+ delete_relationship_from_global_registry(false)
20
+ return
21
+ end
13
22
  ensure_related_entities_have_global_registry_ids!
14
23
  push_global_registry_relationship_type
15
24
  create_relationship_in_global_registry
@@ -17,77 +26,92 @@ module GlobalRegistry #:nodoc:
17
26
 
18
27
  def create_relationship_in_global_registry
19
28
  entity = put_relationship_to_global_registry
20
- global_registry_relationship(type).id_value = global_registry_relationship_entity_id_from_entity entity
29
+ relationship.id_value = global_registry_relationship_entity_id_from_entity entity
21
30
  model.update_column( # rubocop:disable Rails/SkipsModelValidations
22
- global_registry_relationship(type).id_column,
23
- global_registry_relationship(type).id_value
31
+ relationship.id_column,
32
+ relationship.id_value
24
33
  )
25
34
  end
26
35
 
27
36
  def global_registry_relationship_entity_id_from_entity(entity)
28
37
  relationships = Array.wrap entity.dig(
29
38
  'entity',
30
- global_registry_relationship(type).primary_type.to_s,
31
- "#{global_registry_relationship(type).related_relationship_name}:relationship"
39
+ relationship.primary_type.to_s,
40
+ "#{relationship.related_relationship_name}:relationship"
32
41
  )
33
42
  relationships.detect do |rel|
34
43
  cid = rel['client_integration_id']
35
44
  cid = cid['value'] if cid.is_a?(Hash)
36
- cid == global_registry_relationship(type).client_integration_id.to_s
45
+ cid == relationship.client_integration_id.to_s
37
46
  end&.dig('relationship_entity_id')
38
47
  end
39
48
 
40
- def ensure_related_entities_have_global_registry_ids! # rubocop:disable Metrics/AbcSize
41
- if global_registry_relationship(type).primary_id_value.present? &&
42
- global_registry_relationship(type).related_id_value.present?
43
- return
44
- end
49
+ def ensure_related_entities_have_global_registry_ids!
50
+ return if relationship.primary_id_value && relationship.related_id_value
45
51
  # Enqueue push_entity worker for related entities missing global_registry_id and retry relationship push
46
52
  names = []
47
- [global_registry_relationship(type).primary, global_registry_relationship(type).related].each do |model|
48
- next if model.global_registry_entity.id_value?
49
- names << "#{model.class.name}(#{model.id})"
50
- model.push_entity_to_global_registry_async
53
+ unless relationship.primary_id_value
54
+ names << push_primary_to_global_registry
55
+ end
56
+ unless relationship.related_id_value
57
+ names << push_related_to_global_registry
51
58
  end
52
59
  raise GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId,
53
- "#{model.class.name}(#{model.id}) has related entities [#{names.join ', '}] missing " \
60
+ "#{model.class.name}(#{model.id}) has related entities [#{names.compact.join ', '}] missing " \
54
61
  'global_registry_id; will retry.'
55
62
  end
56
63
 
64
+ def push_primary_to_global_registry
65
+ model = relationship.primary
66
+ if relationship.primary_binding == :entity
67
+ model.push_entity_to_global_registry_async
68
+ else
69
+ model.push_relationships_to_global_registry_async(relationship.primary_binding)
70
+ end
71
+ "#{model.class.name}(#{model.id})"
72
+ end
73
+
74
+ def push_related_to_global_registry
75
+ model = relationship.related
76
+ model.push_entity_to_global_registry_async
77
+ "#{model.class.name}(#{model.id})"
78
+ end
79
+
57
80
  def relationship_entity
58
- { entity: { global_registry_relationship(type).primary_type => {
59
- "#{global_registry_relationship(type).related_relationship_name}:relationship" =>
81
+ { entity: { relationship.primary_type => {
82
+ "#{relationship.related_relationship_name}:relationship" =>
60
83
  model.relationship_attributes_to_push(type)
61
- .merge(global_registry_relationship(type).related_type =>
62
- global_registry_relationship(type).related_id_value)
63
- }, client_integration_id: global_registry_relationship(type).primary.id } }
84
+ .merge(relationship.related_type =>
85
+ relationship.related_id_value)
86
+ }, client_integration_id: relationship.primary.id } }
64
87
  end
65
88
 
66
89
  def put_relationship_to_global_registry
67
90
  GlobalRegistry::Entity.put(
68
- global_registry_relationship(type).primary_id_value,
91
+ relationship.primary_id_value,
69
92
  relationship_entity,
70
93
  params: {
71
94
  full_response: true,
72
- fields: "#{global_registry_relationship(type).related_relationship_name}:relationship"
95
+ fields: "#{relationship.related_relationship_name}:relationship"
73
96
  }
74
97
  )
75
98
  rescue RestClient::BadRequest => e
76
99
  response = JSON.parse(e.response.body)
77
100
  raise unless response['error'] =~ /^Validation failed:.*already exists$/i
78
101
  # Delete relationship entity and retry on 400 Bad Request (client_integration_id already exists)
79
- delete_relationship_from_global_registry_and_retry
102
+ delete_relationship_from_global_registry
80
103
  end
81
104
 
82
- def delete_relationship_from_global_registry_and_retry
83
- GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(global_registry_relationship(type).id_value)
105
+ def delete_relationship_from_global_registry(and_retry = true)
106
+ GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(relationship.id_value)
84
107
  model.update_column( # rubocop:disable Rails/SkipsModelValidations
85
- global_registry_relationship(type).id_column, nil
108
+ relationship.id_column, nil
86
109
  )
110
+ return unless and_retry
87
111
  raise GlobalRegistry::Bindings::RelatedEntityExistsWithCID,
88
- "#{model.class.name}(#{model.id}) #{global_registry_relationship(type).related_relationship_name}" \
112
+ "#{model.class.name}(#{model.id}) #{relationship.related_relationship_name}" \
89
113
  ':relationship already exists with client_integration_id(' \
90
- "#{global_registry_relationship(type).client_integration_id}). Will delete and retry."
114
+ "#{relationship.client_integration_id}). Will delete and retry."
91
115
  end
92
116
  end
93
117
  end
@@ -16,17 +16,14 @@ module GlobalRegistry #:nodoc:
16
16
  related_entity_type_id = related_associated_entity_type_id
17
17
 
18
18
  relationship_type = Rails.cache.fetch(relationship_type_cache_key, expires_in: 1.hour) do
19
- GlobalRegistry::RelationshipType.get(
19
+ rel = GlobalRegistry::RelationshipType.get(
20
20
  'filters[between]' => "#{primary_entity_type_id},#{related_entity_type_id}"
21
21
  )['relationship_types'].detect do |r|
22
22
  r['relationship1']['relationship_name'] ==
23
23
  global_registry_relationship(type).primary_relationship_name.to_s
24
24
  end
25
- end
26
-
27
- unless relationship_type
28
- relationship_type =
29
- GlobalRegistry::RelationshipType.post(
25
+ unless rel
26
+ rel = GlobalRegistry::RelationshipType.post(
30
27
  relationship_type: {
31
28
  entity_type1_id: primary_entity_type_id,
32
29
  entity_type2_id: related_entity_type_id,
@@ -34,6 +31,9 @@ module GlobalRegistry #:nodoc:
34
31
  relationship2: global_registry_relationship(type).related_relationship_name
35
32
  }
36
33
  )['relationship_type']
34
+ rename_relationship_entity_type(rel)
35
+ end
36
+ rel
37
37
  end
38
38
  push_global_registry_relationship_type_fields(relationship_type)
39
39
  relationship_type
@@ -79,8 +79,17 @@ module GlobalRegistry #:nodoc:
79
79
  .reject { |k, _v| existing_fields.include? k }
80
80
  .map { |name, type| { name: name, field_type: type } }
81
81
  return if fields.empty?
82
- GlobalRegistry::RelationshipType.put(relationship_type['id'],
83
- relationship_type: { fields: fields })
82
+ relationship_type = GlobalRegistry::RelationshipType.put(relationship_type['id'],
83
+ relationship_type: { fields: fields })
84
+ &.dig('relationship_type')
85
+ Rails.cache.write(relationship_type_cache_key, relationship_type, expires_in: 1.hour) if relationship_type
86
+ end
87
+
88
+ def rename_relationship_entity_type(relationship_type)
89
+ return unless global_registry_relationship(type).rename_entity_type?
90
+ entity_type_id = relationship_type&.dig('relationship_entity_type_id')
91
+ GlobalRegistry::EntityType.put(entity_type_id, entity_type: { id: entity_type_id,
92
+ name: global_registry_relationship(type).type })
84
93
  end
85
94
 
86
95
  def relationship_type_cache_key
@@ -42,6 +42,10 @@ module GlobalRegistry #:nodoc:
42
42
  # to determine the parent class. This can happen if parent is defined by another gem, like `ancestry`.
43
43
  # (default: `nil`) **[`:entity`]**
44
44
  #
45
+ # * `:primary_binding`: Determines what type of global-registry-binding the primary association points to. Defaults
46
+ # to `:entity`, but can be set to a `:relationship` type name (ex: `:assignment`) to create a relationship_type
47
+ # between a relationship and an entity. (default: `:entity`) **[`:relationship`]**
48
+ #
45
49
  # * `:primary_association`: Name of the Active Record primary association. Must be defined before calling
46
50
  # global_registry_bindings in order to determine foreign_key for use in exclude_fields. (default: `nil`)
47
51
  # **[`:relationship`]**
@@ -65,11 +69,11 @@ module GlobalRegistry #:nodoc:
65
69
  # not be determined from `:primary_association`. (default: `:primary_association.foreign_key`)
66
70
  # **[`:relationship`]**
67
71
  #
68
- # * `:primary_relationship_name`: Name of primary relationship role. Required if `:ensure_relationship_type` is
69
- # `true`. (default: `nil`) **[`:relationship`]**
72
+ # * `:primary_relationship_name`: **Required** Name of primary relationship. Should be unique to prevent
73
+ # ambiguous relationship names. (default: `nil`) **[`:relationship`]**
70
74
  #
71
- # * `:related_relationship_name`: Name of the related relationship role. Required if `:ensure_relationship_type`
72
- # is `true`. (default: `nil`) **[`:relationship`]**
75
+ # * `:related_relationship_name`: **Required** Name of the related relationship. Should be unique to prevent
76
+ # ambiguous relationship names (default: `nil`) **[`:relationship`]**
73
77
  #
74
78
  # * `:related_association_type`: Name of the related association entity_type. Required if unable to determined
75
79
  # `:type` from related. (default: `nil`) **[`:relationship`]**
@@ -84,20 +88,23 @@ module GlobalRegistry #:nodoc:
84
88
  # (default: `true`) **[`:entity`]**
85
89
  #
86
90
  # * `:client_integration_id`: Client Integration ID for relationship. Proc or Symbol.
87
- # (default: `:primary_association.id`) **[`:relationship`]**
91
+ # (default: `:primary_association.id`) **[`:relationship`]**
88
92
  #
89
93
  # * `:exclude_fields`: Array, Proc or Symbol. Array of Model fields (as symbols) to exclude when pushing to Global
90
- # Registry. Array Will additionally include `:mdm_id_column` and `:parent_association` foreign key when defined.
91
- # If Proc, is passed type and model instance and should return an Array of the fields to exclude. If Symbol,
92
- # this should be a method name the Model instance responds to. It is passed the type and should return an Array
93
- # of fields to exclude. When Proc or Symbol are used, you must explicitly return the standard defaults.
94
- # (default: `[:id, :created_at, :updated_at, :global_registry_id]`) **[`:entity`, `:relationship`]**
94
+ # Registry. Array Will additionally include `:mdm_id_column` and `:parent_association` foreign key when defined.
95
+ # If Proc, is passed type and model instance and should return an Array of the fields to exclude. If Symbol,
96
+ # this should be a method name the Model instance responds to. It is passed the type and should return an Array
97
+ # of fields to exclude. When Proc or Symbol are used, you must explicitly return the standard defaults.
98
+ # (default: `[:id, :created_at, :updated_at, :global_registry_id]`) **[`:entity`, `:relationship`]**
95
99
  #
96
100
  # * `:extra_fields`: Additional fields to send to Global Registry. Hash, Proc or Symbol. As a Hash, names are the
97
- # keys and :type attributes are the values. Ex: `{language: :string}`. Name is a symbol and type is an
98
- # ActiveRecord column type. As a Proc, it is passed the type and model instance, and should return a Hash.
99
- # As a Symbol, the model should respond to this method, is passed the type, and should return a Hash.
100
- # **[`:entity`, `:relationship`]**
101
+ # keys and :type attributes are the values. Ex: `{language: :string}`. Name is a symbol and type is an
102
+ # ActiveRecord column type. As a Proc, it is passed the type and model instance, and should return a Hash.
103
+ # As a Symbol, the model should respond to this method, is passed the type, and should return a Hash.
104
+ # **[`:entity`, `:relationship`]**
105
+ #
106
+ # * `:include_all_columns`: Include all model columns in the fields to push to Global Registry. If `false`, fields
107
+ # must be defined in the `:extra_fields` option. (default: `true`) **[`:entity`, `:relationship`]**
101
108
  #
102
109
  # * `:mdm_id_column`: Column used to enable MDM tracking and set the name of the column. MDM is disabled when this
103
110
  # option is nil or empty. (default: `nil`) **[`:entity`]**
@@ -36,15 +36,19 @@ module GlobalRegistry #:nodoc:
36
36
  end
37
37
 
38
38
  def entity_columns_to_push
39
- @columns_to_push ||= self
40
- .class
41
- .columns
42
- .collect do |c|
43
- { c.name.underscore.to_sym => normalize_entity_column_type(c.type, c.name) }
44
- end # rubocop:disable Style/MultilineBlockChain
45
- .reduce(&:merge)
46
- .reject { |k, _v| global_registry_entity.exclude_fields.include? k }
47
- .merge(global_registry_entity.extra_fields)
39
+ @columns_to_push ||= if global_registry_entity.include_all_columns?
40
+ self
41
+ .class
42
+ .columns
43
+ .collect do |c|
44
+ { c.name.underscore.to_sym => normalize_entity_column_type(c.type, c.name) }
45
+ end # rubocop:disable Style/MultilineBlockChain
46
+ .reduce(&:merge)
47
+ .reject { |k, _v| global_registry_entity.exclude_fields.include? k }
48
+ .merge(global_registry_entity.extra_fields)
49
+ else
50
+ global_registry_entity.extra_fields
51
+ end
48
52
  end
49
53
 
50
54
  protected
@@ -43,11 +43,17 @@ module GlobalRegistry #:nodoc:
43
43
  ::GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(
44
44
  global_registry_relationship(type).id_value
45
45
  )
46
+ update_column( # rubocop:disable Rails/SkipsModelValidations
47
+ global_registry_relationship(type).id_column, nil
48
+ )
46
49
  ::GlobalRegistry::Bindings::Workers::PushRelationshipWorker.perform_async(self.class, id, type)
47
50
  end
48
51
 
49
52
  def global_registry_relationship_async_delete(type)
50
53
  delete_relationships_from_global_registry_async(type)
54
+ update_column( # rubocop:disable Rails/SkipsModelValidations
55
+ global_registry_relationship(type).id_column, nil
56
+ )
51
57
  end
52
58
 
53
59
  def global_registry_relationship_async_ignore(_type); end # noop
@@ -35,10 +35,7 @@ module GlobalRegistry #:nodoc:
35
35
 
36
36
  def relationship_columns_to_push(type)
37
37
  @relationship_columns_to_push ||= {}
38
- @relationship_columns_to_push[type] ||=
39
- relationship_entity_columns(type)
40
- .reject { |k, _v| global_registry_relationship(type).exclude_fields.include? k }
41
- .merge(global_registry_relationship(type).extra_fields)
38
+ @relationship_columns_to_push[type] ||= relationship_entity_columns(type)
42
39
  end
43
40
 
44
41
  protected
@@ -54,13 +51,18 @@ module GlobalRegistry #:nodoc:
54
51
  end
55
52
 
56
53
  def relationship_entity_columns(type)
57
- return {} if global_registry_relationship(type).primary_class_is_self?
58
- self.class
59
- .columns
60
- .collect do |c|
61
- { c.name.underscore.to_sym => normalize_relationship_column_type(c.type, c.name) }
54
+ if global_registry_relationship(type).include_all_columns?
55
+ self.class
56
+ .columns
57
+ .collect do |c|
58
+ { c.name.underscore.to_sym => normalize_relationship_column_type(c.type, c.name) }
59
+ end # rubocop:disable Style/MultilineBlockChain
60
+ .reduce(&:merge)
61
+ .reject { |k, _v| global_registry_relationship(type).exclude_fields.include? k }
62
+ .merge(global_registry_relationship(type).extra_fields)
63
+ else
64
+ global_registry_relationship(type).extra_fields || {}
62
65
  end
63
- .reduce(&:merge)
64
66
  end
65
67
  end
66
68
  end
@@ -25,6 +25,10 @@ module GlobalRegistry #:nodoc:
25
25
  @options.ensure_entity_type.present?
26
26
  end
27
27
 
28
+ def include_all_columns?
29
+ @options.include_all_columns.present?
30
+ end
31
+
28
32
  def mdm_worker_class_name
29
33
  "Pull#{@model_class.name.tr(':', '')}MdmIdWorker"
30
34
  end
@@ -12,6 +12,7 @@ module GlobalRegistry #:nodoc:
12
12
  :parent_association_class,
13
13
  :mdm_worker_class_name,
14
14
  :ensure_entity_type?,
15
+ :include_all_columns?,
15
16
  to: :@class_options
16
17
 
17
18
  def initialize(model)
@@ -19,6 +19,7 @@ module GlobalRegistry #:nodoc:
19
19
  parent_relationship_name: nil,
20
20
  exclude_fields: %i[id created_at updated_at],
21
21
  extra_fields: {},
22
+ include_all_columns: true,
22
23
  mdm_timeout: 1.minute,
23
24
  ensure_entity_type: true
24
25
  }.freeze
@@ -10,6 +10,7 @@ module GlobalRegistry #:nodoc:
10
10
  :type,
11
11
  :push_on,
12
12
  :client_integration_id,
13
+ :primary_binding,
13
14
  :primary_association,
14
15
  :primary_association_class,
15
16
  :primary_association_foreign_key,
@@ -31,6 +32,14 @@ module GlobalRegistry #:nodoc:
31
32
  def ensure_relationship_type?
32
33
  @options.ensure_relationship_type.present?
33
34
  end
35
+
36
+ def rename_entity_type?
37
+ @options.rename_entity_type.present?
38
+ end
39
+
40
+ def include_all_columns?
41
+ @options.include_all_columns.present?
42
+ end
34
43
  end
35
44
  end
36
45
  end
@@ -6,12 +6,16 @@ module GlobalRegistry #:nodoc:
6
6
  class RelationshipInstanceOptions
7
7
  delegate :id_column,
8
8
  :push_on,
9
+ :primary_binding,
9
10
  :primary_association,
10
11
  :primary_association_class,
11
12
  :primary_association_foreign_key,
12
13
  :related_association,
13
14
  :related_association_class,
14
15
  :related_association_foreign_key,
16
+ :ensure_relationship_type?,
17
+ :rename_entity_type?,
18
+ :include_all_columns?,
15
19
  to: :@class_options
16
20
 
17
21
  def initialize(type, model)
@@ -58,11 +62,21 @@ module GlobalRegistry #:nodoc:
58
62
  end
59
63
 
60
64
  def primary_type
61
- primary&.global_registry_entity&.type
65
+ case primary_binding
66
+ when :entity
67
+ primary&.global_registry_entity&.type
68
+ else
69
+ primary&.global_registry_relationship(primary_binding)&.type
70
+ end
62
71
  end
63
72
 
64
73
  def primary_id_value
65
- primary&.global_registry_entity&.id_value
74
+ case primary_binding
75
+ when :entity
76
+ primary&.global_registry_entity&.id_value
77
+ else
78
+ primary&.global_registry_relationship(primary_binding)&.id_value
79
+ end
66
80
  end
67
81
 
68
82
  def primary_class_is_self?
@@ -120,10 +134,6 @@ module GlobalRegistry #:nodoc:
120
134
  option
121
135
  end
122
136
  end
123
-
124
- def ensure_relationship_type?
125
- @class_options.ensure_relationship_type?
126
- end
127
137
  end
128
138
  end
129
139
  end
@@ -12,16 +12,16 @@ module GlobalRegistry #:nodoc:
12
12
  {
13
13
  id_column: :global_registry_id,
14
14
  type: @model_class.name.demodulize.underscore.to_sym,
15
- client_integration_id: :id,
15
+ client_integration_id: :id, primary_binding: :entity,
16
16
  primary_association: nil,
17
17
  primary_association_class: nil,
18
18
  primary_relationship_name: nil,
19
- related_association: nil, related_association_type: nil,
20
- related_association_class: nil,
19
+ related_association: nil,
20
+ related_association_class: nil, related_association_type: nil,
21
21
  related_relationship_name: nil,
22
22
  related_global_registry_id: nil,
23
- exclude_fields: %i[id created_at updated_at],
24
- extra_fields: {}, ensure_relationship_type: true
23
+ exclude_fields: %i[id created_at updated_at], include_all_columns: true,
24
+ extra_fields: {}, ensure_relationship_type: true, rename_entity_type: true
25
25
  }.freeze
26
26
  end
27
27
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GlobalRegistry #:nodoc:
4
4
  module Bindings #:nodoc:
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.2'
6
6
  end
7
7
  end
@@ -16,12 +16,12 @@ module GlobalRegistry #:nodoc:
16
16
 
17
17
  def initialize(model = nil, type = nil)
18
18
  super model
19
- self.type = type
19
+ self.type = type.to_sym if type
20
20
  end
21
21
 
22
22
  def perform(model_class, id, type)
23
23
  super model_class, id
24
- self.type = type
24
+ self.type = type.to_sym
25
25
  push_relationship_to_global_registry
26
26
  rescue ActiveRecord::RecordNotFound # rubocop:disable Lint/HandleExceptions
27
27
  # If the record was deleted after the job was created, swallow it
@@ -70,15 +70,17 @@ RSpec.describe 'GlobalRegistry::Bindings' do
70
70
  person = build(:person)
71
71
  org = build(:organization)
72
72
  assignment = build(:assignment, person: person, organization: org)
73
- expect(Assignment.global_registry_relationship(:assignment).id_column).to be :global_registry_id
74
- expect(Assignment.global_registry_relationship(:assignment).type).to be :assignment
75
- expect(Assignment.global_registry_relationship(:assignment).primary_association).to be :person
76
- expect(Assignment.global_registry_relationship(:assignment).related_association).to be :organization
77
- expect(assignment.global_registry_relationship(:assignment).primary_relationship_name).to be :person
78
- expect(assignment.global_registry_relationship(:assignment).related_relationship_name).to be :fancy_org
79
- expect(assignment.global_registry_relationship(:assignment).exclude_fields)
80
- .to contain_exactly(:global_registry_id, :id, :created_at, :updated_at, :person_id, :organization_id)
81
- expect(assignment.global_registry_relationship(:assignment).extra_fields).to be_a(Hash).and be_empty
73
+ expect(Assignment.global_registry_relationship(:fancy_org_assignment).id_column).to be :global_registry_id
74
+ expect(Assignment.global_registry_relationship(:fancy_org_assignment).type).to be :fancy_org_assignment
75
+ expect(Assignment.global_registry_relationship(:fancy_org_assignment).primary_association).to be :person
76
+ expect(Assignment.global_registry_relationship(:fancy_org_assignment).related_association).to be :organization
77
+ expect(assignment.global_registry_relationship(:fancy_org_assignment).primary_relationship_name).to be :person
78
+ expect(assignment.global_registry_relationship(:fancy_org_assignment).related_relationship_name).to be :fancy_org
79
+ expect(assignment.global_registry_relationship(:fancy_org_assignment).exclude_fields)
80
+ .to contain_exactly(:global_registry_id, :id, :created_at, :updated_at, :person_id, :organization_id,
81
+ :assigned_by_gr_rel_id, :assigned_by_id)
82
+ expect(assignment.global_registry_relationship(:fancy_org_assignment).extra_fields)
83
+ .to be_a(Hash)
82
84
  end
83
85
  end
84
86
  end
@@ -47,6 +47,8 @@ FactoryGirl.define do
47
47
  person_id nil
48
48
  organization_id nil
49
49
  global_registry_id nil
50
+ assigned_by_id nil
51
+ assigned_by_gr_rel_id nil
50
52
  end
51
53
 
52
54
  factory :area do
@@ -0,0 +1,35 @@
1
+ {
2
+ "entity_types": [
3
+ {
4
+ "id": "5d70b630-4248-11e7-90b4-129bd0521531",
5
+ "name": "fancy_org_assignment",
6
+ "data_visibility": "public",
7
+ "fields": [
8
+ {
9
+ "id": "235ce6fc-4249-11e7-944f-129bd0521531",
10
+ "name": "role",
11
+ "data_visibility": "public",
12
+ "field_type": "string",
13
+ "is_editable": false,
14
+ "unique_value": false
15
+ },
16
+ {
17
+ "id": "675bd9ee-4249-11e7-9e93-129bd0521531",
18
+ "name": "hired_at",
19
+ "data_visibility": "public",
20
+ "field_type": "datetime",
21
+ "is_editable": false,
22
+ "unique_value": false
23
+ }
24
+ ],
25
+ "is_editable": false,
26
+ "unique_value": false
27
+ }
28
+ ],
29
+ "meta": {
30
+ "page": 1,
31
+ "next_page": false,
32
+ "from": 1,
33
+ "to": 1
34
+ }
35
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "relationship_type": {
3
+ "id": "4f5e565c-76c7-11e7-b3f0-129bd0521531",
4
+ "relationship_entity_type_id": "4f5d8150-76c7-11e7-b3f0-129bd0521531",
5
+ "relationship1": {
6
+ "entity_type": "fancy_org_assignment",
7
+ "relationship_name": "assigned_by"
8
+ },
9
+ "relationship2": {
10
+ "entity_type": "person",
11
+ "relationship_name": "person"
12
+ },
13
+ "fields": [],
14
+ "is_editable": true
15
+ }
16
+ }