# current_relations_changeset_id_fkey (changeset_id => changesets.id)
#
-class Relation < ActiveRecord::Base
+class Relation < ApplicationRecord
require "xml/libxml"
include ConsistencyValidations
include NotRedactable
- include ObjectMetadata
self.table_name = "current_relations"
belongs_to :changeset
- has_many :old_relations, -> { order(:version) }
+ has_many :old_relations, -> { order(:version) }, :inverse_of => :current_relation
- has_many :relation_members, -> { order(:sequence_id) }
+ has_many :relation_members, -> { order(:sequence_id) }, :inverse_of => :relation
has_many :relation_tags
has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
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?
end
def tags
- @tags ||= Hash[relation_tags.collect { |t| [t.k, t.v] }]
+ @tags ||= relation_tags.to_h { |t| [t.k, t.v] }
end
- attr_writer :members
-
- attr_writer :tags
+ attr_writer :members, :tags
def add_member(type, id, role)
@members ||= []
end
def preconditions_ok?(good_members = [])
+ raise OSM::APITooManyRelationMembersError.new(id, members.length, Settings.max_number_of_relation_members) if members.length > Settings.max_number_of_relation_members
+
# These are hastables that store an id in the index of all
# the nodes/way/relations that have already been added.
# If the member is valid and visible then we add it to the