BigW Consortium Gitlab

autocomplete_users_finder.rb 1.53 KB
Newer Older
1
class AutocompleteUsersFinder
2 3
  attr_reader :current_user, :project, :group, :search, :skip_users,
              :page, :per_page, :author_id, :params
4

5
  def initialize(params:, current_user:, project:, group:)
6
    @current_user = current_user
7 8
    @project = project
    @group = group
9 10 11 12 13 14 15 16 17
    @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
18
    items = find_users
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    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
43 44 45 46 47

  private

  def find_users
    return users_from_project if project
48
    return group.users_with_parents if group
49 50 51 52 53 54 55 56 57 58 59
    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
60
end