Add not_featured_by scope to Tag (#28815)

This commit is contained in:
Matt Jankowski 2024-04-17 06:05:38 -04:00 committed by GitHub
parent 1d3ecd3fba
commit 650c548c31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 6 deletions

View file

@ -12,10 +12,6 @@ class Api::V1::FeaturedTags::SuggestionsController < Api::BaseController
private private
def set_recently_used_tags def set_recently_used_tags
@recently_used_tags = Tag.recently_used(current_account).where.not(id: featured_tag_ids).limit(10) @recently_used_tags = Tag.suggestions_for_account(current_account).limit(10)
end
def featured_tag_ids
current_account.featured_tags.pluck(:tag_id)
end end
end end

View file

@ -38,7 +38,7 @@ class Settings::FeaturedTagsController < Settings::BaseController
end end
def set_recently_used_tags 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 end
def featured_tag_params def featured_tag_params

View file

@ -53,6 +53,8 @@ class Tag < ApplicationRecord
scope :listable, -> { where(listable: [true, nil]) } scope :listable, -> { where(listable: [true, nil]) }
scope :trendable, -> { Setting.trendable_by_default ? where(trendable: [true, nil]) : where(trendable: true) } scope :trendable, -> { Setting.trendable_by_default ? where(trendable: [true, nil]) : where(trendable: true) }
scope :not_trendable, -> { where(trendable: false) } 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| scope :recently_used, lambda { |account|
joins(:statuses) joins(:statuses)
.where(statuses: { id: account.statuses.select(:id).limit(RECENT_STATUS_LIMIT) }) .where(statuses: { id: account.statuses.select(:id).limit(RECENT_STATUS_LIMIT) })

View file

@ -142,6 +142,25 @@ RSpec.describe Tag do
end end
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 describe '.matches_name' do
it 'returns tags for multibyte case-insensitive names' do it 'returns tags for multibyte case-insensitive names' do
upcase_string = 'abcABCやゆよ' upcase_string = 'abcABCやゆよ'