From 650c548c31b993990f38c9a29570bf7c82e4b15e Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 17 Apr 2024 06:05:38 -0400 Subject: [PATCH] Add `not_featured_by` scope to Tag (#28815) --- .../featured_tags/suggestions_controller.rb | 6 +----- .../settings/featured_tags_controller.rb | 2 +- app/models/tag.rb | 2 ++ spec/models/tag_spec.rb | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/featured_tags/suggestions_controller.rb b/app/controllers/api/v1/featured_tags/suggestions_controller.rb index 4f732ed2d5..9c72e4380d 100644 --- a/app/controllers/api/v1/featured_tags/suggestions_controller.rb +++ b/app/controllers/api/v1/featured_tags/suggestions_controller.rb @@ -12,10 +12,6 @@ class Api::V1::FeaturedTags::SuggestionsController < Api::BaseController private def set_recently_used_tags - @recently_used_tags = Tag.recently_used(current_account).where.not(id: featured_tag_ids).limit(10) - end - - def featured_tag_ids - current_account.featured_tags.pluck(:tag_id) + @recently_used_tags = Tag.suggestions_for_account(current_account).limit(10) end end diff --git a/app/controllers/settings/featured_tags_controller.rb b/app/controllers/settings/featured_tags_controller.rb index c384402650..90c112e219 100644 --- a/app/controllers/settings/featured_tags_controller.rb +++ b/app/controllers/settings/featured_tags_controller.rb @@ -38,7 +38,7 @@ class Settings::FeaturedTagsController < Settings::BaseController end def set_recently_used_tags - @recently_used_tags = Tag.recently_used(current_account).where.not(id: @featured_tags.map(&:id)).limit(10) + @recently_used_tags = Tag.suggestions_for_account(current_account).limit(10) end def featured_tag_params diff --git a/app/models/tag.rb b/app/models/tag.rb index f2168ae904..58baa48c05 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -53,6 +53,8 @@ class Tag < ApplicationRecord scope :listable, -> { where(listable: [true, nil]) } scope :trendable, -> { Setting.trendable_by_default ? where(trendable: [true, nil]) : where(trendable: true) } scope :not_trendable, -> { where(trendable: false) } + scope :suggestions_for_account, ->(account) { recently_used(account).not_featured_by(account) } + scope :not_featured_by, ->(account) { where.not(id: account.featured_tags.select(:tag_id)) } scope :recently_used, lambda { |account| joins(:statuses) .where(statuses: { id: account.statuses.select(:id).limit(RECENT_STATUS_LIMIT) }) diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 5a1d620884..4c2bdd52f4 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -142,6 +142,25 @@ RSpec.describe Tag do end end + describe '.not_featured_by' do + let!(:account) { Fabricate(:account) } + let!(:fun) { Fabricate(:tag, name: 'fun') } + let!(:games) { Fabricate(:tag, name: 'games') } + + before do + Fabricate :featured_tag, account: account, name: 'games' + Fabricate :featured_tag, name: 'fun' + end + + it 'returns tags not featured by the account' do + results = described_class.not_featured_by(account) + + expect(results) + .to include(fun) + .and not_include(games) + end + end + describe '.matches_name' do it 'returns tags for multibyte case-insensitive names' do upcase_string = 'abcABCabcABCやゆよ'