X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/31edc7dff6f4305366d0f93d35ba3fb1956bb71f..7d2ff8e4cd7f223cbe5e31ed1d4a701fbed36cc3:/app/models/relation.rb diff --git a/app/models/relation.rb b/app/models/relation.rb index bcac9d04b..5ed838e10 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -17,7 +17,7 @@ # current_relations_changeset_id_fkey (changeset_id => changesets.id) # -class Relation < ActiveRecord::Base +class Relation < ApplicationRecord require "xml/libxml" include ConsistencyValidations @@ -37,11 +37,11 @@ class Relation < ActiveRecord::Base 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 } + :numericality => { :on => :update, :only_integer => true } validates :version, :presence => true, - :numericality => { :integer_only => true } + :numericality => { :only_integer => true } validates :changeset_id, :presence => true, - :numericality => { :integer_only => true } + :numericality => { :only_integer => true } validates :timestamp, :presence => true validates :changeset, :associated => true validates :visible, :inclusion => [true, false] @@ -54,19 +54,21 @@ class Relation < ActiveRecord::Base TYPES = %w[node way relation].freeze - 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/relation") - doc.find("//osm/relation").each do |pt| - return Relation.from_xml_node(pt, create) + if pt + Relation.from_xml_node(pt, :create => create) + else + raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.") end - raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.") rescue LibXML::XML::Error, ArgumentError => e raise OSM::APIBadXMLError.new("relation", xml, e.message) end - def self.from_xml_node(pt, create = false) + def self.from_xml_node(pt, create: false) relation = Relation.new raise OSM::APIBadXMLError.new("relation", pt, "Version is required when updating") unless create || !pt["version"].nil? @@ -121,31 +123,6 @@ class Relation < ActiveRecord::Base relation end - def to_xml - doc = OSM::API.new.get_xml_doc - doc.root << to_xml_node - doc - end - - def to_xml_node(changeset_cache = {}, user_display_name_cache = {}) - el = XML::Node.new "relation" - el["id"] = id.to_s - - add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache) - - relation_members.each do |member| - member_el = XML::Node.new "member" - member_el["type"] = member.member_type.downcase - member_el["ref"] = member.member_id.to_s - member_el["role"] = member.member_role - el << member_el - end - - add_tags_to_xml_node(el, relation_tags) - - el - end - # FIXME: is this really needed? def members @members ||= relation_members.map do |member| @@ -157,9 +134,7 @@ class Relation < ActiveRecord::Base @tags ||= Hash[relation_tags.collect { |t| [t.k, t.v] }] end - attr_writer :members - - attr_writer :tags + attr_writer :members, :tags def add_member(type, id, role) @members ||= []