Module: Agents

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

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
# File 'backend/app/model/mixins/agents.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
  base.include(Relationships)

  base.define_relationship(:name => :linked_agents,
                           :json_property => 'linked_agents',
                           :contains_references_to_types => proc {AgentManager.registered_agents.map {|a| a[:model]}},
                           :class_callback => proc { |clz|
                             base.initialize_enum(clz)
                             base.initialize_terms(clz)

                             type = base.my_jsonmodel.record_type
                             id_field = "#{type}_id".intern

                             clz.instance_eval do
                               plugin :validation_helpers

                               define_method(:validate) do
                                 if self[:is_primary]
                                   validates_unique(
                                     [:is_primary, id_field],
                                     message: "A #{type} can have only one primary linked agent")
                                 end

                                 super()
                               end
                             end
                           })
end