Method: Architect4r::Model::Node.inherited

Defined in:
lib/architect4r/model/node.rb

.inherited(subklass) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/architect4r/model/node.rb', line 16

def self.inherited(subklass)
  super
  subklass.send(:include, ActiveModel::Conversion)
  subklass.extend ActiveModel::Naming
  subklass.send(:include, Architect4r::Model::Properties)
  subklass.send(:include, Architect4r::Model::Validations)
  
  subklass.class_exec do
    
    def self.model_root
      @model_root ||= begin
        # Check if there is already a model root,
        query = "start root = node(0) match root-[r:#{model_root_relation_type}]->x where r.architect4r_type and r.architect4r_type = '#{name}' return x"
        the_root = connection.cypher_query(query).to_a.first
        the_root &&= the_root['x']
        
        # otherwise create one
        the_root ||= begin 
          m_root = connection.create_node(:name => "#{name} Root", :root_for => name)
          connection.create_relationship(0, m_root, model_root_relation_type, { 'architect4r_type' => name })
          
          # Return model root node
          GenericNode.send(:build_from_database, m_root)
        end
      end
    end
    
  end
end