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