| @@ -11,4 +11,15 @@ class ApplicationController < ActionController::API | |||||
| code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE'] | code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE'] | ||||
| @current_user = User.find_by(inheritance_code: code) | @current_user = User.find_by(inheritance_code: code) | ||||
| end | 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 | end | ||||
| @@ -16,8 +16,8 @@ class TagsController < ApplicationController | |||||
| q = params[:q].to_s.strip | q = params[:q].to_s.strip | ||||
| return render json: [] if q.blank? | return render json: [] if q.blank? | ||||
| with_nico = !(params[:nico].to_s.strip.downcase.in?(['0', 'false', 'off', 'no'])) | |||||
| with_empty = params[:empty].to_s.strip.downcase.in?(['1', 'true', 'on', 'yes']) | |||||
| with_nico = bool?(:nico, default: true) | |||||
| present_only = bool?(:present, default: true) | |||||
| alias_rows = | alias_rows = | ||||
| TagName | TagName | ||||
| @@ -34,7 +34,7 @@ class TagsController < ApplicationController | |||||
| end | end | ||||
| base = Tag.joins(:tag_name).includes(:tag_name) | base = Tag.joins(:tag_name).includes(:tag_name) | ||||
| base = base.where('tags.post_count > 0') unless with_empty | |||||
| base = base.where('tags.post_count > 0') if present_only | |||||
| canonical_hit = | canonical_hit = | ||||
| base | base | ||||
| @@ -4,8 +4,12 @@ require 'rails_helper' | |||||
| RSpec.describe 'Tags API', type: :request do | RSpec.describe 'Tags API', type: :request do | ||||
| let!(:tn) { TagName.create!(name: 'spec_tag') } | 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!(: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 | describe 'GET /tags' do | ||||
| it 'returns tags with name' 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') | expect(json.map { |t| t['name'] }).to include('spec_tag') | ||||
| t = json.find { |t| t['name'] == 'spec_tag' } | t = json.find { |t| t['name'] == 'spec_tag' } | ||||
| expect(t['matched_alias']).to eq('unko') | expect(t['matched_alias']).to eq('unko') | ||||
| expect(json.map { |t| t['name'] }).not_to include('unknown') | |||||
| end | end | ||||
| end | end | ||||