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