diff options
author | Maxim Rydkin <maks.rydkin@gmail.com> | 2017-08-23 22:33:56 +0300 |
---|---|---|
committer | Maxim Rydkin <maks.rydkin@gmail.com> | 2017-09-10 18:31:05 +0300 |
commit | fbbb985a03d0b51e28a3df5a3c3f21a81540405f (patch) | |
tree | 4eeb1f2e52ae82c5917ab1cf340a493200e5e50d /app | |
parent | 5d952f756bcf0355fc5d86d819dfc6913c0ae351 (diff) | |
download | gitlab-ce-fbbb985a03d0b51e28a3df5a3c3f21a81540405f.tar.gz |
extract finder and add first test
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/autocomplete_controller.rb | 23 | ||||
-rw-r--r-- | app/finders/yet_another_users_finder.rb | 42 |
2 files changed, 43 insertions, 22 deletions
diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb index dfc8bd0ba81..8e945f90f76 100644 --- a/app/controllers/autocomplete_controller.rb +++ b/app/controllers/autocomplete_controller.rb @@ -6,28 +6,7 @@ class AutocompleteController < ApplicationController before_action :find_users, only: [:users] def users - @users ||= User.none - @users = @users.active - @users = @users.reorder(:name) - @users = @users.search(params[:search]) if params[:search].present? - @users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present? - @users = @users.page(params[:page]).per(params[:per_page]) - - if params[:todo_filter].present? && current_user - @users = @users.todo_authors(current_user.id, params[:todo_state_filter]) - end - - if params[:search].blank? - # Include current user if available to filter by "Me" - if params[:current_user].present? && current_user - @users = [current_user, *@users].uniq - end - - if params[:author_id].present? && current_user - author = User.find_by_id(params[:author_id]) - @users = [author, *@users].uniq if author - end - end + @users = YetAnotherUsersFinder.new(params: params, current_user: current_user, users: @users).execute render json: @users, only: [:name, :username, :id], methods: [:avatar_url] end diff --git a/app/finders/yet_another_users_finder.rb b/app/finders/yet_another_users_finder.rb new file mode 100644 index 00000000000..5f7f3913160 --- /dev/null +++ b/app/finders/yet_another_users_finder.rb @@ -0,0 +1,42 @@ +class YetAnotherUsersFinder + attr_reader :current_user, :users, :search, :skip_users, :page, + :per_page, :author_id, :params + + def initialize(params:, current_user:, users: nil) + @current_user = current_user + @users = users + @search = params[:search] + @skip_users = params[:skip_users] + @page = params[:page] + @per_page = params[:per_page] + @author_id = params[:author_id] + @params = params + end + + def execute + items = users || User.none + items = items.active + items = items.reorder(:name) + items = items.search(search) if search.present? + items = items.where.not(id: skip_users) if skip_users.present? + items = items.page(page).per(per_page) + + if params[:todo_filter].present? && current_user + items = items.todo_authors(current_user.id, params[:todo_state_filter]) + end + + if search.blank? + # Include current user if available to filter by "Me" + if params[:current_user].present? && current_user + items = [current_user, *items].uniq + end + + if author_id.present? && current_user + author = User.find_by_id(author_id) + items = [author, *items].uniq if author + end + end + + items + end +end |