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