mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 02:49:29 +08:00
FIX: Better match when searching for groups.
This commit is contained in:
parent
ab2a5cef38
commit
edf4af608e
|
@ -710,24 +710,18 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
|
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
|
||||||
|
|
||||||
if params[:include_groups] == "true"
|
groups =
|
||||||
to_render[:groups] = Group.search_group(term).map do |m|
|
if current_user
|
||||||
{ name: m.name, full_name: m.full_name }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if current_user
|
|
||||||
groups =
|
|
||||||
if params[:include_mentionable_groups] == 'true'
|
if params[:include_mentionable_groups] == 'true'
|
||||||
Group.mentionable(current_user)
|
Group.mentionable(current_user)
|
||||||
elsif params[:include_messageable_groups] == 'true'
|
elsif params[:include_messageable_groups] == 'true'
|
||||||
Group.messageable(current_user)
|
Group.messageable(current_user)
|
||||||
end
|
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
|
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
|
end
|
||||||
|
|
||||||
render json: to_render
|
render json: to_render
|
||||||
|
|
|
@ -369,10 +369,14 @@ class Group < ActiveRecord::Base
|
||||||
lookup_group(name) || refresh_automatic_group!(name)
|
lookup_group(name) || refresh_automatic_group!(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search_group(name)
|
def self.search_groups(name, groups: nil)
|
||||||
Group.where(visibility_level: visibility_levels[:public]).where(
|
query = groups || Group
|
||||||
"name ILIKE :term_like OR full_name ILIKE :term_like", term_like: "#{name}%"
|
|
||||||
)
|
query
|
||||||
|
.where(visibility_level: visibility_levels[:public])
|
||||||
|
.where(
|
||||||
|
"name ILIKE :term_like OR full_name ILIKE :term_like", term_like: "%#{name}%"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.lookup_group(name)
|
def self.lookup_group(name)
|
||||||
|
|
|
@ -556,17 +556,21 @@ describe Group do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.search_group' do
|
describe '.search_groups' do
|
||||||
let(:group) { Fabricate(:group, name: 'tEsT', full_name: 'eSTt') }
|
let(:group) { Fabricate(:group, name: 'tEsT_more_things', full_name: 'Abc something awesome') }
|
||||||
|
|
||||||
it 'should return the right groups' do
|
it 'should return the right groups' do
|
||||||
group
|
group
|
||||||
|
|
||||||
expect(Group.search_group('te')).to eq([group])
|
expect(Group.search_groups('te')).to eq([group])
|
||||||
expect(Group.search_group('TE')).to eq([group])
|
expect(Group.search_groups('TE')).to eq([group])
|
||||||
expect(Group.search_group('es')).to eq([group])
|
expect(Group.search_groups('es')).to eq([group])
|
||||||
expect(Group.search_group('ES')).to eq([group])
|
expect(Group.search_groups('ES')).to eq([group])
|
||||||
expect(Group.search_group('test2')).to eq([])
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user