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