Commit 46997c6a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Remove unnecessary contexts

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 234b86c9
class FilterContext
attr_accessor :klass, :current_user, :params
def initialize(klass, current_user, params)
@klass = klass
@current_user = current_user
@params = params
end
def execute
items = by_scope
items = by_state(items)
items = by_project(items)
items = by_search(items)
end
private
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
when 'all'
items
else
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
module Issues
class ListContext < BaseContext
attr_accessor :issues
def execute
@issues = @project.issues
@issues = case params[:state]
when 'all' then @issues
when 'closed' then @issues.closed
else @issues.opened
end
@issues = case params[:scope]
when 'assigned-to-me' then @issues.assigned_to(current_user)
when 'created-by-me' then @issues.authored(current_user)
else @issues
end
@issues = @issues.tagged_with(params[:label_name]) if params[:label_name].present?
@issues = @issues.includes(:author, :project)
# Filter by specific assignee_id (or lack thereof)?
if params[:assignee_id].present?
@issues = @issues.where(assignee_id: (params[:assignee_id] == '0' ? nil : params[:assignee_id]))
end
# Filter by specific milestone_id (or lack thereof)?
if params[:milestone_id].present?
@issues = @issues.where(milestone_id: (params[:milestone_id] == '0' ? nil : params[:milestone_id]))
end
# Sort by :sort param
@issues = @issues.sort(params[:sort])
@issues
end
end
end
# Build collection of Merge Requests
# based on filtering passed via params for @project
class MergeRequestsLoadContext < BaseContext
def execute
merge_requests = @project.merge_requests
merge_requests = case params[:state]
when 'all' then merge_requests
when 'closed' then merge_requests.closed
else merge_requests.opened
end
merge_requests = case params[:scope]
when 'assigned-to-me' then merge_requests.assigned_to(current_user)
when 'created-by-me' then merge_requests.authored(current_user)
else merge_requests
end
merge_requests = merge_requests.page(params[:page]).per(20)
merge_requests = merge_requests.includes(:author, :source_project, :target_project).order("created_at desc")
# Filter by specific assignee_id (or lack thereof)?
if params[:assignee_id].present?
merge_requests = merge_requests.where(assignee_id: (params[:assignee_id] == '0' ? nil : params[:assignee_id]))
end
# Filter by specific milestone_id (or lack thereof)?
if params[:milestone_id].present?
merge_requests = merge_requests.where(milestone_id: (params[:milestone_id] == '0' ? nil : params[:milestone_id]))
end
# Sort by :sort param
merge_requests = merge_requests.sort(params[:sort])
merge_requests
end
end
require 'spec_helper'
describe FilterContext do
let(:user) { create :user }
let(:user2) { create :user }
let(:project1) { create(:project) }
let(:project2) { create(:project) }
let(:merge_request1) { create(:merge_request, author: user, source_project: project1, target_project: project2) }
let(:merge_request2) { create(:merge_request, author: user, source_project: project2, target_project: project1) }
let(:merge_request3) { create(:merge_request, author: user, source_project: project2, target_project: project2) }
let(:issue1) { create(:issue, assignee: user, project: project1) }
let(:issue2) { create(:issue, assignee: user, project: project2) }
let(:issue3) { create(:issue, assignee: user2, project: project2) }
before do
project1.team << [user, :master]
project2.team << [user, :developer]
end
describe 'merge requests' do
before :each do
merge_request1
merge_request2
merge_request3
end
it 'should filter by scope' do
params = { scope: 'authored' }
merge_requests = FilterContext.new(MergeRequest, user, params).execute
merge_requests.size.should == 3
end
it 'should filter by project' do
params = { project_id: project1.id, scope: 'authored' }
merge_requests = FilterContext.new(MergeRequest, user, params).execute
merge_requests.size.should == 1
end
end
describe 'issues' do
before :each do
issue1
issue2
issue3
end
it 'should filter by all' do
params = { scope: "all" }
issues = FilterContext.new(Issue, user, params).execute
issues.size.should == 3
end
it 'should filter by assignee' do
params = {}
issues = FilterContext.new(Issue, user, params).execute
issues.size.should == 2
end
it 'should filter by project' do
params = { project_id: project1.id }
issues = FilterContext.new(Issue, user, params).execute
issues.size.should == 1
end
end
end
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