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 class FilterContext
attr_accessor :items, :params attr_accessor :klass, :current_user, :params
def initialize(items, params) def initialize(klass, current_user, params)
@items = items @klass = klass
@current_user = current_user
@params = params @params = params
end end
def execute def execute
apply_filter(items) items = by_scope
items = by_state(items)
items = by_project(items)
items = by_search(items)
end end
def apply_filter items private
if params[:project_id].present?
items = items.of_projects(params[:project_id])
end
if params[:search].present? def by_scope
items = items.search(params[:search]) 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
end
def by_state(items)
case params[:status] case params[:status]
when 'closed' when 'closed'
items.closed items.closed
...@@ -28,4 +39,20 @@ class FilterContext ...@@ -28,4 +39,20 @@ class FilterContext
items.opened items.opened
end end
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 end
...@@ -51,30 +51,12 @@ class DashboardController < ApplicationController ...@@ -51,30 +51,12 @@ class DashboardController < ApplicationController
end end
def merge_requests def merge_requests
@merge_requests = case params[:scope] @merge_requests = FilterContext.new(MergeRequest, current_user, params).execute
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 = @merge_requests.recent.page(params[:page]).per(20) @merge_requests = @merge_requests.recent.page(params[:page]).per(20)
end end
def issues def issues
@issues = case params[:scope] @issues = FilterContext.new(Issue, current_user, params).execute
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 = @issues.recent.page(params[:page]).per(20) @issues = @issues.recent.page(params[:page]).per(20)
@issues = @issues.includes(:author, :project) @issues = @issues.includes(:author, :project)
......
...@@ -45,15 +45,15 @@ class GroupsController < ApplicationController ...@@ -45,15 +45,15 @@ class GroupsController < ApplicationController
# Get authored or assigned open merge requests # Get authored or assigned open merge requests
def merge_requests def merge_requests
@merge_requests = current_user.cared_merge_requests.of_group(@group) @merge_requests = FilterContext.new(MergeRequest, current_user, params).execute
@merge_requests = FilterContext.new(@merge_requests, params).execute @merge_requests = @merge_requests.of_group(@group)
@merge_requests = @merge_requests.recent.page(params[:page]).per(20) @merge_requests = @merge_requests.recent.page(params[:page]).per(20)
end end
# Get only assigned issues # Get only assigned issues
def issues def issues
@issues = current_user.assigned_issues.of_group(@group) @issues = FilterContext.new(Issue, current_user, params).execute
@issues = FilterContext.new(@issues, params).execute @issues = @issues.of_group(@group)
@issues = @issues.recent.page(params[:page]).per(20) @issues = @issues.recent.page(params[:page]).per(20)
@issues = @issues.includes(:author, :project) @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