- require 'xml/libxml'
-
- belongs_to :user
-
- has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
- has_many :way_tags, :foreign_key => 'id'
-
- has_many :old_ways, :foreign_key => 'id', :order => 'version'
-
- set_table_name 'current_ways'
-
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.new
- p.string = xml
- doc = p.parse
-
- way = Way.new
-
- doc.find('//osm/way').each do |pt|
- if !create and pt['id'] != '0'
- way.id = pt['id'].to_i
- end
-
- if create
- way.timestamp = Time.now
- way.visible = true
- else
- if pt['timestamp']
- way.timestamp = Time.parse(pt['timestamp'])
- end
- end
-
- pt.find('tag').each do |tag|
- way.add_tag_keyval(tag['k'], tag['v'])
- end
-
- pt.find('nd').each do |nd|
- way.add_nd_num(nd['ref'])
- end
- end
- rescue
- way = nil
+ require "xml/libxml"
+
+ include ConsistencyValidations
+ include NotRedactable
+ include ObjectMetadata
+
+ self.table_name = "current_ways"
+
+ belongs_to :changeset
+
+ has_many :old_ways, -> { order(:version) }
+
+ has_many :way_nodes, -> { order(:sequence_id) }
+ has_many :nodes, :through => :way_nodes
+
+ has_many :way_tags
+
+ has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
+ has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
+
+ validates :id, :uniqueness => true, :presence => { :on => :update },
+ :numericality => { :on => :update, :integer_only => true }
+ validates :version, :presence => true,
+ :numericality => { :integer_only => true }
+ validates :changeset_id, :presence => true,
+ :numericality => { :integer_only => true }
+ validates :timestamp, :presence => true
+ validates :changeset, :associated => true
+ validates :visible, :inclusion => [true, false]
+
+ scope :visible, -> { where(:visible => true) }
+ scope :invisible, -> { where(:visible => false) }
+
+ # Read in xml as text and return it's Way object representation
+ def self.from_xml(xml, create = false)
+ p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
+ doc = p.parse
+
+ doc.find("//osm/way").each do |pt|
+ return Way.from_xml_node(pt, create)
+ end
+ raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.")
+ rescue LibXML::XML::Error, ArgumentError => ex
+ raise OSM::APIBadXMLError.new("way", xml, ex.message)
+ end
+
+ def self.from_xml_node(pt, create = false)
+ way = Way.new
+
+ raise OSM::APIBadXMLError.new("way", pt, "Version is required when updating") unless create || !pt["version"].nil?
+ way.version = pt["version"]
+ raise OSM::APIBadXMLError.new("way", pt, "Changeset id is missing") if pt["changeset"].nil?
+ way.changeset_id = pt["changeset"]
+
+ unless create
+ raise OSM::APIBadXMLError.new("way", pt, "ID is required when updating") if pt["id"].nil?
+ way.id = pt["id"].to_i
+ # .to_i will return 0 if there is no number that can be parsed.
+ # We want to make sure that there is no id with zero anyway
+ raise OSM::APIBadUserInput, "ID of way cannot be zero when updating." if way.id.zero?