]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
31e230c3869e2ee197f46a8de823834319536c5f
[rails.git] / app / models / old_way.rb
1 # == Schema Information
2 #
3 # Table name: ways
4 #
5 #  way_id       :bigint(8)        default(0), not null, primary key
6 #  changeset_id :bigint(8)        not null
7 #  timestamp    :datetime         not null
8 #  version      :bigint(8)        not null, primary key
9 #  visible      :boolean          default(TRUE), not null
10 #  redaction_id :integer
11 #
12 # Indexes
13 #
14 #  ways_changeset_id_idx  (changeset_id)
15 #  ways_timestamp_idx     (timestamp)
16 #
17 # Foreign Keys
18 #
19 #  ways_changeset_id_fkey  (changeset_id => changesets.id)
20 #  ways_redaction_id_fkey  (redaction_id => redactions.id)
21 #
22
23 class OldWay < ActiveRecord::Base
24   include ConsistencyValidations
25   include ObjectMetadata
26
27   self.table_name = "ways"
28   self.primary_keys = "way_id", "version"
29
30   # note this needs to be included after the table name changes, or
31   # the queries generated by Redactable will use the wrong table name.
32   include Redactable
33
34   belongs_to :changeset
35   belongs_to :redaction
36   belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
37
38   has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version]
39   has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version]
40
41   validates :changeset, :presence => true, :associated => true
42   validates :timestamp, :presence => true
43   validates :visible, :inclusion => [true, false]
44
45   def self.from_way(way)
46     old_way = OldWay.new
47     old_way.visible = way.visible
48     old_way.changeset_id = way.changeset_id
49     old_way.timestamp = way.timestamp
50     old_way.way_id = way.id
51     old_way.version = way.version
52     old_way.nds = way.nds
53     old_way.tags = way.tags
54     old_way
55   end
56
57   def save_with_dependencies!
58     save!
59
60     tags.each do |k, v|
61       tag = OldWayTag.new
62       tag.k = k
63       tag.v = v
64       tag.way_id = way_id
65       tag.version = version
66       tag.save!
67     end
68
69     sequence = 1
70     nds.each do |n|
71       nd = OldWayNode.new
72       nd.id = [way_id, version, sequence]
73       nd.node_id = n
74       nd.save!
75       sequence += 1
76     end
77   end
78
79   def nds
80     @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
81   end
82
83   def tags
84     @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
85   end
86
87   attr_writer :nds
88
89   attr_writer :tags
90
91   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
92     el = XML::Node.new "way"
93     el["id"] = way_id.to_s
94
95     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
96
97     old_nodes.each do |nd| # FIXME: need to make sure they come back in the right order
98       node_el = XML::Node.new "nd"
99       node_el["ref"] = nd.node_id.to_s
100       el << node_el
101     end
102
103     add_tags_to_xml_node(el, old_tags)
104
105     el
106   end
107
108   # Read full version of old way
109   # For get_nodes_undelete, uses same nodes, even if they've moved since
110   # For get_nodes_revert,   allocates new ids
111   # Currently returns Potlatch-style array
112   # where [5] indicates whether latest version is usable as is (boolean)
113   # (i.e. is it visible? are we actually reverting to an earlier version?)
114
115   def get_nodes_undelete
116     nds.collect do |n|
117       node = Node.find(n)
118       [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
119     end
120   end
121
122   def get_nodes_revert(timestamp)
123     points = []
124     nds.each do |n|
125       oldnode = OldNode.where("node_id = ? AND timestamp <= ?", n, timestamp).unredacted.order("timestamp DESC").first
126       curnode = Node.find(n)
127       id = n
128       reuse = curnode.visible
129       if oldnode.lat != curnode.lat || oldnode.lon != curnode.lon || oldnode.tags != curnode.tags
130         # node has changed: if it's in other ways, give it a new id
131         if curnode.ways - [way_id]
132           id = -1
133           reuse = false
134         end
135       end
136       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
137     end
138     points
139   end
140
141   # Temporary method to match interface to ways
142   def way_nodes
143     old_nodes
144   end
145
146   # Pretend we're not in any relations
147   def containing_relation_members
148     []
149   end
150
151   # check whether this element is the latest version - that is,
152   # has the same version as its "current" counterpart.
153   def is_latest_version?
154     current_way.version == version
155   end
156 end