Module: Agents::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#agent_relator_enum(enum_name) ⇒ Object



26
27
28
# File 'backend/app/model/mixins/agents.rb', line 26

def agent_relator_enum(enum_name)
  @agent_relator_enum = enum_name
end

#agent_role_enum(enum_name) ⇒ Object



22
23
24
# File 'backend/app/model/mixins/agents.rb', line 22

def agent_role_enum(enum_name)
  @agent_role_enum = enum_name
end

#initialize_enum(clz) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'backend/app/model/mixins/agents.rb', line 31

def initialize_enum(clz)
  role_enum = @agent_role_enum or raise "Missing agent_role_enum in #{self}"
  relator_enum = @agent_relator_enum

  if self.columns.include?(:relator_id) && relator_enum.nil?
    raise "Missing agent_relator_enum in #{self}"
  end

  clz.instance_eval do
    include DynamicEnums

    enums = []
    enums << {:property => 'role', :uses_enum => [role_enum]} if role_enum
    enums << {:property => 'relator', :uses_enum => [relator_enum]} if relator_enum

    uses_enums(*enums)
  end
end

#initialize_terms(clz) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'backend/app/model/mixins/agents.rb', line 51

def initialize_terms(clz)
  clz.instance_eval do
    include ASModel::CRUD
    include ASModel::SequelHooks

    self.strict_param_setting = false

    many_to_many :term, :left_key => :linked_agents_rlshp_id, :join_table => :linked_agent_term, :order => Sequel.qualify(:linked_agent_term, :id)

    def_nested_record(:the_property => :terms,
                      :contains_records_of_type => :term,
                      :corresponding_to_association  => :term)


    def self.create(values)
      obj = super
      obj.apply_nested_records({:terms => values['terms']}, true)
    end


    def self.handle_delete(ids_to_delete)
      self.db[:linked_agent_term].
           filter(:linked_agents_rlshp_id => ids_to_delete).
           delete

      super
    end


    alias_method :delete_orig, :delete
    define_method(:delete) do
      self.remove_all_term
      delete_orig
    end


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

      result['terms'] = Array(self.term).map {|term| Term.to_jsonmodel(term)}

      result
    end

  end
end