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