source: subversion/sites/rails_port/app/models/old_node.rb @ 4144

Last change on this file since 4144 was 3269, checked in by tomhughes, 13 years ago

Close a number of holes in the API by making it validate changes
more carefully.

File size: 1.2 KB
Line 
1class OldNode < ActiveRecord::Base
2  set_table_name 'nodes'
3 
4  validates_presence_of :user_id, :timestamp
5  validates_inclusion_of :visible, :in => [ true, false ]
6  validates_numericality_of :latitude, :longitude
7  validate :validate_position
8
9  belongs_to :user
10
11  def validate_position
12    errors.add_to_base("Node is not in the world") unless in_world?
13  end
14
15  def in_world?
16    return false if self.latitude < -90 or self.latitude > 90
17    return false if self.longitude < -180 or self.longitude > 180
18    return true
19  end
20
21  def self.from_node(node)
22    old_node = OldNode.new
23    old_node.latitude = node.latitude
24    old_node.longitude = node.longitude
25    old_node.visible = node.visible
26    old_node.tags = node.tags
27    old_node.timestamp = node.timestamp
28    old_node.user_id = node.user_id
29    old_node.id = node.id
30    return old_node
31  end
32
33  def to_xml_node
34    el1 = XML::Node.new 'node'
35    el1['id'] = self.id.to_s
36    el1['lat'] = self.latitude.to_s
37    el1['lon'] = self.longitude.to_s
38    el1['user'] = self.user.display_name if self.user.data_public?
39    Node.split_tags(el1, self.tags)
40    el1['visible'] = self.visible.to_s
41    el1['timestamp'] = self.timestamp.xmlschema
42    return el1
43  end
44end
Note: See TracBrowser for help on using the repository browser.