2014-02-25 12:15:08 -05:00
|
|
|
# BaseFinder
|
2014-01-15 09:16:23 -05:00
|
|
|
#
|
|
|
|
# Used to filter Issues and MergeRequests collections by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# klass - actual class like Issue or MergeRequest
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
|
|
|
# scope: 'created-by-me' or 'assigned-to-me' or 'all'
|
|
|
|
# state: 'open' or 'closed' or 'all'
|
|
|
|
# group_id: integer
|
|
|
|
# project_id: integer
|
|
|
|
# milestone_id: integer
|
|
|
|
# assignee_id: integer
|
|
|
|
# search: string
|
|
|
|
# label_name: string
|
|
|
|
# sort: string
|
|
|
|
#
|
2014-02-25 12:15:08 -05:00
|
|
|
class BaseFinder
|
|
|
|
attr_accessor :current_user, :params
|
2014-01-15 09:16:23 -05:00
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
def execute(current_user, params)
|
2014-01-15 09:16:23 -05:00
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
|
2014-02-03 10:02:44 -05:00
|
|
|
items = init_collection
|
|
|
|
items = by_scope(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
items = by_state(items)
|
|
|
|
items = by_group(items)
|
2014-02-03 11:56:31 -05:00
|
|
|
items = by_project(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
items = by_search(items)
|
|
|
|
items = by_milestone(items)
|
|
|
|
items = by_assignee(items)
|
|
|
|
items = by_label(items)
|
|
|
|
items = sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-02-03 10:02:44 -05:00
|
|
|
def init_collection
|
2014-01-15 09:16:23 -05:00
|
|
|
table_name = klass.table_name
|
|
|
|
|
2014-02-03 10:02:44 -05:00
|
|
|
if project
|
2014-02-13 15:45:51 -05:00
|
|
|
if project.public? || (current_user && current_user.can?(:read_project, project))
|
2014-02-03 10:02:44 -05:00
|
|
|
project.send(table_name)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-02-13 15:45:51 -05:00
|
|
|
elsif current_user && params[:authorized_only].presence
|
2014-03-18 20:05:10 -04:00
|
|
|
klass.of_projects(current_user.authorized_projects).references(:project)
|
2014-02-13 15:45:51 -05:00
|
|
|
else
|
2014-06-05 13:37:35 -04:00
|
|
|
klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project)
|
2014-02-03 10:02:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_scope(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
case params[:scope]
|
|
|
|
when 'created-by-me', 'authored' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(author_id: current_user.id)
|
2014-01-15 09:16:23 -05:00
|
|
|
when 'all' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items
|
2014-01-15 09:16:23 -05:00
|
|
|
when 'assigned-to-me' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(assignee_id: current_user.id)
|
2014-01-15 09:16:23 -05:00
|
|
|
else
|
|
|
|
raise 'You must specify default scope'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
|
|
|
case params[:state]
|
|
|
|
when 'closed'
|
|
|
|
items.closed
|
|
|
|
when 'all'
|
|
|
|
items
|
|
|
|
when 'opened'
|
|
|
|
items.opened
|
|
|
|
else
|
|
|
|
raise 'You must specify default state'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_group(items)
|
|
|
|
if params[:group_id].present?
|
|
|
|
items = items.of_group(Group.find(params[:group_id]))
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
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
|
|
|
|
|
|
|
|
def sort(items)
|
|
|
|
items.sort(params[:sort])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_milestone(items)
|
|
|
|
if params[:milestone_id].present?
|
|
|
|
items = items.where(milestone_id: (params[:milestone_id] == '0' ? nil : params[:milestone_id]))
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_assignee(items)
|
|
|
|
if params[:assignee_id].present?
|
|
|
|
items = items.where(assignee_id: (params[:assignee_id] == '0' ? nil : params[:assignee_id]))
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_label(items)
|
|
|
|
if params[:label_name].present?
|
|
|
|
items = items.tagged_with(params[:label_name])
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
2014-01-31 11:06:13 -05:00
|
|
|
|
|
|
|
def project
|
|
|
|
Project.where(id: params[:project_id]).first if params[:project_id].present?
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|