Class: Link

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RubyLess, Zena::Use::QueryLink::ModelMethods
Defined in:
app/models/link.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Zena::Use::QueryLink::ModelMethods

included

Instance Attribute Details

#relationObject (readonly)

Returns the value of attribute relation.



10
11
12
# File 'app/models/link.rb', line 10

def relation
  @relation
end

#sideObject

Returns the value of attribute side.



11
12
13
# File 'app/models/link.rb', line 11

def side
  @side
end

#startObject

Returns the value of attribute start.



11
12
13
# File 'app/models/link.rb', line 11

def start
  @start
end

Class Method Details

.find_through(node, link_id) ⇒ Object



14
15
16
17
18
19
# File 'app/models/link.rb', line 14

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.link  = link
  link
end

Instance Method Details

#can_write?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/link.rb', line 104

def can_write?
  this.can_write?
end


132
133
134
# File 'app/models/link.rb', line 132

def link_count
  read_attribute('link_count') || 0
end

#nameObject

Used by tags



128
129
130
# File 'app/models/link.rb', line 128

def name
  comment
end

#otherObject



84
85
86
# File 'app/models/link.rb', line 84

def other
  @side == :source ? target : source
end

#other_zipObject



92
93
94
# File 'app/models/link.rb', line 92

def other_zip
  other[:zip]
end

#relation_proxy(node = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/link.rb', line 108

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

#roleObject



123
124
125
# File 'app/models/link.rb', line 123

def role
  relation_proxy.other_role
end

#sourceObject



65
66
67
68
69
70
71
# File 'app/models/link.rb', line 65

def source
  @source ||= begin
    node = secure!(Node) { Node.find(source_id) }
    node.link = self
    node
  end
end

#targetObject



57
58
59
60
61
62
63
# File 'app/models/link.rb', line 57

def target
  @target ||= begin
    node = secure!(Node) { Node.find(target_id) }
    node.link = self
    node
  end
end

#thisObject



88
89
90
# File 'app/models/link.rb', line 88

def this
  @side == :source ? source : target
end

#this_zipObject



96
97
98
# File 'app/models/link.rb', line 96

def this_zip
  this[:zip]
end

#update_attributes_with_transformations(attrs) ⇒ Object

TODO: is this used ?



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
# File 'app/models/link.rb', line 23

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::Use::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

#zipObject



100
101
102
# File 'app/models/link.rb', line 100

def zip
  self[:id]
end