Module: DirectionalRelationships::ClassMethods
- Defined in:
- backend/app/model/mixins/directional_relationships.rb
Instance Attribute Summary collapse
-
#directional_relationships ⇒ Object
readonly
Returns the value of attribute directional_relationships.
Instance Method Summary collapse
-
#create_from_json(json, opts = {}) ⇒ Object
-
#define_directional_relationship(opts) ⇒ Object
-
#prepare_directional_relationship_for_display(json) ⇒ Object
-
#prepare_directional_relationship_for_storage(json) ⇒ Object
-
#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
Instance Attribute Details
#directional_relationships ⇒ Object (readonly)
Returns the value of attribute directional_relationships
52 53 54 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 52 def directional_relationships @directional_relationships end |
Instance Method Details
#create_from_json(json, opts = {}) ⇒ Object
113 114 115 116 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 113 def create_from_json(json, opts = {}) prepare_directional_relationship_for_storage(json) super end |
#define_directional_relationship(opts) ⇒ Object
54 55 56 57 58 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 54 def define_directional_relationship(opts) self.define_relationship(opts) @directional_relationships ||= [] @directional_relationships << opts end |
#prepare_directional_relationship_for_display(json) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 79 def prepare_directional_relationship_for_display(json) Array(directional_relationships).each do |rel| property = rel[:json_property] ASUtils.wrap(json[property]).each do |relationship| ref = JSONModel.parse_reference(json.uri) if (relationship['relationship_target_record_type'] == ref[:type] && relationship['relationship_target_id'] == ref[:id].to_i) # This means we're looking at the relationship from the other side. # # For example, if the relationship is "A is a parent of B", then we # want: # # * 'GET A' to yield {relator => 'is_parent_of', ref => 'B'} # * 'GET B' to yield {relator => 'is_child_of', ref => 'A'} # # So we want to invert the relator for this case. relator_values = JSONModel.enum_values(JSONModel(relationship['jsonmodel_type'].intern).schema['properties']['relator']['dynamic_enum']) relator_values -= ['other_unmapped'] # grumble. if relator_values.length == 2 # When there are two possible values we assume they're inverses # Set the relator to whatever the inverse of the current one is. relationship['relator'] = (relator_values - [relationship['relator']]).first end end end end end |
#prepare_directional_relationship_for_storage(json) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 61 def prepare_directional_relationship_for_storage(json) Array(directional_relationships).each do |rel| property = rel[:json_property] ASUtils.wrap(json[property]).each do |relationship| # Relationships are directionless by default, but here we want to # store a direction (e.g. A is a child of B) # # Store the JSONModel type and ID # ref = JSONModel.parse_reference(relationship['ref']) relationship['relationship_target_record_type'] = ref[:type] relationship['relationship_target_id'] = ref[:id].to_i end end end |
#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'backend/app/model/mixins/directional_relationships.rb', line 119 def sequel_to_jsonmodel(objs, opts = {}) jsons = super jsons.zip(objs).each do |json, obj| prepare_directional_relationship_for_display(json) end jsons end |