debian-mirror-gitlab/app/controllers/explore/projects_controller.rb

121 lines
2.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
class Explore::ProjectsController < Explore::ApplicationController
2020-03-13 15:44:24 +05:30
include PageLimiter
2017-08-17 22:00:37 +05:30
include ParamsBackwardCompatibility
2018-03-17 18:26:18 +05:30
include RendersMemberAccess
2020-07-28 23:09:34 +05:30
include RendersProjectsList
2019-12-04 20:38:33 +05:30
include SortingHelper
include SortingPreference
2017-08-17 22:00:37 +05:30
2020-12-08 15:28:05 +05:30
MIN_SEARCH_LENGTH = 3
2017-08-17 22:00:37 +05:30
before_action :set_non_archived_param
2019-12-04 20:38:33 +05:30
before_action :set_sorting
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
# Limit taken from https://gitlab.com/gitlab-org/gitlab/issues/38357
before_action only: [:index, :trending, :starred] do
limit_pages(200)
end
rescue_from PageOutOfBoundsError, with: :page_out_of_bounds
2014-09-02 18:07:02 +05:30
def index
2017-09-10 17:25:29 +05:30
@projects = load_projects
2016-04-02 18:10:28 +05:30
respond_to do |format|
format.html
format.json do
render json: {
2019-07-07 11:18:12 +05:30
html: view_to_html_string("explore/projects/_projects", projects: @projects)
2016-04-02 18:10:28 +05:30
}
end
end
2014-09-02 18:07:02 +05:30
end
def trending
2017-08-17 22:00:37 +05:30
params[:trending] = true
2017-09-10 17:25:29 +05:30
@projects = load_projects
2016-04-02 18:10:28 +05:30
respond_to do |format|
format.html
format.json do
render json: {
2019-07-07 11:18:12 +05:30
html: view_to_html_string("explore/projects/_projects", projects: @projects)
2016-04-02 18:10:28 +05:30
}
end
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def starred
2017-09-10 17:25:29 +05:30
@projects = load_projects.reorder('star_count DESC')
2016-04-02 18:10:28 +05:30
respond_to do |format|
format.html
format.json do
render json: {
2019-07-07 11:18:12 +05:30
html: view_to_html_string("explore/projects/_projects", projects: @projects)
2016-04-02 18:10:28 +05:30
}
end
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
private
2020-03-13 15:44:24 +05:30
def load_project_counts
2019-03-02 22:35:43 +05:30
@total_user_projects_count = ProjectsFinder.new(params: { non_public: true }, current_user: current_user).execute
@total_starred_projects_count = ProjectsFinder.new(params: { starred: true }, current_user: current_user).execute
2020-03-13 15:44:24 +05:30
end
def load_projects
load_project_counts
2019-03-02 22:35:43 +05:30
2020-12-08 15:28:05 +05:30
projects = ProjectsFinder.new(current_user: current_user, params: params.merge(minimum_search_length: MIN_SEARCH_LENGTH)).execute
2020-04-22 19:07:51 +05:30
projects = preload_associations(projects)
projects = projects.page(params[:page]).without_count
2018-03-17 18:26:18 +05:30
prepare_projects_for_rendering(projects)
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def preload_associations(projects)
2020-10-24 23:57:45 +05:30
projects.includes(:route, :creator, :group, :project_feature, namespace: [:route, :owner])
2020-04-22 19:07:51 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-12-04 20:38:33 +05:30
def set_sorting
params[:sort] = set_sort_order
@sort = params[:sort]
end
def default_sort_order
sort_value_latest_activity
end
def sorting_field
Project::SORTING_PREFERENCE_FIELD
end
2020-03-13 15:44:24 +05:30
def page_out_of_bounds(error)
load_project_counts
@max_page_number = error.message
respond_to do |format|
format.html do
render "page_out_of_bounds", status: :bad_request
end
format.json do
render json: {
html: view_to_html_string("explore/projects/page_out_of_bounds")
}, status: :bad_request
end
end
end
2014-09-02 18:07:02 +05:30
end
2020-04-22 19:07:51 +05:30
Explore::ProjectsController.prepend_if_ee('EE::Explore::ProjectsController')