]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Began code for feature lookup: implemented code to respond to mouse click and latlon...
[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.visible = way.visible
9     old_way.user_id = way.user_id
10     old_way.timestamp = way.timestamp
11     old_way.id = way.id
12     old_way.nds = way.nds
13     old_way.tags = way.tags
14     return old_way
15   end
16
17   def save_with_dependencies!
18
19     # dont touch this unless you really have figured out why it's called
20     # (Rails doesn't deal well with the old ways table (called 'ways') because
21     # it doesn't have a unique key. It knows how to insert and auto_increment
22     # id and get it back but we have that and we want to get the 'version' back
23     # we could add another column but thats a lot of data. No, set_primary_key
24     # doesn't work either.
25     save!
26     clear_aggregation_cache
27     clear_association_cache
28     @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
29
30     # ok, you can touch from here on
31
32     self.tags.each do |k,v|
33       tag = OldWayTag.new
34       tag.k = k
35       tag.v = v
36       tag.id = self.id
37       tag.version = self.version
38       tag.save!
39     end
40
41     i = 1
42     self.nds.each do |n|
43       nd = OldWayNode.new
44       nd.id = self.id
45       nd.node_id = n
46       nd.sequence_id = i
47       nd.version = self.version
48       nd.save!
49       i += 1
50     end
51   end
52
53   def nds
54     unless @nds
55         @nds = Array.new
56         OldWayNode.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |nd|
57             @nds += [nd.node_id]
58         end
59     end
60     @nds
61   end
62
63   def tags
64     unless @tags
65         @tags = Hash.new
66         OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
67             @tags[tag.k] = tag.v
68         end
69     end
70     @tags = Hash.new unless @tags
71     @tags
72   end
73
74   def nds=(s)
75     @nds = s
76   end
77
78   def tags=(t)
79     @tags = t
80   end
81
82 #  has_many :way_nodes, :class_name => 'OldWayNode', :foreign_key => 'id'
83 #  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
84
85   def old_nodes
86     OldWayNode.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
87   end
88
89   def old_tags
90     OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
91   end
92
93   def to_xml_node
94     el1 = XML::Node.new 'way'
95     el1['id'] = self.id.to_s
96     el1['visible'] = self.visible.to_s
97     el1['timestamp'] = self.timestamp.xmlschema
98     el1['user'] = self.user.display_name if self.user.data_public?
99     
100     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
101       e = XML::Node.new 'nd'
102       e['ref'] = nd.node_id.to_s
103       el1 << e
104     end
105  
106     self.old_tags.each do |tag|
107       e = XML::Node.new 'tag'
108       e['k'] = tag.k
109       e['v'] = tag.v
110       el1 << e
111     end
112     return el1
113   end 
114 end