Module: RelatedAgents

Extended by:
JSONModel
Defined in:
backend/app/model/mixins/related_agents.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Methods included from JSONModel

JSONModel, JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'backend/app/model/mixins/related_agents.rb', line 6

def self.included(base)
  base.include(DirectionalRelationships)

  base.define_directional_relationship(:name => :related_agents,
                                       :json_property => 'related_agents',
                                       :contains_references_to_types => proc {
                                         AgentManager.registered_agents.map {|a| a[:model]}
                                       },
                                       :class_callback => proc {|clz|
                                         clz.instance_eval do
                                           include DynamicEnums
                                           uses_enums({
                                                        :property => 'relator',
                                                        :uses_enum => %w[agent_relationship_associative_relator
                                                                         agent_relationship_earlierlater_relator
                                                                         agent_relationship_parentchild_relator
                                                                         agent_relationship_subordinatesuperior_relator
                                                                         agent_relationship_identity_relator
                                                                         agent_relationship_hierarchical_relator
                                                                         agent_relationship_temporal_relator
                                                                         agent_relationship_family_relator]
                                                      },
                                                      {
                                                        :property => 'specific_relator',
                                                        :uses_enum => 'agent_relationship_specific_relator'
                                                      })
                                         end
                                         RelatedAgents.set_up_date_record_handling(clz)
                                       })
end

.set_up_date_record_handling(relationship_clz) ⇒ Object

When saving/loading this relationship, link up and fetch a nested date record to capture the dates.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'backend/app/model/mixins/related_agents.rb', line 40

def self.set_up_date_record_handling(relationship_clz)
  relationship_clz.instance_eval do
    extend JSONModel
    one_to_one :relationship_date, :class => "StructuredDateLabel", :key => :related_agents_rlshp_id

    include ASModel::SequelHooks

    def self.create(values)
      date_values = values.delete('dates')
      obj = super

      if date_values
        date = StructuredDateLabel.create_from_json(JSONModel(:structured_date_label).from_hash(date_values))
        obj.relationship_date = date
        obj.save
      end

      obj
    end


    alias_method :delete_orig, :delete
    define_method(:delete) do
      relationship_date.delete if relationship_date
      delete_orig
    end


    alias_method :values_orig, :values
    define_method(:values) do
      result = values_orig

      if self.relationship_date
        result['dates'] = StructuredDateLabel.to_jsonmodel(self.relationship_date).to_hash
      end

      result
    end
  end
end