-
Notifications
You must be signed in to change notification settings - Fork 16
/
link.rb
118 lines (100 loc) · 2.4 KB
/
link.rb
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class Link < ActiveRecord::Base
attr_reader :relation
attr_accessor :start, :side
class << self
def find_through(node, link_id)
return nil unless link = Link.find(:first, :conditions => ['(source_id = ? OR target_id = ?) AND id = ?', node[:id], node[:id], link_id])
link.start = node
node.set_link(link)
link
end
end
# TODO: is this used ?
def update_attributes_with_transformations(attrs)
return false unless @node
if attrs['role']
# TODO: destroy this link and create a new one ?
end
rel = @node.relation_proxy_from_link(self)
rel.other_link = self
Zena::Relations::LINK_ATTRIBUTES.each do |k|
k = k.to_s # TODO: use only strings or symbols but avoid this mess
rel.send("other_#{k}=", attrs[k]) if attrs.has_key?(k)
self[k] = attrs[k]
end
if other_id = attrs['other_id'] || attrs['other_zip']
other_id = secure(Node) { Node.translate_pseudo_id(other_id, :id, @node) }
rel.other_id = other_id
if @side == :source
self[:target_id] = other_id
@target = nil
else
self[:source_id] = other_id
@source = nil
end
end
@node.save
@node.link = self
@errors = @node.errors
return errors.empty?
end
def target
@target ||= begin
node = secure!(Node) { Node.find(target_id) }
node.link = self
node
end
end
def source
@source ||= begin
node = secure!(Node) { Node.find(source_id) }
node.link = self
node
end
end
def start=(node)
@node = node
if @node[:id] == self[:source_id]
@side = :source
@source = @node
else
@side = :target
@target = @node
end
end
def other
@side == :source ? target : source
end
def this
@side == :source ? source : target
end
def other_zip
other[:zip]
end
def this_zip
this[:zip]
end
def zip
self[:id]
end
def can_write?
this.can_write?
end
def relation_proxy(node=nil)
return @relation_proxy if defined?(@relation_proxy)
rel = RelationProxy.find(self[:relation_id])
@node = node if node
if @node
if self[:source_id] == @node[:id]
rel.side = :source
else
rel.side = :target
end
rel.start = @node
end
@relation_proxy = rel
end
def role
relation_proxy.other_role
end
end