Module: RelatedAgents

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

Class Method Summary (collapse)

Methods included from JSONModel

JSONModel, JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, 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_repository, strict_mode, strict_mode?, with_repository

Class Method Details

+ (Object) included(base)



6
7
8
9
10
11
12
13
14
15
16
17
# File 'backend/app/model/mixins/related_agents.rb', line 6

def self.included(base)
  callback = proc { |clz| RelatedAgents.set_up_date_record_handling(clz) }

  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 => callback)
end

+ (Object) set_up_date_record_handling(relationship_clz)

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'backend/app/model/mixins/related_agents.rb', line 22

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

    include ASModel::SequelHooks

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

      if date_values
        date = ASDate.create_from_json(JSONModel(:date).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'] = ASDate.to_jsonmodel(self.relationship_date).to_hash
      end

      result
    end
  end
end