Fix validation for multiple parent protocols for drafts [SCI-7994] (#5020)

* Fix validation for multiple parent protocols for drafts [SCI-7994]

* Fix validation for drafts [SCI-7994]

* Fix validation for drafts [SCI-7994]
This commit is contained in:
ajugo 2023-02-24 17:56:51 +01:00 committed by GitHub
parent ced0da6dbc
commit 95d04d11d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -66,7 +66,7 @@ class Protocol < ApplicationRecord
end
with_options if: :in_repository_draft? do
# Only one draft can exist for each protocol
validates :parent_id, uniqueness: { scope: :protocol_type }, if: -> { parent_id.present? }
validate :ensure_single_draft
validate :versions_same_name_constrain
end
with_options if: -> { in_repository? && !parent } do |protocol|
@ -744,4 +744,10 @@ class Protocol < ApplicationRecord
errors.add(:base, I18n.t('activerecord.errors.models.protocol.wrong_version_number'))
end
end
def ensure_single_draft
if parent&.draft && parent.draft.id != id
errors.add(:base, I18n.t('activerecord.errors.models.protocol.wrong_parent_draft_number'))
end
end
end

View file

@ -152,6 +152,7 @@ en:
wrong_parent_type: "Protocol can only be linked to published protocol!"
wrong_version_name: "Protocol versions should have same name!"
wrong_version_number: "Protocol version number should be sequential!"
wrong_parent_draft_number: "Procol can have only 1 draft"
attributes:
step_order:
invalid: "Invalid step order."