# current_relations_changeset_id_fkey (changeset_id => changesets.id)
#
-class Relation < ActiveRecord::Base
+class Relation < ApplicationRecord
require "xml/libxml"
include ConsistencyValidations
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]
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)
+ 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
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|
@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 ||= []