FIX: Searchable user fields do not always have an integer name (#30223)

This commit is contained in:
Natalie Tay 2024-12-17 08:06:19 +08:00 committed by GitHub
parent a8bdc5f7c5
commit d43d8e0023
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 1 deletions

View File

@ -8,7 +8,10 @@ class UserCustomField < ActiveRecord::Base
scope :searchable,
-> do
joins(
"INNER JOIN user_fields ON user_fields.id = REPLACE(user_custom_fields.name, 'user_field_', '')::INTEGER AND user_fields.searchable IS TRUE AND user_custom_fields.name like 'user_field_%'",
"INNER JOIN user_fields ON user_fields.id = REPLACE(user_custom_fields.name, 'user_field_', '')::INTEGER",
).where("user_fields.searchable = TRUE").where(
"user_custom_fields.name ~ ?",
'^user_field_\\d+$',
)
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
RSpec.describe UserCustomField do
describe ".searchable" do
it "includes user_custom_fields with valid, searchable user_field references" do
Fabricate(:user_field, id: 123, searchable: true)
valid_user_custom_field = Fabricate(:user_custom_field, name: "user_field_123")
result = UserCustomField.searchable
expect(result).to include(valid_user_custom_field)
end
it "excludes user_custom_fields with non-searchable user_field references" do
Fabricate(:user_field, id: 456, searchable: false)
non_searchable_custom_field = Fabricate(:user_custom_field, name: "user_field_456")
result = UserCustomField.searchable
expect(result).not_to include(non_searchable_custom_field)
end
it "excludes user_custom_fields with invalid user_field references" do
invalid_user_custom_field = Fabricate(:user_custom_field, name: "user_field_invalid")
result = UserCustomField.searchable
expect(result).not_to include(invalid_user_custom_field)
end
it "excludes user_custom_fields with unrelated names" do
unrelated_custom_field = Fabricate(:user_custom_field, name: "cost_center")
result = UserCustomField.searchable
expect(result).not_to include(unrelated_custom_field)
end
end
end