- has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
-
- validates_presence_of :id, :on => :update
- validates_presence_of :timestamp,:version, :changeset_id
- validates_uniqueness_of :id
- validates_inclusion_of :visible, :in => [ true, false ]
- validates_numericality_of :id, :on => :update, :integer_only => true
- validates_numericality_of :changeset_id, :version, :integer_only => true
- validates_associated :changeset
-
- TYPES = ["node", "way", "relation"]
-
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.string(xml)
- doc = p.parse
-
- doc.find('//osm/relation').each do |pt|
- return Relation.from_xml_node(pt, create)
- end
- rescue LibXML::XML::Error => ex
- raise OSM::APIBadXMLError.new("relation", xml, ex.message)
+ 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 :timestamp, :presence => true
+ validates :changeset, :associated => true
+ validates :visible, :inclusion => [true, false]
+
+ scope :visible, -> { where(:visible => true) }
+ scope :invisible, -> { where(:visible => false) }
+ scope :nodes, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Node", :member_id => ids.flatten }) }
+ scope :ways, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Way", :member_id => ids.flatten }) }
+ scope :relations, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Relation", :member_id => ids.flatten }) }
+
+ TYPES = %w[node way relation].freeze
+
+ 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")
+
+ if pt
+ Relation.from_xml_node(pt, :create => create)
+ else
+ raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.")