]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Fix some more rubocop style issues
[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_presence_of :changeset_id, :timestamp
14   validates_inclusion_of :visible, :in => [true, false]
15   validates_numericality_of :latitude, :longitude
16   validate :validate_position
17   validates_associated :changeset
18
19   belongs_to :changeset
20   belongs_to :redaction
21   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id"
22
23   has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version]
24
25   def validate_position
26     errors.add(:base, "Node is not in the world") unless in_world?
27   end
28
29   def self.from_node(node)
30     old_node = OldNode.new
31     old_node.latitude = node.latitude
32     old_node.longitude = node.longitude
33     old_node.visible = node.visible
34     old_node.tags = node.tags
35     old_node.timestamp = node.timestamp
36     old_node.changeset_id = node.changeset_id
37     old_node.node_id = node.id
38     old_node.version = node.version
39     old_node
40   end
41
42   def to_xml
43     doc = OSM::API.new.get_xml_doc
44     doc.root << to_xml_node
45     doc
46   end
47
48   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
49     el = XML::Node.new "node"
50     el["id"] = node_id.to_s
51
52     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
53
54     if self.visible?
55       el["lat"] = lat.to_s
56       el["lon"] = lon.to_s
57     end
58
59     add_tags_to_xml_node(el, old_tags)
60
61     el
62   end
63
64   def save_with_dependencies!
65     save!
66
67     tags.each do |k, v|
68       tag = OldNodeTag.new
69       tag.k = k
70       tag.v = v
71       tag.node_id = node_id
72       tag.version = version
73       tag.save!
74     end
75   end
76
77   def tags
78     @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
79   end
80
81   attr_writer :tags
82
83   def tags_as_hash
84     tags
85   end
86
87   # Pretend we're not in any ways
88   def ways
89     []
90   end
91
92   # Pretend we're not in any relations
93   def containing_relation_members
94     []
95   end
96
97   # check whether this element is the latest version - that is,
98   # has the same version as its "current" counterpart.
99   def is_latest_version?
100     current_node.version == version
101   end
102 end