rails way bits
[rails.git] / app / models / old_way.rb
1 class OldWay < ActiveRecord::Base
2   set_table_name 'ways'
3
4   belongs_to :user
5
6   def self.from_way(way)
7     old_way = OldWay.new
8     old_way.user_id = way.user_id
9     old_way.timestamp = way.timestamp
10     old_way.id = way.id
11     old_way.segs = way.segs
12     old_way.tags = way.tags
13     return old_way
14   end
15
16   def save
17     t = Time.now
18     self.timestamp = t
19     self.save
20     
21     WayTag.delete_all(['id = ?', self.id])
22
23     self.tags.each do |k,v|
24       tag = WayTag.new
25       tag.k = k
26       tag.v = v
27       tag.id = self.id
28       tag.save
29     end
30
31     WaySegment.delete_all(['id = ?', self.id])
32     
33     i = 0
34     self.segs.each do |n|
35       seg = WaySegment.new
36       seg.id = self.id
37       seg.segment_id = n
38       seg.sequence_id = i
39       seg.save
40       i += 1
41     end
42
43     old_way = OldWay.from_way(self)
44     old_way.save
45   end
46
47   def segs
48     @segs = Array.new unless @segs
49     @segs
50   end
51
52   def tags
53     @tags = Hash.new unless @tags
54     @tags
55   end
56
57   def segs=(s)
58     @segs = s
59   end
60
61   def tags=(t)
62     @tags = t
63   end
64
65 end