Commit 1e64333c authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor FilterContext

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 1a2e11c8
class FilterContext
attr_accessor :items, :params
attr_accessor :klass, :current_user, :params
def initialize(items, params)
@items = items
def initialize(klass, current_user, params)
@klass = klass
@current_user = current_user
@params = params
end
def execute
apply_filter(items)
items = by_scope
items = by_state(items)
items = by_project(items)
items = by_search(items)
end
def apply_filter items
if params[:project_id].present?
items = items.of_projects(params[:project_id])
end
private
if params[:search].present?
items = items.search(params[:search])
def by_scope
table_name = klass.table_name
case params[:scope]
when 'authored' then
current_user.send(table_name)
when 'all' then
klass.of_projects(current_user.authorized_projects.pluck(:id))
else
current_user.send("assigned_#{table_name}")
end
end
def by_state(items)
case params[:status]
when 'closed'
items.closed
......@@ -28,4 +39,20 @@ class FilterContext
items.opened
end
end
def by_project(items)
if params[:project_id].present?
items = items.of_projects(params[:project_id])
end
items
end
def by_search(items)
if params[:search].present?
items = items.search(params[:search])
end
items
end
end
......@@ -51,30 +51,12 @@ class DashboardController < ApplicationController
end
def merge_requests
@merge_requests = case params[:scope]
when 'authored' then
current_user.merge_requests
when 'all' then
MergeRequest.where(target_project_id: current_user.authorized_projects.pluck(:id))
else
current_user.assigned_merge_requests
end
@merge_requests = FilterContext.new(@merge_requests, params).execute
@merge_requests = FilterContext.new(MergeRequest, current_user, params).execute
@merge_requests = @merge_requests.recent.page(params[:page]).per(20)
end
def issues
@issues = case params[:scope]
when 'authored' then
current_user.issues
when 'all' then
Issue.where(project_id: current_user.authorized_projects.pluck(:id))
else
current_user.assigned_issues
end
@issues = FilterContext.new(@issues, params).execute
@issues = FilterContext.new(Issue, current_user, params).execute
@issues = @issues.recent.page(params[:page]).per(20)
@issues = @issues.includes(:author, :project)
......
......@@ -45,15 +45,15 @@ class GroupsController < ApplicationController
# Get authored or assigned open merge requests
def merge_requests
@merge_requests = current_user.cared_merge_requests.of_group(@group)
@merge_requests = FilterContext.new(@merge_requests, params).execute
@merge_requests = FilterContext.new(MergeRequest, current_user, params).execute
@merge_requests = @merge_requests.of_group(@group)
@merge_requests = @merge_requests.recent.page(params[:page]).per(20)
end
# Get only assigned issues
def issues
@issues = current_user.assigned_issues.of_group(@group)
@issues = FilterContext.new(@issues, params).execute
@issues = FilterContext.new(Issue, current_user, params).execute
@issues = @issues.of_group(@group)
@issues = @issues.recent.page(params[:page]).per(20)
@issues = @issues.includes(:author, :project)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment