Module: Agents::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#agent_relator_enum(enum_name) ⇒ Object



56
57
58
# File 'backend/app/model/mixins/agents.rb', line 56

def agent_relator_enum(enum_name)
  @agent_relator_enum = enum_name
end

#agent_role_enum(enum_name) ⇒ Object



52
53
54
# File 'backend/app/model/mixins/agents.rb', line 52

def agent_role_enum(enum_name)
  @agent_role_enum = enum_name
end

#has_primary_agents?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'backend/app/model/mixins/agents.rb', line 128

def has_primary_agents?
  self.my_jsonmodel.schema['properties'].dig('linked_agents', 'items', 'properties', 'is_primary') ? true : false
end

#initialize_enum(clz) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'backend/app/model/mixins/agents.rb', line 60

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



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'backend/app/model/mixins/agents.rb', line 80

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

#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'backend/app/model/mixins/agents.rb', line 39

def sequel_to_jsonmodel(objs, opts = {})
  jsons = super
  if self.has_primary_agents?
    jsons.each do |json|
      json['linked_agents'].each do |agent_link|
        agent_link['is_primary'] = (agent_link['is_primary'] == 1)
      end
    end
  end

  jsons
end