2018-09-23 19:44:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
class Dashboard::TodosController < Dashboard::ApplicationController
|
2017-02-22 07:19:09 +00:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2019-09-03 17:45:00 +00:00
|
|
|
include PaginatedCollection
|
2020-06-22 15:09:27 +00:00
|
|
|
include Analytics::UniqueVisitsHelper
|
2017-02-22 07:19:09 +00:00
|
|
|
|
2017-06-13 17:14:14 +00:00
|
|
|
before_action :authorize_read_project!, only: :index
|
2018-11-28 18:36:59 +00:00
|
|
|
before_action :authorize_read_group!, only: :index
|
2016-06-15 02:09:48 +00:00
|
|
|
before_action :find_todos, only: [:index, :destroy_all]
|
2016-02-20 19:53:25 +00:00
|
|
|
|
2020-10-02 12:09:03 +00:00
|
|
|
feature_category :issue_tracking
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
def index
|
2016-07-26 21:21:20 +00:00
|
|
|
@sort = params[:sort]
|
2016-12-20 18:52:09 +00:00
|
|
|
@todos = @todos.page(params[:page])
|
2019-06-24 17:48:50 +00:00
|
|
|
@todos = @todos.with_entity_associations
|
2017-10-04 16:37:38 +00:00
|
|
|
|
2019-09-03 17:45:00 +00:00
|
|
|
return if redirect_out_of_range(@todos, todos_page_count(@todos))
|
2016-02-20 13:59:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-05-28 18:08:37 +00:00
|
|
|
todo = current_user.todos.find(params[:id])
|
|
|
|
|
|
|
|
TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :mark_done)
|
2016-02-20 13:59:59 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-06 22:45:16 +00:00
|
|
|
format.html do
|
|
|
|
redirect_to dashboard_todos_path,
|
2019-11-17 12:06:19 +00:00
|
|
|
status: :found,
|
2019-07-04 15:45:54 +00:00
|
|
|
notice: _('To-do item successfully marked as done.')
|
2017-06-06 22:45:16 +00:00
|
|
|
end
|
2016-03-16 01:16:25 +00:00
|
|
|
format.js { head :ok }
|
2016-07-11 06:10:04 +00:00
|
|
|
format.json { render json: todos_counts }
|
2016-02-20 13:59:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-20 19:53:25 +00:00
|
|
|
def destroy_all
|
2020-05-28 18:08:37 +00:00
|
|
|
updated_ids = TodoService.new.resolve_todos(@todos, current_user, resolved_by_action: :mark_all_done)
|
2016-02-20 19:53:25 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2019-11-17 12:06:19 +00:00
|
|
|
format.html { redirect_to dashboard_todos_path, status: :found, notice: _('Everything on your to-do list is marked as done.') }
|
2016-03-16 01:16:25 +00:00
|
|
|
format.js { head :ok }
|
2017-01-29 09:44:30 +00:00
|
|
|
format.json { render json: todos_counts.merge(updated_ids: updated_ids) }
|
2016-02-20 19:53:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-16 13:11:08 +00:00
|
|
|
def restore
|
2020-05-28 18:08:37 +00:00
|
|
|
TodoService.new.restore_todo(current_user.todos.find(params[:id]), current_user)
|
2017-01-16 13:11:08 +00:00
|
|
|
|
|
|
|
render json: todos_counts
|
|
|
|
end
|
|
|
|
|
2017-01-29 09:44:30 +00:00
|
|
|
def bulk_restore
|
2020-05-28 18:08:37 +00:00
|
|
|
TodoService.new.restore_todos(current_user.todos.for_ids(params[:ids]), current_user)
|
2017-01-29 09:44:30 +00:00
|
|
|
|
|
|
|
render json: todos_counts
|
|
|
|
end
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
private
|
|
|
|
|
2017-06-13 17:14:14 +00:00
|
|
|
def authorize_read_project!
|
|
|
|
project_id = params[:project_id]
|
|
|
|
|
|
|
|
if project_id.present?
|
|
|
|
project = Project.find(project_id)
|
|
|
|
render_404 unless can?(current_user, :read_project, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-28 18:36:59 +00:00
|
|
|
def authorize_read_group!
|
|
|
|
group_id = params[:group_id]
|
|
|
|
|
|
|
|
if group_id.present?
|
|
|
|
group = Group.find(group_id)
|
|
|
|
render_404 unless can?(current_user, :read_group, group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-20 19:53:25 +00:00
|
|
|
def find_todos
|
2017-10-04 16:37:38 +00:00
|
|
|
@todos ||= TodosFinder.new(current_user, todo_params).execute
|
2016-02-20 19:53:25 +00:00
|
|
|
end
|
2016-07-11 06:10:04 +00:00
|
|
|
|
|
|
|
def todos_counts
|
|
|
|
{
|
2019-09-18 18:06:14 +00:00
|
|
|
count: current_user.todos_pending_count,
|
|
|
|
done_count: current_user.todos_done_count
|
2016-07-11 06:10:04 +00:00
|
|
|
}
|
|
|
|
end
|
2017-10-04 16:37:38 +00:00
|
|
|
|
2019-09-03 17:45:00 +00:00
|
|
|
def todos_page_count(todos)
|
2019-09-18 14:02:45 +00:00
|
|
|
if todo_params.except(:sort, :page).empty?
|
2019-09-03 17:45:00 +00:00
|
|
|
(current_user.todos_pending_count.to_f / todos.limit_value).ceil
|
|
|
|
else
|
|
|
|
todos.total_pages
|
2017-10-04 16:37:38 +00:00
|
|
|
end
|
2019-09-03 17:45:00 +00:00
|
|
|
end
|
2017-10-04 16:37:38 +00:00
|
|
|
|
2019-09-03 17:45:00 +00:00
|
|
|
def todo_params
|
|
|
|
params.permit(:action_id, :author_id, :project_id, :type, :sort, :state, :group_id)
|
2017-10-04 16:37:38 +00:00
|
|
|
end
|
2016-02-20 13:59:59 +00:00
|
|
|
end
|