Module: DirectionalRelationships

Defined in:
backend/app/model/mixins/directional_relationships.rb

Overview

This extends regular relationships to add support for storing a relationship between two records where the direction of the relationship matters. For example, a relationship between two agents might be called “is_parent_of” when viewed from one side, and “is_child_of” when viewed from the other.

Directional relationships are just relationships with some extra characteristics:

  • In the database, we store ‘relationship_target_record_type’ and ‘relationship_target_id’. These contain the type and identifier of the record that is the subject of this relationship. If the relationship is:

    A is_parent_of B

    Then these two columns will contain a reference to record B. This information is used to store the direction of the relationship.

  • We also store ‘jsonmodel_type’ in the database for the relationship, since there can be multiple relationship record types corresponding to a single logical relationship (the related_agent relationship uses this feature)

  • The ‘relator’ property describes the nature of the relationship. This should be an enum containing either one or two values. If it’s one value, the relator will be the same whether you look at the relationship from record A or record B (a relator like “sibling” would make sense for this case).

    If there are two relator values, they should be logical inverses. If you fetch record A you might see a relator of “is_parent_of” for its relationship. Fetch record B and the same relationship might show a relator of “is_child_of”–the relator is automatically mapped based on which direction you’re traversing the relationship in.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



38
39
40
41
# File 'backend/app/model/mixins/directional_relationships.rb', line 38

def self.included(base)
  base.include(Relationships)
  base.extend(ClassMethods)
end

Instance Method Details

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



44
45
46
47
# File 'backend/app/model/mixins/directional_relationships.rb', line 44

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.prepare_directional_relationship_for_storage(json)
  super
end