]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Fix interpolation errors
[rails.git] / app / models / old_node.rb
1 class OldNode < ActiveRecord::Base
2   include GeoRecord
3   include ConsistencyValidations
4
5   self.table_name = "nodes"
6   self.primary_keys = "node_id", "version"
7
8   # note this needs to be included after the table name changes, or
9   # the queries generated by Redactable will use the wrong table name.
10   include Redactable
11
12   validates_presence_of :changeset_id, :timestamp
13   validates_inclusion_of :visible, :in => [ true, false ]
14   validates_numericality_of :latitude, :longitude
15   validate :validate_position
16   validates_associated :changeset
17
18   belongs_to :changeset
19   belongs_to :redaction
20   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id"
21
22   def validate_position
23     errors.add(:base, "Node is not in the world") unless in_world?
24   end
25
26   def self.from_node(node)
27     old_node = OldNode.new
28     old_node.latitude = node.latitude
29     old_node.longitude = node.longitude
30     old_node.visible = node.visible
31     old_node.tags = node.tags
32     old_node.timestamp = node.timestamp
33     old_node.changeset_id = node.changeset_id
34     old_node.node_id = node.id
35     old_node.version = node.version
36     return old_node
37   end
38   
39   def to_xml
40     doc = OSM::API.new.get_xml_doc
41     doc.root << to_xml_node()
42     return doc
43   end
44
45   def to_xml_node
46     el1 = XML::Node.new 'node'
47     el1['id'] = self.node_id.to_s
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     if self.visible?
57       el1['lat'] = self.lat.to_s
58       el1['lon'] = self.lon.to_s
59     end
60     
61     el1['changeset'] = self.changeset.id.to_s
62     if self.changeset.user.data_public?
63       el1['user'] = self.changeset.user.display_name
64       el1['uid'] = self.changeset.user.id.to_s
65     end
66
67     el1['visible'] = self.visible.to_s
68     el1['timestamp'] = self.timestamp.xmlschema
69     el1['version'] = self.version.to_s
70     
71     el1['redacted'] = self.redaction.id.to_s if self.redacted?
72
73     return el1
74   end
75
76   def save_with_dependencies!
77     save!
78     #not sure whats going on here
79     clear_aggregation_cache
80     clear_association_cache
81     #ok from here
82     @attributes.update(OldNode.where(:node_id => self.node_id, :timestamp => self.timestamp, :version => self.version).first.instance_variable_get('@attributes'))
83    
84     self.tags.each do |k,v|
85       tag = OldNodeTag.new
86       tag.k = k
87       tag.v = v
88       tag.node_id = self.node_id
89       tag.version = self.version
90       tag.save!
91     end
92   end
93
94   def tags
95     unless @tags
96       @tags = Hash.new
97       OldNodeTag.where(:node_id => self.node_id, :version => self.version).each do |tag|
98         @tags[tag.k] = tag.v
99       end
100     end
101     @tags = Hash.new unless @tags
102     @tags
103   end
104
105   def tags=(t)
106     @tags = t 
107   end
108
109   def tags_as_hash 
110     return self.tags
111   end 
112  
113   # Pretend we're not in any ways 
114   def ways 
115     return [] 
116   end 
117  
118   # Pretend we're not in any relations 
119   def containing_relation_members 
120     return [] 
121   end 
122
123   # check whether this element is the latest version - that is,
124   # has the same version as its "current" counterpart.
125   def is_latest_version?
126     current_node.version == self.version
127   end
128 end