2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
class Dashboard::TodosController < Dashboard::ApplicationController
|
2017-02-22 02:19:09 -05:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2017-06-13 13:14:14 -04:00
|
|
|
before_action :authorize_read_project!, only: :index
|
2018-11-28 13:36:59 -05:00
|
|
|
before_action :authorize_read_group!, only: :index
|
2016-06-14 22:09:48 -04:00
|
|
|
before_action :find_todos, only: [:index, :destroy_all]
|
2016-02-20 14:53:25 -05:00
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
def index
|
2016-07-26 17:21:20 -04:00
|
|
|
@sort = params[:sort]
|
2016-12-20 13:52:09 -05:00
|
|
|
@todos = @todos.page(params[:page])
|
2019-06-24 13:48:50 -04:00
|
|
|
@todos = @todos.with_entity_associations
|
2017-10-04 12:37:38 -04:00
|
|
|
|
|
|
|
return if redirect_out_of_range(@todos)
|
2016-02-20 08:59:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2017-04-21 05:36:34 -04:00
|
|
|
TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-06 18:45:16 -04:00
|
|
|
format.html do
|
|
|
|
redirect_to dashboard_todos_path,
|
|
|
|
status: 302,
|
2019-07-04 11:45:54 -04:00
|
|
|
notice: _('To-do item successfully marked as done.')
|
2017-06-06 18:45:16 -04:00
|
|
|
end
|
2016-03-15 21:16:25 -04:00
|
|
|
format.js { head :ok }
|
2016-07-11 02:10:04 -04:00
|
|
|
format.json { render json: todos_counts }
|
2016-02-20 08:59:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-20 14:53:25 -05:00
|
|
|
def destroy_all
|
2017-01-29 04:44:30 -05:00
|
|
|
updated_ids = TodoService.new.mark_todos_as_done(@todos, current_user)
|
2016-02-20 14:53:25 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
2019-07-04 11:45:54 -04:00
|
|
|
format.html { redirect_to dashboard_todos_path, status: 302, notice: _('Everything on your to-do list is marked as done.') }
|
2016-03-15 21:16:25 -04:00
|
|
|
format.js { head :ok }
|
2017-01-29 04:44:30 -05:00
|
|
|
format.json { render json: todos_counts.merge(updated_ids: updated_ids) }
|
2016-02-20 14:53:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-16 08:11:08 -05:00
|
|
|
def restore
|
2017-04-21 05:36:34 -04:00
|
|
|
TodoService.new.mark_todos_as_pending_by_ids(params[:id], current_user)
|
2017-01-16 08:11:08 -05:00
|
|
|
|
|
|
|
render json: todos_counts
|
|
|
|
end
|
|
|
|
|
2017-01-29 04:44:30 -05:00
|
|
|
def bulk_restore
|
|
|
|
TodoService.new.mark_todos_as_pending_by_ids(params[:ids], current_user)
|
|
|
|
|
|
|
|
render json: todos_counts
|
|
|
|
end
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
private
|
|
|
|
|
2017-06-13 13:14:14 -04: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 13:36:59 -05: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 14:53:25 -05:00
|
|
|
def find_todos
|
2017-10-04 12:37:38 -04:00
|
|
|
@todos ||= TodosFinder.new(current_user, todo_params).execute
|
2016-02-20 14:53:25 -05:00
|
|
|
end
|
2016-07-11 02:10:04 -04:00
|
|
|
|
|
|
|
def todos_counts
|
|
|
|
{
|
2017-02-22 02:19:09 -05:00
|
|
|
count: number_with_delimiter(current_user.todos_pending_count),
|
|
|
|
done_count: number_with_delimiter(current_user.todos_done_count)
|
2016-07-11 02:10:04 -04:00
|
|
|
}
|
|
|
|
end
|
2017-10-04 12:37:38 -04:00
|
|
|
|
|
|
|
def todo_params
|
2018-07-16 09:35:19 -04:00
|
|
|
params.permit(:action_id, :author_id, :project_id, :type, :sort, :state, :group_id)
|
2017-10-04 12:37:38 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-10-04 12:37:38 -04:00
|
|
|
def redirect_out_of_range(todos)
|
|
|
|
total_pages =
|
|
|
|
if todo_params.except(:sort, :page).empty?
|
2017-11-09 08:13:48 -05:00
|
|
|
(current_user.todos_pending_count.to_f / todos.limit_value).ceil
|
2017-10-04 12:37:38 -04:00
|
|
|
else
|
|
|
|
todos.total_pages
|
|
|
|
end
|
|
|
|
|
|
|
|
return false if total_pages.zero?
|
|
|
|
|
|
|
|
out_of_range = todos.current_page > total_pages
|
|
|
|
|
|
|
|
if out_of_range
|
2018-04-08 00:35:30 -04:00
|
|
|
redirect_to url_for(safe_params.merge(page: total_pages, only_path: true))
|
2017-10-04 12:37:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
out_of_range
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-02-20 08:59:59 -05:00
|
|
|
end
|