Fix protocol name validation [SCI-8003]

This commit is contained in:
Anton 2023-02-24 10:15:23 +01:00
parent 8906bc25e7
commit dd27b13537

View file

@ -69,33 +69,22 @@ class Protocol < ApplicationRecord
validates :parent_id, uniqueness: { scope: :protocol_type }, if: -> { parent_id.present? }
validate :versions_same_name_constrain
end
with_options if: -> { in_repository? && active? && !parent } do |protocol|
with_options if: -> { in_repository? && !parent } do |protocol|
# Active protocol must have unique name inside its team
protocol
.validates_uniqueness_of :name, case_sensitive: false,
scope: :team,
conditions: lambda {
active.where(
where(
protocol_type: [
Protocol.protocol_types[:in_repository_published_original],
Protocol.protocol_types[:in_repository_draft]
]
],
parent_id: nil
)
}
end
with_options if: -> { in_repository? && archived? && !previous_version } do |protocol|
# Archived protocol must have unique name inside its team
protocol
.validates_uniqueness_of :name, case_sensitive: false,
scope: :team,
conditions: lambda {
archived.where(
protocol_type: [
Protocol.protocol_types[:in_repository_published_original],
Protocol.protocol_types[:in_repository_draft]
]
)
}
protocol.validates :archived_by, presence: true
protocol.validates :archived_on, presence: true
end