]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
408dd72c7e93dab73f0618c2b6849ad1721b5329
[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     unless @nds
69       @nds = Array.new
70       self.old_nodes.order(:sequence_id).each do |nd|
71         @nds += [nd.node_id]
72       end
73     end
74     @nds
75   end
76
77   def tags
78     unless @tags
79       @tags = Hash.new
80       self.old_tags.each do |tag|
81         @tags[tag.k] = tag.v
82       end
83     end
84     @tags = Hash.new unless @tags
85     @tags
86   end
87
88   def nds=(s)
89     @nds = s
90   end
91
92   def tags=(t)
93     @tags = t
94   end
95
96   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
97     el = XML::Node.new 'way'
98     el['id'] = self.way_id.to_s
99
100     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
101
102     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
103       node_el = XML::Node.new 'nd'
104       node_el['ref'] = nd.node_id.to_s
105       el << node_el
106     end
107       
108     add_tags_to_xml_node(el, self.old_tags)
109
110     return el
111   end
112
113   # Read full version of old way
114   # For get_nodes_undelete, uses same nodes, even if they've moved since
115   # For get_nodes_revert,   allocates new ids 
116   # Currently returns Potlatch-style array
117   # where [5] indicates whether latest version is usable as is (boolean)
118   # (i.e. is it visible? are we actually reverting to an earlier version?)
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.version, node.tags_as_hash, node.visible]
125     end
126     points
127   end
128   
129   def get_nodes_revert(timestamp)
130     points=[]
131     self.nds.each do |n|
132       oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
133       curnode = Node.find(n)
134       id = n; reuse = curnode.visible
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.way_id] then id=-1; reuse=false end
138       end
139       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
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
159   # check whether this element is the latest version - that is,
160   # has the same version as its "current" counterpart.
161   def is_latest_version?
162     current_way.version == self.version
163   end
164 end