]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
There is no routing.js to compile
[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     save!
35
36     self.tags.each do |k,v|
37       tag = OldWayTag.new
38       tag.k = k
39       tag.v = v
40       tag.way_id = self.way_id
41       tag.version = self.version
42       tag.save!
43     end
44
45     sequence = 1
46     self.nds.each do |n|
47       nd = OldWayNode.new
48       nd.id = [self.way_id, self.version, sequence]
49       nd.node_id = n
50       nd.save!
51       sequence += 1
52     end
53   end
54
55   def nds
56     @nds ||= self.old_nodes.order(:sequence_id).collect { |n| n.node_id }
57   end
58
59   def tags
60     @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
61   end
62
63   def nds=(s)
64     @nds = s
65   end
66
67   def tags=(t)
68     @tags = t
69   end
70
71   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
72     el = XML::Node.new 'way'
73     el['id'] = self.way_id.to_s
74
75     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
76
77     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
78       node_el = XML::Node.new 'nd'
79       node_el['ref'] = nd.node_id.to_s
80       el << node_el
81     end
82       
83     add_tags_to_xml_node(el, self.old_tags)
84
85     return el
86   end
87
88   # Read full version of old way
89   # For get_nodes_undelete, uses same nodes, even if they've moved since
90   # For get_nodes_revert,   allocates new ids 
91   # Currently returns Potlatch-style array
92   # where [5] indicates whether latest version is usable as is (boolean)
93   # (i.e. is it visible? are we actually reverting to an earlier version?)
94
95   def get_nodes_undelete
96     self.nds.collect do |n|
97       node = Node.find(n)
98       [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
99     end
100   end
101   
102   def get_nodes_revert(timestamp)
103     points=[]
104     self.nds.each do |n|
105       oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
106       curnode = Node.find(n)
107       id = n; reuse = curnode.visible
108       if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
109         # node has changed: if it's in other ways, give it a new id
110         if curnode.ways-[self.way_id] then id=-1; reuse=false end
111       end
112       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
113     end
114     points
115   end
116
117   # Temporary method to match interface to nodes
118   def tags_as_hash
119     return self.tags
120   end
121
122   # Temporary method to match interface to ways
123   def way_nodes
124     return self.old_nodes
125   end
126
127   # Pretend we're not in any relations
128   def containing_relation_members
129     return []
130   end
131
132   # check whether this element is the latest version - that is,
133   # has the same version as its "current" counterpart.
134   def is_latest_version?
135     current_way.version == self.version
136   end
137 end