diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index be74190f803..bec832bdbd4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index bf2c2147b6d..226b0555b04 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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) diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 3920a7da43c..fbdb7c9505f 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -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