diff options
author | Maxim Rydkin <maks.rydkin@gmail.com> | 2017-09-08 12:00:31 +0300 |
---|---|---|
committer | Maxim Rydkin <maks.rydkin@gmail.com> | 2017-09-10 18:31:06 +0300 |
commit | fa276e30292349173e98f7fe0f9a94c82345dc7a (patch) | |
tree | 223b287f7e35fcda434224f4143e738866af85ff /app | |
parent | 20ff87a261ac0ea06268d2cb7223f104baf2ed35 (diff) |
move `find_users` method into finder and add `load_group` method
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/autocomplete_controller.rb | 26 | ||||
-rw-r--r-- | app/finders/autocomplete_users_finder.rb | 28 |
2 files changed, 30 insertions, 24 deletions
diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb index d907777e936..10e8e54f402 100644 --- a/app/controllers/autocomplete_controller.rb +++ b/app/controllers/autocomplete_controller.rb @@ -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,26 +39,14 @@ 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 def load_project diff --git a/app/finders/autocomplete_users_finder.rb b/app/finders/autocomplete_users_finder.rb index f88e94b70b7..b8f52e31926 100644 --- a/app/finders/autocomplete_users_finder.rb +++ b/app/finders/autocomplete_users_finder.rb @@ -1,10 +1,11 @@ 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 |