Use normalizes API for Tag display_name value (#35797)

This commit is contained in:
Matt Jankowski
2025-12-03 10:39:01 -05:00
committed by GitHub
parent b3b5bf26d1
commit 7c730e9041
2 changed files with 8 additions and 4 deletions

View File

@@ -67,6 +67,8 @@ class Tag < ApplicationRecord
}
scope :matches_name, ->(term) { where(arel_table[:name].lower.matches(arel_table.lower("#{sanitize_sql_like(Tag.normalize(term))}%"), nil, true)) } # Search with case-sensitive to use B-tree index
normalizes :display_name, with: ->(value) { value.gsub(HASHTAG_INVALID_CHARS_RE, '') }
update_index('tags', :self)
def to_param
@@ -113,10 +115,7 @@ class Tag < ApplicationRecord
names.map do |(normalized_name, display_name)|
tag = begin
matching_name(normalized_name).first || create!(
name: normalized_name,
display_name: display_name.gsub(HASHTAG_INVALID_CHARS_RE, '')
)
matching_name(normalized_name).first || create!(name: normalized_name, display_name:)
rescue ActiveRecord::RecordNotUnique
find_normalized(normalized_name)
end

View File

@@ -57,6 +57,11 @@ RSpec.describe Tag do
end
end
describe 'Normalizations' do
it { is_expected.to normalize(:display_name).from('#HelloWorld').to('HelloWorld') }
it { is_expected.to normalize(:display_name).from('Hello❤World').to('HelloWorld') }
end
describe 'HASHTAG_RE' do
subject { described_class::HASHTAG_RE }