From 866ee8ed9a3c4a15494d4c6aa172947e2ca1931c Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 17 Oct 2024 10:16:58 -0400 Subject: [PATCH] Use `Tag#formatted_name` where relevant --- app/helpers/settings_helper.rb | 2 +- app/models/featured_tag.rb | 2 ++ .../activitypub/actor_serializer.rb | 2 +- .../activitypub/hashtag_serializer.rb | 2 +- .../activitypub/note_serializer.rb | 2 +- app/views/tags/show.rss.ruby | 2 +- spec/models/tag_spec.rb | 20 +++++++++++++------ 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index fd631ce92e..223b1a7452 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -51,7 +51,7 @@ module SettingsHelper def post_link_to_featured_tag(tag) link_to( - "##{tag.display_name}", + tag.formatted_name, settings_featured_tags_path(featured_tag: { name: tag.name }), method: :post ) diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb index a4e7b7cf6f..ff6ac6a459 100644 --- a/app/models/featured_tag.rb +++ b/app/models/featured_tag.rb @@ -30,6 +30,8 @@ class FeaturedTag < ApplicationRecord scope :by_name, ->(name) { joins(:tag).where(tag: { name: HashtagNormalizer.new.normalize(name) }) } + delegate :formatted_name, to: :tag + LIMIT = 10 def sign? diff --git a/app/serializers/activitypub/actor_serializer.rb b/app/serializers/activitypub/actor_serializer.rb index f698e758e8..75e9b88e7e 100644 --- a/app/serializers/activitypub/actor_serializer.rb +++ b/app/serializers/activitypub/actor_serializer.rb @@ -182,7 +182,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer end def name - "##{object.name}" + object.formatted_name end end diff --git a/app/serializers/activitypub/hashtag_serializer.rb b/app/serializers/activitypub/hashtag_serializer.rb index 2b24eb8cc1..e33bf76802 100644 --- a/app/serializers/activitypub/hashtag_serializer.rb +++ b/app/serializers/activitypub/hashtag_serializer.rb @@ -12,7 +12,7 @@ class ActivityPub::HashtagSerializer < ActivityPub::Serializer end def name - "##{object.display_name}" + object.formatted_name end def href diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb index 7b29e6d69b..aabf5e83d4 100644 --- a/app/serializers/activitypub/note_serializer.rb +++ b/app/serializers/activitypub/note_serializer.rb @@ -287,7 +287,7 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer end def name - "##{object.name}" + object.formatted_name end end diff --git a/app/views/tags/show.rss.ruby b/app/views/tags/show.rss.ruby index 5a2b4fb567..cbbba926bc 100644 --- a/app/views/tags/show.rss.ruby +++ b/app/views/tags/show.rss.ruby @@ -1,7 +1,7 @@ # frozen_string_literal: true RSS::Builder.build do |doc| - doc.title("##{@tag.display_name}") + doc.title(@tag.formatted_name) doc.description(I18n.t('rss.descriptions.tag', hashtag: @tag.display_name)) doc.link(tag_url(@tag)) doc.last_build_date(@statuses.first.created_at) if @statuses.any? diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 18dd26be94..ba5bd6c10d 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -115,14 +115,22 @@ RSpec.describe Tag do end describe '#formatted_name' do - it 'returns name with a proceeding hash symbol' do - tag = Fabricate(:tag, name: 'foo') - expect(tag.formatted_name).to eq '#foo' + context 'when display name is not present' do + let(:tag) { Fabricate.build :tag, name: 'foo' } + + it 'returns name with a preceding hash symbol' do + expect(tag.formatted_name) + .to eq '#foo' + end end - it 'returns display_name with a proceeding hash symbol, if display name present' do - tag = Fabricate(:tag, name: 'foobar', display_name: 'FooBar') - expect(tag.formatted_name).to eq '#FooBar' + context 'when display name is present' do + let(:tag) { Fabricate.build :tag, name: 'foobar', display_name: 'FooBar' } + + it 'returns display_name with a preceding hash symbol' do + expect(tag.formatted_name) + .to eq '#FooBar' + end end end