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