]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Added redactions controller test and fixed a bug in the controller
[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     if self.redacted?
108       el1['redacted'] = self.redaction.title
109     end
110     
111     unless self.redacted? and (@user.nil? or not @user.moderator?)
112       # If a way is redacted and the user isn't a moderator, only show
113       # meta-data from this revision, but no real data.
114       self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
115         e = XML::Node.new 'nd'
116         e['ref'] = nd.node_id.to_s
117         el1 << e
118       end
119       
120       self.old_tags.each do |tag|
121         e = XML::Node.new 'tag'
122         e['k'] = tag.k
123         e['v'] = tag.v
124         el1 << e
125       end
126     end
127     return el1
128   end
129
130   # Read full version of old way
131   # For get_nodes_undelete, uses same nodes, even if they've moved since
132   # For get_nodes_revert,   allocates new ids 
133   # Currently returns Potlatch-style array
134   # where [5] indicates whether latest version is usable as is (boolean)
135   # (i.e. is it visible? are we actually reverting to an earlier version?)
136
137   def get_nodes_undelete
138     points = []
139     self.nds.each do |n|
140       node = Node.find(n)
141       points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
142     end
143     points
144   end
145   
146   def get_nodes_revert(timestamp)
147     points=[]
148     self.nds.each do |n|
149       oldnode = OldNode.where('node_id = ? AND timestamp <= ? AND redaction_id is NULL', n, timestamp).order("timestamp DESC").first
150       curnode = Node.find(n)
151       id = n; reuse = curnode.visible
152       if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
153         # node has changed: if it's in other ways, give it a new id
154         if curnode.ways-[self.way_id] then id=-1; reuse=false end
155       end
156       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
157     end
158     points
159   end
160
161   # Temporary method to match interface to nodes
162   def tags_as_hash
163     return self.tags
164   end
165
166   # Temporary method to match interface to ways
167   def way_nodes
168     return self.old_nodes
169   end
170
171   # Pretend we're not in any relations
172   def containing_relation_members
173     return []
174   end
175
176   # check whether this element is the latest version - that is,
177   # has the same version as its "current" counterpart.
178   def is_latest_version?
179     current_way.version == self.version
180   end
181 end