Read about our upcoming Code of Conduct on this issue

projects_controller.rb 2.03 KB
Newer Older
1
2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3
class Explore::ProjectsController < Explore::ApplicationController
4
  include ParamsBackwardCompatibility
5
  include RendersMemberAccess
George Koltsov's avatar
George Koltsov committed
6
7
  include SortingHelper
  include SortingPreference
8
9

  before_action :set_non_archived_param
George Koltsov's avatar
George Koltsov committed
10
  before_action :set_sorting
11

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12
  def index
13
    @projects = load_projects
14
15
16
17
18

    respond_to do |format|
      format.html
      format.json do
        render json: {
19
          html: view_to_html_string("explore/projects/_projects", projects: @projects)
20
21
22
        }
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
23
24
25
  end

  def trending
26
    params[:trending] = true
27
    @projects = load_projects
28
29
30
31
32

    respond_to do |format|
      format.html
      format.json do
        render json: {
33
          html: view_to_html_string("explore/projects/_projects", projects: @projects)
34
35
36
        }
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37
  end
38

39
  # rubocop: disable CodeReuse/ActiveRecord
40
  def starred
41
    @projects = load_projects.reorder('star_count DESC')
42
43
44
45
46

    respond_to do |format|
      format.html
      format.json do
        render json: {
47
          html: view_to_html_string("explore/projects/_projects", projects: @projects)
48
49
50
        }
      end
    end
51
  end
52
  # rubocop: enable CodeReuse/ActiveRecord
53

54
  private
55

56
  # rubocop: disable CodeReuse/ActiveRecord
57
  def load_projects
58
59
60
    @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

61
62
    projects = ProjectsFinder.new(current_user: current_user, params: params)
                 .execute
63
                 .includes(:route, :creator, :group, namespace: [:route, :owner])
64
65
66
67
                 .page(params[:page])
                 .without_count

    prepare_projects_for_rendering(projects)
68
  end
69
  # rubocop: enable CodeReuse/ActiveRecord
George Koltsov's avatar
George Koltsov committed
70
71
72
73
74
75
76
77
78
79
80
81
82

  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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83
end