]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Change cursor to 'move' when hovering routing markers
[rails.git] / app / models / old_node.rb
1 class OldNode < ActiveRecord::Base
2   include GeoRecord
3   include ConsistencyValidations
4   include ObjectMetadata
5
6   self.table_name = "nodes"
7   self.primary_keys = "node_id", "version"
8
9   # note this needs to be included after the table name changes, or
10   # the queries generated by Redactable will use the wrong table name.
11   include Redactable
12
13   validates :changeset, :presence => true, :associated => true
14   validates :latitude, :presence => true,
15                        :numericality => { :integer_only => true }
16   validates :longitude, :presence => true,
17                         :numericality => { :integer_only => true }
18   validates :timestamp, :presence => true
19   validates :visible, :inclusion => [true, false]
20
21   validate :validate_position
22
23   belongs_to :changeset
24   belongs_to :redaction
25   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id"
26
27   has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version]
28
29   def validate_position
30     errors.add(:base, "Node is not in the world") unless in_world?
31   end
32
33   def self.from_node(node)
34     old_node = OldNode.new
35     old_node.latitude = node.latitude
36     old_node.longitude = node.longitude
37     old_node.visible = node.visible
38     old_node.tags = node.tags
39     old_node.timestamp = node.timestamp
40     old_node.changeset_id = node.changeset_id
41     old_node.node_id = node.id
42     old_node.version = node.version
43     old_node
44   end
45
46   def to_xml
47     doc = OSM::API.new.get_xml_doc
48     doc.root << to_xml_node
49     doc
50   end
51
52   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
53     el = XML::Node.new "node"
54     el["id"] = node_id.to_s
55
56     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
57
58     if self.visible?
59       el["lat"] = lat.to_s
60       el["lon"] = lon.to_s
61     end
62
63     add_tags_to_xml_node(el, old_tags)
64
65     el
66   end
67
68   def save_with_dependencies!
69     save!
70
71     tags.each do |k, v|
72       tag = OldNodeTag.new
73       tag.k = k
74       tag.v = v
75       tag.node_id = node_id
76       tag.version = version
77       tag.save!
78     end
79   end
80
81   def tags
82     @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
83   end
84
85   attr_writer :tags
86
87   def tags_as_hash
88     tags
89   end
90
91   # Pretend we're not in any ways
92   def ways
93     []
94   end
95
96   # Pretend we're not in any relations
97   def containing_relation_members
98     []
99   end
100
101   # check whether this element is the latest version - that is,
102   # has the same version as its "current" counterpart.
103   def is_latest_version?
104     current_node.version == version
105   end
106 end