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