From c8248de14caf2465db65f3243aedd51428080aeb Mon Sep 17 00:00:00 2001 From: Susanna Kiwala Date: Tue, 8 Oct 2024 12:25:46 -0500 Subject: [PATCH 1/2] Update feature uniqueness validation to take into account revision context --- server/app/models/feature.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/server/app/models/feature.rb b/server/app/models/feature.rb index 825c977ea..d84938129 100644 --- a/server/app/models/feature.rb +++ b/server/app/models/feature.rb @@ -26,7 +26,34 @@ class Feature < ApplicationRecord searchkick highlight: [:name, :aliases, :feature_type], callbacks: :async scope :search_import, -> { includes(:feature_aliases) } - validates :name, uniqueness: { scope: :feature_instance_type } + validate :unique_name_in_context + + def unique_name_in_context + base_query = self.class.where( + deprecated: false, + feature_instance_type: feature_instance_type, + name: name + ) + + duplicate_name = if in_revision_validation_context + base_query + .where.not(feature_instance_id: revision_target_id) + .exists? + else + if persisted? + base_query + .where.not(feature_instance_id: id) + .exists? + else + base_query + .exists? + end + end + + if duplicate_name + errors.add(:name, 'must be unique within a Feature Instance Type') + end + end def search_data aliases = feature_aliases.map(&:name) From 42673fc0cc81336c9da6ff5dda6479520d57087d Mon Sep 17 00:00:00 2001 From: Susanna Kiwala Date: Tue, 8 Oct 2024 12:53:18 -0500 Subject: [PATCH 2/2] Fix second clause --- server/app/models/feature.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/app/models/feature.rb b/server/app/models/feature.rb index d84938129..dde3eff4b 100644 --- a/server/app/models/feature.rb +++ b/server/app/models/feature.rb @@ -42,7 +42,7 @@ def unique_name_in_context else if persisted? base_query - .where.not(feature_instance_id: id) + .where.not(id: id) .exists? else base_query