root/trunk/app/models/node_link.rb @ 1

Revision 1, 1.9 KB (checked in by dema, 8 years ago)

Initial import

Line 
1class NodeLink < AbstractModel
2
3        property :node_id, "Node"
4        property :target_node_id, "Node"
5        property :position, "Integer"
6       
7  belongs_to :node
8  belongs_to :target_node, :class_name => "Node", :foreign_key => "target_node_id"
9  acts_as_list :scope => 'node_id = <#{SemanticRecord::Base::SR_NAMESPACE}#{node_id}> and type = <#{SemanticRecord::Base::SR_NAMESPACE}#{type_attr}>'
10 
11  def before_destroy() remove_from_list end
12 
13  def self.find_all_by_node(node_id, type = nil)
14    if type.nil?
15      find_all ["node_id = ?", node_id]
16    else
17      find_all ["node_id = ? and type = ?", node_id, to_uri(type)]
18                end     
19  end
20 
21  def self.destroy_by_values(node_id, target_node_id = nil, type = nil)
22    if target_node_id && type
23      destroy_all ["node_id = ? and target_node_id = ? and type = ?", node_id, target_node_id, to_uri(type)]
24    else
25      if type.nil?
26        destroy_all ["node_id = ? and target_node_id = ?", node_id, target_node_id]
27      elsif target_node_id.nil?
28        destroy_all ["node_id = ? and type = ?", node_id, to_uri(type)]
29      else
30        destroy_all ["node_id = ?", node_id]
31                end
32        end     
33        end
34
35        def self.find_by_values(node_id, target_node_id, type)
36          NodeLink.find_first [ "node_id = ? AND target_node_id = ? AND type = ?", node_id, target_node_id, type.to_uri ]
37        end
38
39        def after_save
40          treat_inverse { |nl, inverse_link|
41            if nl.nil?
42              klass = Object.const_get(inverse_link.name) rescue nil
43              klass.create("node_id" => target_node_id, "target_node_id" => node_id) if klass             
44                        end
45                }
46        end
47
48        def after_destroy
49          treat_inverse { |nl, inverse_link| nl.destroy if nl }
50        end
51       
52        def treat_inverse
53          link = Link.find_by_name(type)
54          if link.inverse_link
55            nl = NodeLink.find_by_values target_node_id, node_id, link.inverse_link.name                         
56            yield(nl, link.inverse_link)
57                end
58        end
59end
Note: See TracBrowser for help on using the browser.