1 class OldNode < ActiveRecord::Base
3 include ConsistencyValidations
5 self.table_name = "nodes"
6 self.primary_keys = "node_id", "version"
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.
12 validates_presence_of :changeset_id, :timestamp
13 validates_inclusion_of :visible, :in => [ true, false ]
14 validates_numericality_of :latitude, :longitude
15 validate :validate_position
16 validates_associated :changeset
20 belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id"
23 errors.add(:base, "Node is not in the world") unless in_world?
26 def self.from_node(node)
27 old_node = OldNode.new
28 old_node.latitude = node.latitude
29 old_node.longitude = node.longitude
30 old_node.visible = node.visible
31 old_node.tags = node.tags
32 old_node.timestamp = node.timestamp
33 old_node.changeset_id = node.changeset_id
34 old_node.node_id = node.id
35 old_node.version = node.version
40 doc = OSM::API.new.get_xml_doc
41 doc.root << to_xml_node()
45 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
46 el1 = XML::Node.new 'node'
47 el1['id'] = self.node_id.to_s
48 el1['version'] = self.version.to_s
49 el1['changeset'] = self.changeset_id.to_s
52 el1['lat'] = self.lat.to_s
53 el1['lon'] = self.lon.to_s
56 if changeset_cache.key?(self.changeset_id)
57 # use the cache if available
59 changeset_cache[self.changeset_id] = self.changeset.user_id
62 user_id = changeset_cache[self.changeset_id]
64 if user_display_name_cache.key?(user_id)
65 # use the cache if available
66 elsif self.changeset.user.data_public?
67 user_display_name_cache[user_id] = self.changeset.user.display_name
69 user_display_name_cache[user_id] = nil
72 if not user_display_name_cache[user_id].nil?
73 el1['user'] = user_display_name_cache[user_id]
74 el1['uid'] = user_id.to_s
77 self.tags.each do |k,v|
78 el2 = XML::Node.new('tag')
84 el1['visible'] = self.visible.to_s
85 el1['timestamp'] = self.timestamp.xmlschema
87 el1['redacted'] = self.redaction.id.to_s if self.redacted?
92 def save_with_dependencies!
94 #not sure whats going on here
95 clear_aggregation_cache
96 clear_association_cache
98 @attributes.update(OldNode.where(:node_id => self.node_id, :timestamp => self.timestamp, :version => self.version).first.instance_variable_get('@attributes'))
100 self.tags.each do |k,v|
104 tag.node_id = self.node_id
105 tag.version = self.version
113 OldNodeTag.where(:node_id => self.node_id, :version => self.version).each do |tag|
117 @tags = Hash.new unless @tags
129 # Pretend we're not in any ways
134 # Pretend we're not in any relations
135 def containing_relation_members
139 # check whether this element is the latest version - that is,
140 # has the same version as its "current" counterpart.
141 def is_latest_version?
142 current_node.version == self.version