2016-04-02 18:10:28 +05:30
|
|
|
# TodosFinder
|
|
|
|
#
|
|
|
|
# Used to filter Todos by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
|
|
|
# action_id: integer
|
|
|
|
# author_id: integer
|
|
|
|
# project_id; integer
|
|
|
|
# state: 'pending' or 'done'
|
|
|
|
# type: 'Issue' or 'MergeRequest'
|
|
|
|
#
|
|
|
|
|
|
|
|
class TodosFinder
|
|
|
|
NONE = '0'
|
|
|
|
|
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params)
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = current_user.todos
|
|
|
|
items = by_action_id(items)
|
|
|
|
items = by_author(items)
|
|
|
|
items = by_project(items)
|
|
|
|
items = by_state(items)
|
|
|
|
items = by_type(items)
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
items.reorder(id: :desc)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def action_id?
|
2016-06-16 23:09:34 +05:30
|
|
|
action_id.present? && [Todo::ASSIGNED, Todo::MENTIONED, Todo::BUILD_FAILED, Todo::MARKED].include?(action_id.to_i)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def action_id
|
|
|
|
params[:action_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def author?
|
|
|
|
params[:author_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
|
|
|
return @author if defined?(@author)
|
|
|
|
|
|
|
|
@author =
|
|
|
|
if author? && params[:author_id] != NONE
|
|
|
|
User.find(params[:author_id])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project?
|
|
|
|
params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
|
|
|
if project?
|
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
|
|
|
|
unless Ability.abilities.allowed?(current_user, :read_project, @project)
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
|
|
|
|
|
|
|
if project?
|
|
|
|
@projects = project
|
|
|
|
else
|
|
|
|
@projects = ProjectsFinder.new.execute(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def type?
|
|
|
|
type.present? && ['Issue', 'MergeRequest'].include?(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
params[:type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_action_id(items)
|
|
|
|
if action_id?
|
|
|
|
items = items.where(action: action_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_author(items)
|
|
|
|
if author?
|
|
|
|
items = items.where(author_id: author.try(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
|
|
|
if project?
|
|
|
|
items = items.where(project: project)
|
2016-06-16 23:09:34 +05:30
|
|
|
elsif projects
|
|
|
|
items = items.merge(projects).joins(:project)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
2016-06-22 15:30:34 +05:30
|
|
|
case params[:state].to_s
|
2016-04-02 18:10:28 +05:30
|
|
|
when 'done'
|
|
|
|
items.done
|
|
|
|
else
|
|
|
|
items.pending
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_type(items)
|
|
|
|
if type?
|
|
|
|
items = items.where(target_type: type)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|