]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
2820d9438a1e6868151e68a01595c0bed7f1610a
[rails.git] / app / models / old_way.rb
1 class OldWay < ActiveRecord::Base
2   set_table_name 'ways'
3
4   belongs_to :changeset
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.version = way.version
13     old_way.nds = way.nds
14     old_way.tags = way.tags
15     return old_way
16   end
17
18   def save_with_dependencies!
19
20     # dont touch this unless you really have figured out why it's called
21     # (Rails doesn't deal well with the old ways table (called 'ways') because
22     # it doesn't have a unique key. It knows how to insert and auto_increment
23     # id and get it back but we have that and we want to get the 'version' back
24     # we could add another column but thats a lot of data. No, set_primary_key
25     # doesn't work either.
26     save!
27     clear_aggregation_cache
28     clear_association_cache
29     @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
30
31     # ok, you can touch from here on
32
33     self.tags.each do |k,v|
34       tag = OldWayTag.new
35       tag.k = k
36       tag.v = v
37       tag.id = self.id
38       tag.version = self.version
39       tag.save!
40     end
41
42     sequence = 1
43     self.nds.each do |n|
44       nd = OldWayNode.new
45       nd.id = [self.id, self.version, sequence]
46       nd.node_id = n
47       nd.save!
48       sequence += 1
49     end
50   end
51
52   def nds
53     unless @nds
54         @nds = Array.new
55         OldWayNode.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |nd|
56             @nds += [nd.node_id]
57         end
58     end
59     @nds
60   end
61
62   def tags
63     unless @tags
64         @tags = Hash.new
65         OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
66             @tags[tag.k] = tag.v
67         end
68     end
69     @tags = Hash.new unless @tags
70     @tags
71   end
72
73   def nds=(s)
74     @nds = s
75   end
76
77   def tags=(t)
78     @tags = t
79   end
80
81 #  has_many :way_nodes, :class_name => 'OldWayNode', :foreign_key => 'id'
82 #  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
83
84   def old_nodes
85     OldWayNode.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
86   end
87
88   def old_tags
89     OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
90   end
91
92   def to_xml_node
93     el1 = XML::Node.new 'way'
94     el1['id'] = self.id.to_s
95     el1['visible'] = self.visible.to_s
96     el1['timestamp'] = self.timestamp.xmlschema
97     el1['user'] = self.user.display_name if self.user.data_public?
98     el1['version'] = self.version.to_s
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
115   # Read full version of old way
116   # For get_nodes_undelete, uses same nodes, even if they've moved since
117   # For get_nodes_revert,   allocates new ids 
118   # Currently returns Potlatch-style array
119   
120   def get_nodes_undelete
121         points = []
122         self.nds.each do |n|
123           node=Node.find(n)
124           points << [node.lon, node.lat, n, node.visible ? 1 : 0, node.tags_as_hash]
125     end
126         points
127   end
128   
129   def get_nodes_revert
130     points=[]
131     self.nds.each do |n|
132       oldnode=OldNode.find(:first, :conditions=>['id=? AND timestamp<=?',n,self.timestamp], :order=>"timestamp DESC")
133       curnode=Node.find(n)
134       id=n; v=curnode.visible ? 1 : 0
135       if oldnode.lat!=curnode.lat or oldnode.lon!=curnode.lon or oldnode.tags!=curnode.tags then
136         # node has changed: if it's in other ways, give it a new id
137         if curnode.ways-[self.id] then id=-1; v=nil end
138       end
139       points << [oldnode.lon, oldnode.lat, id, v, oldnode.tags_as_hash]
140     end
141     points
142   end
143
144   # Temporary method to match interface to nodes
145   def tags_as_hash
146     return self.tags
147   end
148
149   # Temporary method to match interface to ways
150   def way_nodes
151     return self.old_nodes
152   end
153
154   # Pretend we're not in any relations
155   def containing_relation_members
156     return []
157   end
158 end