Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update feature uniqueness validation to take into account revision context #1124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion server/app/models/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the revision target id here will always be the feature id, not the feature instance id, but we can double check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made that change specifically because I was running into this issue where the revision_target_id was the feature_instance_id when I was testing fusion variant coordinate revisions locally. However, maybe that is not intended and the revision should capture the feature id?

.exists?
else
if persisted?
base_query
.where.not(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)
Expand Down
Loading