]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Add SOTM advert
[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     return old_node
40   end
41   
42   def to_xml
43     doc = OSM::API.new.get_xml_doc
44     doc.root << to_xml_node()
45     return doc
46   end
47
48   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
49     el = XML::Node.new 'node'
50     el['id'] = self.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'] = self.lat.to_s
56       el['lon'] = self.lon.to_s
57     end
58
59     add_tags_to_xml_node(el, self.old_tags)
60
61     return el
62   end
63
64   def save_with_dependencies!
65     save!
66     #not sure whats going on here
67     clear_aggregation_cache
68     clear_association_cache
69     #ok from here
70     @attributes.update(OldNode.where(:node_id => self.node_id, :timestamp => self.timestamp, :version => self.version).first.instance_variable_get('@attributes'))
71    
72     self.tags.each do |k,v|
73       tag = OldNodeTag.new
74       tag.k = k
75       tag.v = v
76       tag.node_id = self.node_id
77       tag.version = self.version
78       tag.save!
79     end
80   end
81
82   def tags
83     @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
84   end
85
86   def tags=(t)
87     @tags = t 
88   end
89
90   def tags_as_hash 
91     return self.tags
92   end 
93  
94   # Pretend we're not in any ways 
95   def ways 
96     return [] 
97   end 
98  
99   # Pretend we're not in any relations 
100   def containing_relation_members 
101     return [] 
102   end 
103
104   # check whether this element is the latest version - that is,
105   # has the same version as its "current" counterpart.
106   def is_latest_version?
107     current_node.version == self.version
108   end
109 end