Fixes #4095 - Trigger autocomplete when clicking the "Tags" field also without a search term.

This commit is contained in:
Dominik Klein 2022-05-23 12:27:43 +02:00
parent 08934daaf3
commit 757a9b274e
5 changed files with 35 additions and 4 deletions

View file

@ -15,10 +15,15 @@ PreCommit:
on_warn: fail
CoffeeLint:
# .coffeelint/rules/* not supported in YAML, specify all rules separately.
flags: ['--reporter=csv', '--rules', './.coffeelint/rules/detect_translatable_string.coffee']
flags:
[
'--reporter=csv',
'--rules',
'./.coffeelint/rules/detect_translatable_string.coffee',
]
enabled: true
on_warn: fail
exclude: public/assets/chat/**/*
exclude: 'public/assets/chat/**/*'
CustomScript:
enabled: true
description: 'Check if translation catalog is up-to-date'

View file

@ -57,7 +57,7 @@ class App.WidgetTag extends App.Controller
source = "#{App.Config.get('api_path')}/tag_search"
@$('.js-newTagInput').autocomplete(
source: source
minLength: 1
minLength: 0
response: (e, ui) =>
return if !ui
return if !ui.content

View file

@ -6,7 +6,8 @@ class TagsController < ApplicationController
# GET /api/v1/tag_search?term=abc
def search
list = Tag::Item.where('name_downcase LIKE ?', "%#{params[:term].strip.downcase}%").order(name: :asc).limit(params[:limit] || 10)
list = get_tag_list(params[:term], params[:limit] || 10)
results = []
list.each do |item|
result = {
@ -95,4 +96,13 @@ class TagsController < ApplicationController
render json: {}
end
private
def get_tag_list(term, limit)
if term.blank?
return Tag::Item.left_outer_joins(:tags).group(:id).order('COUNT(tags.tag_item_id) DESC, name ASC').limit(limit)
end
Tag::Item.where('name_downcase LIKE ?', "%#{term.strip.downcase}%").order(name: :asc).limit(limit)
end
end

View file

@ -4,6 +4,10 @@ class Tag::Item < ApplicationModel
validates :name, presence: true
before_save :fill_namedowncase
has_many :tags, foreign_key: 'tag_item_id',
inverse_of: :tag_item,
dependent: :destroy
=begin
lookup by name and create tag item

View file

@ -100,6 +100,18 @@ RSpec.describe Tag, type: :request do
include_examples 'no tag found using', search_term: '1foobar'
include_examples 'no tag found using', search_term: 'foobar2'
end
context 'without search term' do
before do
create_list(:tag, 2, tag_item: tags.last)
create_list(:tag, 1, tag_item: tags.first)
end
it 'most used is on first place without search term' do
get '/api/v1/tag_search', params: { term: '' }
expect(json_response.first['value']).to eq(tags.last.name)
end
end
end
end
end