-class Way < ActiveRecord::Base
- 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
+# == Schema Information
+#
+# Table name: current_ways
+#
+# id :bigint(8) not null, primary key
+# changeset_id :bigint(8) not null
+# timestamp :datetime not null
+# visible :boolean not null
+# version :bigint(8) not null
+#
+# Indexes
+#
+# current_ways_timestamp_idx (timestamp)
+#
+# Foreign Keys
+#
+# current_ways_changeset_id_fkey (changeset_id => changesets.id)
+#
+
+class Way < ApplicationRecord
+ 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, :only_integer => true }
+ validates :version, :presence => true,
+ :numericality => { :only_integer => true }
+ validates :changeset_id, :presence => true,
+ :numericality => { :only_integer => 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
+ pt = doc.find_first("//osm/way")
+
+ if pt
+ Way.from_xml_node(pt, create)
+ else
+ raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.")