]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
changing the belongs_to and has_many items in the models to reflect the new foriegn...
[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 :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.user_id = node.user_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['user'] = self.user.display_name if self.user.data_public?
42
43     self.tags.each do |k,v|
44       el2 = XML::Node.new('tag')
45       el2['k'] = k.to_s
46       el2['v'] = v.to_s
47       el1 << el2
48     end
49
50     el1['visible'] = self.visible.to_s
51     el1['timestamp'] = self.timestamp.xmlschema
52     el1['version'] = self.version.to_s
53     return el1
54   end
55
56   def save_with_dependencies!
57     save!
58     #not sure whats going on here
59     clear_aggregation_cache
60     clear_association_cache
61     #ok from here
62     @attributes.update(OldNode.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
63    
64     self.tags.each do |k,v|
65       tag = OldNodeTag.new
66       tag.k = k
67       tag.v = v
68       tag.id = self.id
69       tag.version = self.version
70       tag.save!
71     end
72   end
73
74   def tags
75     unless @tags
76         @tags = Hash.new
77         OldNodeTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
78             @tags[tag.k] = tag.v
79         end
80     end
81     @tags = Hash.new unless @tags
82     @tags
83   end
84
85   def tags=(t)
86     @tags = t 
87   end
88
89   def tags_as_hash 
90     hash = {} 
91     Tags.split(self.tags) do |k,v| 
92       hash[k] = v 
93     end 
94     hash 
95   end 
96  
97   # Pretend we're not in any ways 
98   def ways 
99     return [] 
100   end 
101  
102   # Pretend we're not in any relations 
103   def containing_relation_members 
104     return [] 
105   end 
106 end