X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/baea8dc10eb0399e37850cc5ab857f392f780f1f..f6141277d5a727fdec5c7bb26e35b17b1e6f495d:/app/models/relation.rb diff --git a/app/models/relation.rb b/app/models/relation.rb index 25564940b..69e2acfd1 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -27,9 +27,9 @@ class Relation < ApplicationRecord 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 @@ -39,8 +39,6 @@ class Relation < ApplicationRecord :numericality => { :on => :update, :only_integer => true } validates :version, :presence => true, :numericality => { :only_integer => true } - validates :changeset_id, :presence => true, - :numericality => { :only_integer => true } validates :timestamp, :presence => true validates :changeset, :associated => true validates :visible, :inclusion => [true, false] @@ -130,7 +128,7 @@ class Relation < ApplicationRecord end def tags - @tags ||= relation_tags.collect { |t| [t.k, t.v] }.to_h + @tags ||= relation_tags.to_h { |t| [t.k, t.v] } end attr_writer :members, :tags @@ -168,7 +166,7 @@ class Relation < ApplicationRecord # shouldn't be possible to get race conditions. Relation.transaction do lock! - check_consistency(self, new_relation, user) + check_update_element_consistency(self, new_relation, user) # This will check to see if this relation is used by another relation rel = RelationMember.joins(:relation).find_by("visible = ? AND member_type = 'Relation' and member_id = ? ", true, id) raise OSM::APIPreconditionFailedError, "The relation #{new_relation.id} is used in relation #{rel.relation.id}." unless rel.nil? @@ -184,7 +182,7 @@ class Relation < ApplicationRecord def update_from(new_relation, user) Relation.transaction do lock! - check_consistency(self, new_relation, user) + check_update_element_consistency(self, new_relation, user) raise OSM::APIPreconditionFailedError, "Cannot update relation #{id}: data or member data is invalid." unless new_relation.preconditions_ok?(members) self.changeset_id = new_relation.changeset_id @@ -197,7 +195,7 @@ class Relation < ApplicationRecord end def create_with_history(user) - check_create_consistency(self, user) + check_create_element_consistency(self, user) raise OSM::APIPreconditionFailedError, "Cannot create relation: data or member data is invalid." unless preconditions_ok? self.version = 0 @@ -206,6 +204,8 @@ class Relation < ApplicationRecord 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 @@ -234,7 +234,7 @@ class Relation < ApplicationRecord element = model.lock("for share").find_by(:id => m[1]) # and check that it is OK to use. - raise OSM::APIPreconditionFailedError, "Relation with id #{id} cannot be saved due to #{m[0]} with id #{m[1]}" unless element&.visible? && element&.preconditions_ok? + raise OSM::APIPreconditionFailedError, "Relation with id #{id} cannot be saved due to #{m[0]} with id #{m[1]}" unless element&.visible? && element.preconditions_ok? hash[m[1]] = true end @@ -263,7 +263,7 @@ class Relation < ApplicationRecord private def save_with_history! - t = Time.now.getutc + t = Time.now.utc self.version += 1 self.timestamp = t