has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
validates_presence_of :id, :on => :update
has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
validates_presence_of :id, :on => :update
validates_numericality_of :id, :on => :update, :integer_only => true
validates_numericality_of :changeset_id, :version, :integer_only => true
validates_associated :changeset
validates_numericality_of :id, :on => :update, :integer_only => true
validates_numericality_of :changeset_id, :version, :integer_only => true
validates_associated :changeset
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 }) }
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 }) }
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.string(xml)
- doc = p.parse
+ def self.from_xml(xml, create = false)
+ p = XML::Parser.string(xml)
+ doc = p.parse
- doc.find('//osm/relation').each do |pt|
- return Relation.from_xml_node(pt, create)
- end
- raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.")
- rescue LibXML::XML::Error, ArgumentError => ex
- raise OSM::APIBadXMLError.new("relation", xml, ex.message)
+ doc.find("//osm/relation").each do |pt|
+ return Relation.from_xml_node(pt, create)
+ fail OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.")
+ rescue LibXML::XML::Error, ArgumentError => ex
+ raise OSM::APIBadXMLError.new("relation", xml, ex.message)
- raise OSM::APIBadXMLError.new("relation", pt, "Version is required when updating") unless create or not pt['version'].nil?
- relation.version = pt['version']
- raise OSM::APIBadXMLError.new("relation", pt, "Changeset id is missing") if pt['changeset'].nil?
- relation.changeset_id = pt['changeset']
-
+ fail OSM::APIBadXMLError.new("relation", pt, "Version is required when updating") unless create || !pt["version"].nil?
+ relation.version = pt["version"]
+ fail OSM::APIBadXMLError.new("relation", pt, "Changeset id is missing") if pt["changeset"].nil?
+ relation.changeset_id = pt["changeset"]
+
- raise OSM::APIBadXMLError.new("relation", pt, "ID is required when updating") if pt['id'].nil?
- relation.id = pt['id'].to_i
- # .to_i will return 0 if there is no number that can be parsed.
+ fail OSM::APIBadXMLError.new("relation", pt, "ID is required when updating") if pt["id"].nil?
+ relation.id = pt["id"].to_i
+ # .to_i will return 0 if there is no number that can be parsed.
- pt.find('tag').each do |tag|
- raise OSM::APIBadXMLError.new("relation", pt, "tag is missing key") if tag['k'].nil?
- raise OSM::APIBadXMLError.new("relation", pt, "tag is missing value") if tag['v'].nil?
- relation.add_tag_keyval(tag['k'], tag['v'])
+ pt.find("tag").each do |tag|
+ fail OSM::APIBadXMLError.new("relation", pt, "tag is missing key") if tag["k"].nil?
+ fail OSM::APIBadXMLError.new("relation", pt, "tag is missing value") if tag["v"].nil?
+ relation.add_tag_keyval(tag["k"], tag["v"])
- # isn't done for a new relation then @members attribute will be nil,
- # and the members will be loaded from the database instead of being
+ # isn't done for a new relation then @members attribute will be nil,
+ # and the members will be loaded from the database instead of being
- raise OSM::APIBadXMLError.new("relation", pt, "The #{member['type']} is not allowed only, #{TYPES.inspect} allowed") unless TYPES.include? member['type']
+ fail OSM::APIBadXMLError.new("relation", pt, "The #{member['type']} is not allowed only, #{TYPES.inspect} allowed") unless TYPES.include? member["type"]
- #member_ref = member['ref']
- #member_role
- member['role'] ||= "" # Allow the upload to not include this, in which case we default to an empty string.
- logger.debug member['role']
- relation.add_member(member['type'].classify, member['ref'], member['role'])
+ # member_ref = member['ref']
+ # member_role
+ member["role"] ||= "" # Allow the upload to not include this, in which case we default to an empty string.
+ logger.debug member["role"]
+ relation.add_member(member["type"].classify, member["ref"], member["role"])
add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
- if p
- 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
+
+ next unless p
+
+ 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
# duplicate tags are now forbidden, so we can't allow values
# in the hash to be overwritten.
# duplicate tags are now forbidden, so we can't allow values
# in the hash to be overwritten.
# the element with given +type+ and +id+. this only works with nodes
# and ways at the moment, as they're the only elements to respond to
# the :bbox call.
def update_changeset_element(type, id)
element = Kernel.const_get(type.capitalize).find(id)
changeset.update_bbox! element.bbox
# the element with given +type+ and +id+. this only works with nodes
# and ways at the moment, as they're the only elements to respond to
# the :bbox call.
def update_changeset_element(type, id)
element = Kernel.const_get(type.capitalize).find(id)
changeset.update_bbox! element.bbox
# provide repeatable reads for the used-by checks. this means it
# shouldn't be possible to get race conditions.
Relation.transaction do
self.lock!
check_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
# provide repeatable reads for the used-by checks. this means it
# shouldn't be possible to get race conditions.
Relation.transaction do
self.lock!
check_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
- rel = RelationMember.joins(:relation).where("visible = ? AND member_type = 'Relation' and member_id = ? ", true, self.id).first
- raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is used in relation #{rel.relation.id}.") unless rel.nil?
+ rel = RelationMember.joins(:relation).where("visible = ? AND member_type = 'Relation' and member_id = ? ", true, id).first
+ fail OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is used in relation #{rel.relation.id}.") unless rel.nil?
- unless new_relation.preconditions_ok?(self.members)
- raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.")
+ unless new_relation.preconditions_ok?(members)
+ fail OSM::APIPreconditionFailedError.new("Cannot update relation #{id}: data or member data is invalid.")
def create_with_history(user)
check_create_consistency(self, user)
unless self.preconditions_ok?
def create_with_history(user)
check_create_consistency(self, user)
unless self.preconditions_ok?
# relevant hash table, with the value true as a cache.
# Thus if you have nodes with the ids of 50 and 1 already in the
# relation, then the hash table nodes would contain:
# => {50=>true, 1=>true}
# relevant hash table, with the value true as a cache.
# Thus if you have nodes with the ids of 50 and 1 already in the
# relation, then the hash table nodes would contain:
# => {50=>true, 1=>true}
# pre-set all existing members to good
good_members.each { |m| elements[m[0].downcase.to_sym][m[1]] = true }
# pre-set all existing members to good
good_members.each { |m| elements[m[0].downcase.to_sym][m[1]] = true }
- unless hash.key? m[1]
- # use reflection to look up the appropriate class
- model = Kernel.const_get(m[0].capitalize)
- # get the element with that ID
- element = model.where(:id => m[1]).first
-
- # and check that it is OK to use.
- unless element and element.visible? and element.preconditions_ok?
- raise OSM::APIPreconditionFailedError.new("Relation with id #{self.id} cannot be saved due to #{m[0]} with id #{m[1]}")
- end
- hash[m[1]] = true
+ next if hash.key? m[1]
+
+ # use reflection to look up the appropriate class
+ model = Kernel.const_get(m[0].capitalize)
+ # get the element with that ID
+ element = model.where(:id => m[1]).first
+
+ # and check that it is OK to use.
+ unless element && element.visible? && element.preconditions_ok?
+ fail OSM::APIPreconditionFailedError.new("Relation with id #{id} cannot be saved due to #{m[0]} with id #{m[1]}")
key = old_tag.k
# if we can match the tags we currently have to the list
# of old tags, then we never set the tags_changed flag. but
key = old_tag.k
# if we can match the tags we currently have to the list
# of old tags, then we never set the tags_changed flag. but
tags_changed |= (old_tag.v != tags[key])
# remove from the map, so that we can expect an empty map
tags_changed |= (old_tag.v != tags[key])
# remove from the map, so that we can expect an empty map
- tags_changed |= (not tags.empty?)
- RelationTag.delete_all(:relation_id => self.id)
- self.tags.each do |k,v|
+ tags_changed |= (!tags.empty?)
+ RelationTag.delete_all(:relation_id => id)
+ self.tags.each do |k, v|
# same pattern as before, but this time we're collecting the
# changed members in an array, as the bounding box updates for
# elements are per-element, not blanked on/off like for tags.
# same pattern as before, but this time we're collecting the
# changed members in an array, as the bounding box updates for
# elements are per-element, not blanked on/off like for tags.
# members may be in a different order and i don't feel like implementing
# a longest common subsequence algorithm to optimise this.
members = self.members
# members may be in a different order and i don't feel like implementing
# a longest common subsequence algorithm to optimise this.
members = self.members
- RelationMember.delete_all(:relation_id => self.id)
- members.each_with_index do |m,i|
+ RelationMember.delete_all(:relation_id => id)
+ members.each_with_index do |m, i|
# bounding box. this is similar to how the map call does things and is
# reasonable on the assumption that adding or removing members doesn't
# materially change the rest of the relation.
# bounding box. this is similar to how the map call does things and is
# reasonable on the assumption that adding or removing members doesn't
# materially change the rest of the relation.
- any_relations =
- changed_members.collect { |id,type| type == "relation" }.
- inject(false) { |b,s| b or s }
+ any_relations =
+ changed_members.collect { |_id, type| type == "relation" }
+ .inject(false) { |a, e| a || e }
# add all non-relation bounding boxes to the changeset
# FIXME: check for tag changes along with element deletions and
# make sure that the deleted element's bounding box is hit.
self.members
# add all non-relation bounding boxes to the changeset
# FIXME: check for tag changes along with element deletions and
# make sure that the deleted element's bounding box is hit.
self.members
- update_members.each do |type, id, role|
- if type != "Relation"
- update_changeset_element(type, id)
- end
+ update_members.each do |type, id, _role|
+ update_changeset_element(type, id) if type != "Relation"