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