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 ...@@ -3,10 +3,10 @@ class AutocompleteController < ApplicationController
skip_before_action :authenticate_user!, only: [:users, :award_emojis] skip_before_action :authenticate_user!, only: [:users, :award_emojis]
before_action :load_project, only: [:users] before_action :load_project, only: [:users]
before_action :find_users, only: [:users] before_action :load_group, only: [:users]
def 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] render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end end
...@@ -39,25 +39,13 @@ class AutocompleteController < ApplicationController ...@@ -39,25 +39,13 @@ class AutocompleteController < ApplicationController
private private
def find_users def load_group
@users = @group ||= begin
if @project if @project.blank? && params[:group_id].present?
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?
group = Group.find(params[:group_id]) group = Group.find(params[:group_id])
return render_404 unless can?(current_user, :read_group, group) return render_404 unless can?(current_user, :read_group, group)
group
group.users end
elsif current_user
User.all
else
User.none
end end
end end
......
class AutocompleteUsersFinder class AutocompleteUsersFinder
attr_reader :current_user, :users, :search, :skip_users, :page, attr_reader :current_user, :project, :group, :search, :skip_users,
:per_page, :author_id, :params :page, :per_page, :author_id, :params
def initialize(params:, current_user:, users: nil) def initialize(params:, current_user:, project:, group:)
@current_user = current_user @current_user = current_user
@users = users @project = project
@group = group
@search = params[:search] @search = params[:search]
@skip_users = params[:skip_users] @skip_users = params[:skip_users]
@page = params[:page] @page = params[:page]
...@@ -14,7 +15,7 @@ class AutocompleteUsersFinder ...@@ -14,7 +15,7 @@ class AutocompleteUsersFinder
end end
def execute def execute
items = users || User.none items = find_users
items = items.active items = items.active
items = items.reorder(:name) items = items.reorder(:name)
items = items.search(search) if search.present? items = items.search(search) if search.present?
...@@ -39,4 +40,21 @@ class AutocompleteUsersFinder ...@@ -39,4 +40,21 @@ class AutocompleteUsersFinder
items items
end 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 end
...@@ -9,15 +9,39 @@ describe AutocompleteUsersFinder do ...@@ -9,15 +9,39 @@ describe AutocompleteUsersFinder do
let(:current_user) { create(:user) } let(:current_user) { create(:user) }
let(:params) { {} } let(:params) { {} }
let(:users) { User.all } let(:project) { nil }
subject { described_class.new(params: params, current_user: current_user, users: users).execute.to_a } let(:group) { nil }
context 'when users param not passed or nil' do subject { described_class.new(params: params, current_user: current_user, project: project, group: group).execute.to_a }
let(:users) { nil }
context 'when current_user not passed or nil' do
let(:current_user) { nil }
it { is_expected.to match_array([]) } it { is_expected.to match_array([]) }
end 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]) } it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
context 'when filtered by search' do 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