diff --git a/.overcommit.yml b/.overcommit.yml index ec20b597c..8c6e18a53 100644 --- a/.overcommit.yml +++ b/.overcommit.yml @@ -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' diff --git a/app/assets/javascripts/app/controllers/widget/tag.coffee b/app/assets/javascripts/app/controllers/widget/tag.coffee index c3e528172..38d1f39d6 100644 --- a/app/assets/javascripts/app/controllers/widget/tag.coffee +++ b/app/assets/javascripts/app/controllers/widget/tag.coffee @@ -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 diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index aaf3962cb..0284769e0 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/models/tag/item.rb b/app/models/tag/item.rb index 9686956e2..c45bafa80 100644 --- a/app/models/tag/item.rb +++ b/app/models/tag/item.rb @@ -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 diff --git a/spec/requests/tag_spec.rb b/spec/requests/tag_spec.rb index 52866f87d..fd1deb72f 100644 --- a/spec/requests/tag_spec.rb +++ b/spec/requests/tag_spec.rb @@ -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