From 39ec57c1425b66980948275e76ef5f2059cdc391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Thu, 29 Jan 2026 22:48:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=82=BF=E3=82=B0=E8=A3=9C=E5=AE=8C?= =?UTF-8?q?=E3=81=8B=E3=82=89=200=20=E4=BB=B6=E3=82=92=E9=99=A4=E5=A4=96?= =?UTF-8?q?=EF=BC=88#109=EF=BC=89=20(#245)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #109 #109 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/245 --- backend/app/controllers/application_controller.rb | 11 +++++++++++ backend/app/controllers/tags_controller.rb | 4 +++- backend/spec/requests/tags_spec.rb | 7 ++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/app/controllers/application_controller.rb b/backend/app/controllers/application_controller.rb index 7c580b3..0d412e0 100644 --- a/backend/app/controllers/application_controller.rb +++ b/backend/app/controllers/application_controller.rb @@ -11,4 +11,15 @@ class ApplicationController < ActionController::API code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE'] @current_user = User.find_by(inheritance_code: code) end + + def bool? key, default: false + return default if params[key].nil? + + s = params[key].to_s.strip.downcase + if default + !(s.in?(['0', 'false', 'off', 'no'])) + else + s.in?(['', '1', 'true', 'on', 'yes']) + end + end end diff --git a/backend/app/controllers/tags_controller.rb b/backend/app/controllers/tags_controller.rb index d5e561f..1c5dcb2 100644 --- a/backend/app/controllers/tags_controller.rb +++ b/backend/app/controllers/tags_controller.rb @@ -16,7 +16,8 @@ class TagsController < ApplicationController q = params[:q].to_s.strip return render json: [] if q.blank? - with_nico = !(params[:nico].to_s.strip.downcase.in?(['0', 'false', 'off', 'no'])) + with_nico = bool?(:nico, default: true) + present_only = bool?(:present, default: true) alias_rows = TagName @@ -33,6 +34,7 @@ class TagsController < ApplicationController end base = Tag.joins(:tag_name).includes(:tag_name) + base = base.where('tags.post_count > 0') if present_only canonical_hit = base diff --git a/backend/spec/requests/tags_spec.rb b/backend/spec/requests/tags_spec.rb index c9aad4f..ca09879 100644 --- a/backend/spec/requests/tags_spec.rb +++ b/backend/spec/requests/tags_spec.rb @@ -4,8 +4,12 @@ require 'rails_helper' RSpec.describe 'Tags API', type: :request do let!(:tn) { TagName.create!(name: 'spec_tag') } - let!(:tag) { Tag.create!(tag_name: tn, category: 'general') } + let!(:tag) { Tag.create!(tag_name: tn, category: :general) } let!(:alias_tn) { TagName.create!(name: 'unko', canonical: tn) } + let!(:post) { Post.create!(url: 'https://example.com/unkounkounko') } + let!(:post_tag) { PostTag.create!(post:, tag:) } + let!(:tn2) { TagName.create!(name: 'unknown') } + let!(:tag2) { Tag.create!(tag_name: tn2, category: :general) } describe 'GET /tags' do it 'returns tags with name' do @@ -71,6 +75,7 @@ RSpec.describe 'Tags API', type: :request do expect(json.map { |t| t['name'] }).to include('spec_tag') t = json.find { |t| t['name'] == 'spec_tag' } expect(t['matched_alias']).to eq('unko') + expect(json.map { |t| t['name'] }).not_to include('unknown') end end