]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Show a helpful message when there are no changesets to display
[rails.git] / app / models / old_way.rb
1 class OldWay < ActiveRecord::Base
2   include ConsistencyValidations
3
4   self.table_name = "ways"
5   self.primary_keys = "way_id", "version"
6
7   # note this needs to be included after the table name changes, or
8   # the queries generated by Redactable will use the wrong table name.
9   include Redactable
10
11   belongs_to :changeset
12   belongs_to :redaction
13   belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
14
15   has_many :old_nodes, :class_name => 'OldWayNode', :foreign_key => [:way_id, :version]
16   has_many :old_tags, :class_name => 'OldWayTag', :foreign_key => [:way_id, :version]
17
18   validates_associated :changeset
19   
20   def self.from_way(way)
21     old_way = OldWay.new
22     old_way.visible = way.visible
23     old_way.changeset_id = way.changeset_id
24     old_way.timestamp = way.timestamp
25     old_way.way_id = way.id
26     old_way.version = way.version
27     old_way.nds = way.nds
28     old_way.tags = way.tags
29     return old_way
30   end
31
32   def save_with_dependencies!
33
34     # dont touch this unless you really have figured out why it's called
35     # (Rails doesn't deal well with the old ways table (called 'ways') because
36     # it doesn't have a unique key. It knows how to insert and auto_increment
37     # id and get it back but we have that and we want to get the 'version' back
38     # we could add another column but thats a lot of data. No, set_primary_key
39     # doesn't work either.
40     save!
41     clear_aggregation_cache
42     clear_association_cache
43     @attributes.update(OldWay.where(:way_id => self.way_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
44
45     # ok, you can touch from here on
46
47     self.tags.each do |k,v|
48       tag = OldWayTag.new
49       tag.k = k
50       tag.v = v
51       tag.way_id = self.way_id
52       tag.version = self.version
53       tag.save!
54     end
55
56     sequence = 1
57     self.nds.each do |n|
58       nd = OldWayNode.new
59       nd.id = [self.way_id, self.version, sequence]
60       nd.node_id = n
61       nd.save!
62       sequence += 1
63     end
64   end
65
66   def nds
67     unless @nds
68       @nds = Array.new
69       OldWayNode.where(:way_id => self.way_id, :version => self.version).order(:sequence_id).each do |nd|
70         @nds += [nd.node_id]
71       end
72     end
73     @nds
74   end
75
76   def tags
77     unless @tags
78       @tags = Hash.new
79       OldWayTag.where(:way_id => self.way_id, :version => self.version).each do |tag|
80         @tags[tag.k] = tag.v
81       end
82     end
83     @tags = Hash.new unless @tags
84     @tags
85   end
86
87   def nds=(s)
88     @nds = s
89   end
90
91   def tags=(t)
92     @tags = t
93   end
94
95   def to_xml_node
96     el1 = XML::Node.new 'way'
97     el1['id'] = self.way_id.to_s
98     el1['visible'] = self.visible.to_s
99     el1['timestamp'] = self.timestamp.xmlschema
100     if self.changeset.user.data_public?
101       el1['user'] = self.changeset.user.display_name
102       el1['uid'] = self.changeset.user.id.to_s
103     end
104     el1['version'] = self.version.to_s
105     el1['changeset'] = self.changeset.id.to_s
106
107     el1['redacted'] = self.redaction.id.to_s if self.redacted?
108     
109     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
110       e = XML::Node.new 'nd'
111       e['ref'] = nd.node_id.to_s
112       el1 << e
113     end
114       
115     self.old_tags.each do |tag|
116       e = XML::Node.new 'tag'
117       e['k'] = tag.k
118       e['v'] = tag.v
119       el1 << e
120     end
121
122     return el1
123   end
124
125   # Read full version of old way
126   # For get_nodes_undelete, uses same nodes, even if they've moved since
127   # For get_nodes_revert,   allocates new ids 
128   # Currently returns Potlatch-style array
129   # where [5] indicates whether latest version is usable as is (boolean)
130   # (i.e. is it visible? are we actually reverting to an earlier version?)
131
132   def get_nodes_undelete
133     points = []
134     self.nds.each do |n|
135       node = Node.find(n)
136       points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
137     end
138     points
139   end
140   
141   def get_nodes_revert(timestamp)
142     points=[]
143     self.nds.each do |n|
144       oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
145       curnode = Node.find(n)
146       id = n; reuse = curnode.visible
147       if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
148         # node has changed: if it's in other ways, give it a new id
149         if curnode.ways-[self.way_id] then id=-1; reuse=false end
150       end
151       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
152     end
153     points
154   end
155
156   # Temporary method to match interface to nodes
157   def tags_as_hash
158     return self.tags
159   end
160
161   # Temporary method to match interface to ways
162   def way_nodes
163     return self.old_nodes
164   end
165
166   # Pretend we're not in any relations
167   def containing_relation_members
168     return []
169   end
170
171   # check whether this element is the latest version - that is,
172   # has the same version as its "current" counterpart.
173   def is_latest_version?
174     current_way.version == self.version
175   end
176 end