提交 d184f27e 编写于 作者: M Mehdi Lahmam

Refactor Admin::ProjectsFinder by extracting finders as private methods

上级 99bb3dde
......@@ -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
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册