debian-mirror-gitlab/app/controllers/autocomplete_controller.rb

81 lines
2.1 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
class AutocompleteController < ApplicationController
2015-09-11 14:41:01 +05:30
skip_before_action :authenticate_user!, only: [:users]
2016-09-13 17:45:13 +05:30
before_action :load_project, only: [:users]
2015-12-23 02:04:40 +05:30
before_action :find_users, only: [:users]
2015-09-11 14:41:01 +05:30
2015-04-26 12:48:37 +05:30
def users
2015-09-11 14:41:01 +05:30
@users ||= User.none
2015-04-26 12:48:37 +05:30
@users = @users.search(params[:search]) if params[:search].present?
2016-09-13 17:45:13 +05:30
@users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
2015-04-26 12:48:37 +05:30
@users = @users.active
2015-09-25 12:07:36 +05:30
@users = @users.reorder(:name)
2016-06-02 11:05:42 +05:30
@users = @users.page(params[:page])
2015-09-11 14:41:01 +05:30
2015-12-23 02:04:40 +05:30
if params[:search].blank?
2015-09-11 14:41:01 +05:30
# Include current user if available to filter by "Me"
if params[:current_user] && current_user
2016-06-02 11:05:42 +05:30
@users = [*@users, current_user]
2015-09-11 14:41:01 +05:30
end
2016-06-02 11:05:42 +05:30
if params[:author_id].present?
author = User.find_by_id(params[:author_id])
@users = [author, *@users] if author
end
@users.uniq!
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end
def user
@user = User.find(params[:id])
render json: @user, only: [:name, :username, :id], methods: [:avatar_url]
end
2015-12-23 02:04:40 +05:30
def projects
project = Project.find_by_id(params[:project_id])
2016-09-13 17:45:13 +05:30
projects = projects_finder.execute(project, search: params[:search], offset_id: params[:offset_id])
no_project = {
id: 0,
name_with_namespace: 'No project',
}
2016-09-13 17:45:13 +05:30
projects.unshift(no_project) unless params[:offset_id].present?
render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
end
2015-12-23 02:04:40 +05:30
private
def find_users
@users =
2016-09-13 17:45:13 +05:30
if @project
@project.team.users
2015-12-23 02:04:40 +05:30
elsif 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
end
end
2016-09-13 17:45:13 +05:30
def load_project
@project ||= begin
if params[:project_id].present?
project = Project.find(params[:project_id])
return render_404 unless can?(current_user, :read_project, project)
project
end
end
end
def projects_finder
MoveToProjectFinder.new(current_user)
end
2015-04-26 12:48:37 +05:30
end