Commit fa276e30 authored by Maxim Rydkin's avatar Maxim Rydkin

move `find_users` method into finder and add `load_group` method

parent 20ff87a2
......@@ -3,10 +3,10 @@ class AutocompleteController < ApplicationController
skip_before_action :authenticate_user!, only: [:users, :award_emojis]
before_action :load_project, only: [:users]
before_action :find_users, only: [:users]
before_action :load_group, only: [:users]
def users
@users = AutocompleteUsersFinder.new(params: params, current_user: current_user, users: @users).execute
@users = AutocompleteUsersFinder.new(params: params, current_user: current_user, project: @project, group: @group).execute
render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end
......@@ -39,25 +39,13 @@ class AutocompleteController < ApplicationController
private
def find_users
@users =
if @project
user_ids = @project.team.users.pluck(:id)
if params[:author_id].present?
user_ids << params[:author_id]
end
User.where(id: user_ids)
elsif params[:group_id].present?
def load_group
@group ||= begin
if @project.blank? && params[:group_id].present?
group = Group.find(params[:group_id])
return render_404 unless can?(current_user, :read_group, group)
group.users
elsif current_user
User.all
else
User.none
group
end
end
end
......
class AutocompleteUsersFinder
attr_reader :current_user, :users, :search, :skip_users, :page,
:per_page, :author_id, :params
attr_reader :current_user, :project, :group, :search, :skip_users,
:page, :per_page, :author_id, :params
def initialize(params:, current_user:, users: nil)
def initialize(params:, current_user:, project:, group:)
@current_user = current_user
@users = users
@project = project
@group = group
@search = params[:search]
@skip_users = params[:skip_users]
@page = params[:page]
......@@ -14,7 +15,7 @@ class AutocompleteUsersFinder
end
def execute
items = users || User.none
items = find_users
items = items.active
items = items.reorder(:name)
items = items.search(search) if search.present?
......@@ -39,4 +40,21 @@ class AutocompleteUsersFinder
items
end
private
def find_users
return users_from_project if project
return group.users if group
return User.all if current_user
User.none
end
def users_from_project
user_ids = project.team.users.pluck(:id)
user_ids << author_id if author_id.present?
User.where(id: user_ids)
end
end
......@@ -9,15 +9,39 @@ describe AutocompleteUsersFinder do
let(:current_user) { create(:user) }
let(:params) { {} }
let(:users) { User.all }
subject { described_class.new(params: params, current_user: current_user, users: users).execute.to_a }
let(:project) { nil }
let(:group) { nil }
context 'when users param not passed or nil' do
let(:users) { nil }
subject { described_class.new(params: params, current_user: current_user, project: project, group: group).execute.to_a }
context 'when current_user not passed or nil' do
let(:current_user) { nil }
it { is_expected.to match_array([]) }
end
context 'when project passed' do
let(:project) { create(:project) }
it { is_expected.to match_array([project.owner]) }
context 'when author_id passed' do
let(:params) { { author_id: user2.id } }
it { is_expected.to match_array([project.owner, user2]) }
end
end
context 'when group passed and project not passed' do
let(:group) { create(:group, :public) }
before do
group.add_users([user1], GroupMember::DEVELOPER)
end
it { is_expected.to match_array([user1]) }
end
it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
context 'when filtered by search' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment