X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/6ec02bcdb21d258bb35a6100ac53f2d633d56ccc..18899f2867c85ea4bff8ecb188f57768300b4f8d:/app/models/way.rb diff --git a/app/models/way.rb b/app/models/way.rb index 3284c47f3..b11c225dd 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -17,20 +17,19 @@ # current_ways_changeset_id_fkey (changeset_id => changesets.id) # -class Way < ActiveRecord::Base +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 :old_ways, -> { order(:version) }, :inverse_of => :current_way - has_many :way_nodes, -> { order(:sequence_id) } + has_many :way_nodes, -> { order(:sequence_id) }, :inverse_of => :way has_many :nodes, :through => :way_nodes has_many :way_tags @@ -42,8 +41,6 @@ class Way < ActiveRecord::Base :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] @@ -52,19 +49,21 @@ class Way < ActiveRecord::Base scope :invisible, -> { where(:visible => false) } # Read in xml as text and return it's Way object representation - def self.from_xml(xml, create = false) + 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") - doc.find("//osm/way").each do |pt| - return Way.from_xml_node(pt, create) + if pt + Way.from_xml_node(pt, :create => create) + else + raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.") end - raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.") rescue LibXML::XML::Error, ArgumentError => e raise OSM::APIBadXMLError.new("way", xml, e.message) end - def self.from_xml_node(pt, create = false) + 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? @@ -111,12 +110,10 @@ class Way < ActiveRecord::Base end def tags - @tags ||= Hash[way_tags.collect { |t| [t.k, t.v] }] + @tags ||= way_tags.to_h { |t| [t.k, t.v] } end - attr_writer :nds - - attr_writer :tags + attr_writer :nds, :tags def add_nd_num(n) @nds ||= [] @@ -230,7 +227,7 @@ class Way < ActiveRecord::Base private def save_with_history! - t = Time.now.getutc + t = Time.now.utc self.version += 1 self.timestamp = t