2016-02-20 13:59:59 +00:00
|
|
|
# TodosFinder
|
2016-02-18 03:20:31 +00:00
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# Used to filter Todos by set of params
|
2016-02-18 03:20:31 +00:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
|
|
|
# action_id: integer
|
|
|
|
# author_id: integer
|
|
|
|
# project_id; integer
|
2016-07-11 06:10:04 +00:00
|
|
|
# state: 'pending' (default) or 'done'
|
2016-02-18 03:20:31 +00:00
|
|
|
# type: 'Issue' or 'MergeRequest'
|
|
|
|
#
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
class TodosFinder
|
2016-02-18 03:20:31 +00:00
|
|
|
NONE = '0'
|
|
|
|
|
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params)
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-06-01 23:44:35 +00:00
|
|
|
items = current_user.todos
|
2016-02-18 03:20:31 +00:00
|
|
|
items = by_action_id(items)
|
2016-06-22 17:15:09 +00:00
|
|
|
items = by_action(items)
|
2016-02-18 03:20:31 +00:00
|
|
|
items = by_author(items)
|
|
|
|
items = by_state(items)
|
|
|
|
items = by_type(items)
|
2016-08-12 12:29:59 +00:00
|
|
|
# Filtering by project HAS TO be the last because we use
|
|
|
|
# the project IDs yielded by the todos query thus far
|
|
|
|
items = by_project(items)
|
2016-02-18 03:20:31 +00:00
|
|
|
|
2016-06-02 21:17:46 +00:00
|
|
|
items.reorder(id: :desc)
|
2016-02-18 03:20:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def action_id?
|
2016-07-11 10:02:11 +00:00
|
|
|
action_id.present? && Todo::ACTION_NAMES.has_key?(action_id.to_i)
|
2016-02-18 03:20:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def action_id
|
|
|
|
params[:action_id]
|
|
|
|
end
|
|
|
|
|
2016-06-22 17:15:09 +00:00
|
|
|
def to_action_id
|
|
|
|
Todo::ACTION_NAMES.key(action.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
def action?
|
|
|
|
action.present? && to_action_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def action
|
|
|
|
params[:action]
|
|
|
|
end
|
|
|
|
|
2016-02-18 03:20:31 +00:00
|
|
|
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-08-12 12:29:59 +00:00
|
|
|
def projects(items)
|
|
|
|
item_project_ids = items.reorder(nil).select(:project_id)
|
|
|
|
ProjectsFinder.new.execute(current_user) do |relation|
|
|
|
|
relation.where(id: item_project_ids)
|
2016-06-01 23:44:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 03:20:31 +00:00
|
|
|
def type?
|
|
|
|
type.present? && ['Issue', 'MergeRequest'].include?(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
params[:type]
|
|
|
|
end
|
|
|
|
|
2016-06-22 17:15:09 +00:00
|
|
|
def by_action(items)
|
|
|
|
if action?
|
|
|
|
items = items.where(action: to_action_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2016-02-18 03:20:31 +00:00
|
|
|
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-08-12 12:29:59 +00:00
|
|
|
else
|
|
|
|
item_projects = projects(items)
|
|
|
|
items = items.merge(item_projects).joins(:project)
|
2016-02-18 03:20:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
2016-06-15 02:09:48 +00:00
|
|
|
case params[:state].to_s
|
2016-02-18 03:20:31 +00:00
|
|
|
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
|