]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
5d9c6d7ef31d7c8ecf57d738b92ea82021ff266e
[rails.git] / app / models / old_node.rb
1 class OldNode < GeoRecord
2   set_primary_keys :id, :version
3   set_table_name 'nodes'
4   
5   validates_presence_of :user_id, :timestamp
6   validates_inclusion_of :visible, :in => [ true, false ]
7   validates_numericality_of :latitude, :longitude
8   validate :validate_position
9
10   belongs_to :user
11  
12   def validate_position
13     errors.add_to_base("Node is not in the world") unless in_world?
14   end
15
16   def in_world?
17     return false if self.lat < -90 or self.lat > 90
18     return false if self.lon < -180 or self.lon > 180
19     return true
20   end
21
22   def self.from_node(node)
23     old_node = OldNode.new
24     old_node.latitude = node.latitude
25     old_node.longitude = node.longitude
26     old_node.visible = node.visible
27     old_node.tags = node.tags
28     old_node.timestamp = node.timestamp
29     old_node.user_id = node.user_id
30     old_node.id = node.id
31     old_node.version = node.version
32     return old_node
33   end
34   
35   def to_xml
36     doc = OSM::API.new.get_xml_doc
37     doc.root << to_xml_node()
38     return doc
39   end
40
41   def to_xml_node
42     el1 = XML::Node.new 'node'
43     el1['id'] = self.id.to_s
44     el1['lat'] = self.lat.to_s
45     el1['lon'] = self.lon.to_s
46     el1['user'] = self.user.display_name if self.user.data_public?
47
48     self.tags.each do |k,v|
49       el2 = XML::Node.new('tag')
50       el2['k'] = k.to_s
51       el2['v'] = v.to_s
52       el1 << el2
53     end
54
55     el1['visible'] = self.visible.to_s
56     el1['timestamp'] = self.timestamp.xmlschema
57     el1['version'] = self.version.to_s
58     return el1
59   end
60
61   def save_with_dependencies!
62     save!
63     #not sure whats going on here
64     clear_aggregation_cache
65     clear_association_cache
66     #ok from here
67     @attributes.update(OldNode.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
68    
69     self.tags.each do |k,v|
70       tag = OldNodeTag.new
71       tag.k = k
72       tag.v = v
73       tag.id = self.id
74       tag.version = self.version
75       tag.save!
76     end
77   end
78
79   def tags
80     unless @tags
81         @tags = Hash.new
82         OldNodeTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
83             @tags[tag.k] = tag.v
84         end
85     end
86     @tags = Hash.new unless @tags
87     @tags
88   end
89
90   def tags=(t)
91     @tags = t 
92   end 
93
94 end