From f7cb79b572ada1e2a1b2f3ee58081d6db584e6ec Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Sat, 16 Aug 2025 10:50:07 -0400 Subject: [PATCH] Use `normalizes` api for Tag display_name value --- app/models/tag.rb | 5 +++-- spec/models/tag_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index f9eb6bfd33e..2344834f567 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 @@ -112,8 +114,7 @@ class Tag < ApplicationRecord names = Array(name_or_names).map { |str| [normalize(str), str] }.uniq(&:first) names.map do |(normalized_name, display_name)| - tag = matching_name(normalized_name).first || create(name: normalized_name, - display_name: display_name.gsub(HASHTAG_INVALID_CHARS_RE, '')) + tag = matching_name(normalized_name).first || create(name: normalized_name, display_name:) yield tag if block_given? diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index d41d3a9e21e..a595ec128be 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -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 }