FEATURE: Make the tag_groups#search endpoint public. (#12643)

The method uses the "TagGroup#visible" method to respect the tag group visibility settings.
This commit is contained in:
Roman Rizzi
2021-04-08 14:23:13 -03:00
committed by GitHub
parent 26d7eedf4c
commit 8339b8f412
4 changed files with 88 additions and 15 deletions

View File

@@ -1,8 +1,8 @@
# frozen_string_literal: true
class TagGroupsController < ApplicationController
requires_login
before_action :ensure_staff
requires_login except: [:search]
before_action :ensure_staff, except: [:search]
skip_before_action :check_xhr, only: [:index, :show, :new]
before_action :fetch_tag_group, only: [:show, :update, :destroy]
@@ -61,15 +61,21 @@ class TagGroupsController < ApplicationController
end
def search
matches = if params[:q].present?
TagGroup.where('lower(name) ILIKE ?', "%#{params[:q].strip}%")
else
TagGroup.all
matches = TagGroup.includes(:tags).visible(guardian).all
if params[:q].present?
matches = matches.where('lower(name) ILIKE ?', "%#{params[:q].strip}%")
end
if params[:ids].present?
matches = matches.where(id: params[:ids])
end
matches = matches.order('name').limit(params[:limit] || 5)
render json: { results: matches.map { |x| { id: x.name, text: x.name } } }
render json: {
results: matches.map { |x| { name: x.name, tag_names: x.tags.base_tags.pluck(:name).sort } }
}
end
private