FIX: Better match when searching for groups.

This commit is contained in:
Guo Xiang Tan 2017-11-02 10:20:14 +08:00
parent ab2a5cef38
commit edf4af608e
3 changed files with 25 additions and 23 deletions

View File

@ -710,24 +710,18 @@ class UsersController < ApplicationController
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
if params[:include_groups] == "true"
to_render[:groups] = Group.search_group(term).map do |m|
{ name: m.name, full_name: m.full_name }
end
end
if current_user
groups =
groups =
if current_user
if params[:include_mentionable_groups] == 'true'
Group.mentionable(current_user)
elsif params[:include_messageable_groups] == 'true'
Group.messageable(current_user)
end
if groups
to_render[:groups] = groups.where("name ILIKE :term_like", term_like: "#{term}%")
.map { |m| { name: m.name, full_name: m.full_name } }
end
if groups || params[:include_groups] == "true"
to_render[:groups] = Group.search_groups(term, groups: groups)
.map { |m| { name: m.name, full_name: m.full_name } }
end
render json: to_render

View File

@ -369,10 +369,14 @@ class Group < ActiveRecord::Base
lookup_group(name) || refresh_automatic_group!(name)
end
def self.search_group(name)
Group.where(visibility_level: visibility_levels[:public]).where(
"name ILIKE :term_like OR full_name ILIKE :term_like", term_like: "#{name}%"
)
def self.search_groups(name, groups: nil)
query = groups || Group
query
.where(visibility_level: visibility_levels[:public])
.where(
"name ILIKE :term_like OR full_name ILIKE :term_like", term_like: "%#{name}%"
)
end
def self.lookup_group(name)

View File

@ -556,17 +556,21 @@ describe Group do
end
end
describe '.search_group' do
let(:group) { Fabricate(:group, name: 'tEsT', full_name: 'eSTt') }
describe '.search_groups' do
let(:group) { Fabricate(:group, name: 'tEsT_more_things', full_name: 'Abc something awesome') }
it 'should return the right groups' do
group
expect(Group.search_group('te')).to eq([group])
expect(Group.search_group('TE')).to eq([group])
expect(Group.search_group('es')).to eq([group])
expect(Group.search_group('ES')).to eq([group])
expect(Group.search_group('test2')).to eq([])
expect(Group.search_groups('te')).to eq([group])
expect(Group.search_groups('TE')).to eq([group])
expect(Group.search_groups('es')).to eq([group])
expect(Group.search_groups('ES')).to eq([group])
expect(Group.search_groups('ngs')).to eq([group])
expect(Group.search_groups('sOmEthi')).to eq([group])
expect(Group.search_groups('abc')).to eq([group])
expect(Group.search_groups('sOmEthi')).to eq([group])
expect(Group.search_groups('test2')).to eq([])
end
end