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