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