diff --git a/app/controllers/admin/projects_controller.rb b/app/controllers/admin/projects_controller.rb index 0b6cd71e6516d0b79ca3302a76f5094f31f94da5..50cf2643390388bc156695a36985d5a764692071 100644 --- a/app/controllers/admin/projects_controller.rb +++ b/app/controllers/admin/projects_controller.rb @@ -3,9 +3,9 @@ class Admin::ProjectsController < Admin::ApplicationController before_action :group, only: [:show, :transfer] def index - finder = Admin::ProjectsFinder.new(params: params, current_user: current_user) - @projects = finder.execute - @sort = finder.sort + params[:sort] ||= 'latest_activity_desc' + @sort = params[:sort] + @projects = Admin::ProjectsFinder.new(params: params, current_user: current_user).execute respond_to do |format| format.html diff --git a/app/finders/admin/projects_finder.rb b/app/finders/admin/projects_finder.rb index 7176bfe22d67a9859302a32b150c591e671d89b4..eac35ae028176e7821f58f600d68c100e4134c03 100644 --- a/app/finders/admin/projects_finder.rb +++ b/app/finders/admin/projects_finder.rb @@ -1,33 +1,61 @@ class Admin::ProjectsFinder - attr_reader :sort, :namespace_id, :visibility_level, :with_push, - :abandoned, :last_repository_check_failed, :archived, - :personal, :name, :page, :current_user + attr_reader :params, :current_user def initialize(params:, current_user:) + @params = params @current_user = current_user - @sort = params.fetch(:sort) { 'latest_activity_desc' } - @namespace_id = params[:namespace_id] - @visibility_level = params[:visibility_level] - @with_push = params[:with_push] - @abandoned = params[:abandoned] - @last_repository_check_failed = params[:last_repository_check_failed] - @archived = params[:archived] - @personal = params[:personal] - @name = params[:name] - @page = params[:page] end def execute items = Project.without_deleted.with_statistics - items = items.in_namespace(namespace_id) if namespace_id.present? - items = items.where(visibility_level: visibility_level) if visibility_level.present? - items = items.with_push if with_push.present? - items = items.abandoned if abandoned.present? - items = items.where(last_repository_check_failed: true) if last_repository_check_failed.present? - items = items.non_archived unless archived.present? - items = items.personal(current_user) if personal.present? - items = items.search(name) if name.present? - items = items.sort(sort) - items.includes(:namespace).order("namespaces.path, projects.name ASC").page(page) + items = by_namespace_id(items) + items = by_visibilty_level(items) + items = by_with_push(items) + items = by_abandoned(items) + items = by_last_repository_check_failed(items) + items = by_archived(items) + items = by_personal(items) + items = by_name(items) + items = sort(items) + items.includes(:namespace).order("namespaces.path, projects.name ASC").page(params[:page]) + end + + private + + def by_namespace_id(items) + params[:namespace_id].present? ? items.in_namespace(params[:namespace_id]) : items + end + + def by_visibilty_level(items) + params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items + end + + def by_with_push(items) + params[:with_push].present? ? items.with_push : items + end + + def by_abandoned(items) + params[:abandoned].present? ? items.abandoned : items + end + + def by_last_repository_check_failed(items) + params[:last_repository_check_failed].present? ? items.where(last_repository_check_failed: true) : items + end + + def by_archived(items) + items.non_archived unless params[:archived].present? + end + + def by_personal(items) + params[:personal].present? ? items.personal(current_user) : items + end + + def by_name(items) + params[:name].present? ? items.search(params[:name]) : items + end + + def sort(items) + sort = params.fetch(:sort) { 'latest_activity_desc' } + items.sort(sort) end end