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
|
|
|
|
|
2016-06-15 02:09:48 +00:00
|
|
|
before_action :find_todos, only: [:index, :destroy_all]
|
2016-02-20 19:53:25 +00:00
|
|
|
|
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])
|
|
|
|
if @todos.out_of_range? && @todos.total_pages != 0
|
2017-04-05 22:52:19 +00:00
|
|
|
redirect_to url_for(params.merge(page: @todos.total_pages, only_path: true))
|
2016-12-20 18:52:09 +00:00
|
|
|
end
|
2016-02-20 13:59:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2017-04-29 12:29:59 +00:00
|
|
|
TodoService.new.mark_todos_as_done_by_ids([params[:id]], current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2016-06-15 02:09:48 +00:00
|
|
|
format.html { redirect_to dashboard_todos_path, notice: 'Todo was successfully marked as done.' }
|
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
|
2017-01-29 09:44:30 +00:00
|
|
|
updated_ids = TodoService.new.mark_todos_as_done(@todos, current_user)
|
2016-02-20 19:53:25 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to dashboard_todos_path, notice: 'All todos were 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
|
2017-04-29 12:29:59 +00:00
|
|
|
TodoService.new.mark_todos_as_pending_by_ids([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
|
|
|
|
TodoService.new.mark_todos_as_pending_by_ids(params[:ids], current_user)
|
|
|
|
|
|
|
|
render json: todos_counts
|
|
|
|
end
|
|
|
|
|
2017-02-21 19:21:49 +00:00
|
|
|
# Used in TodosHelper also
|
|
|
|
def self.todos_count_format(count)
|
|
|
|
count >= 100 ? '99+' : count
|
|
|
|
end
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
private
|
|
|
|
|
2016-02-20 19:53:25 +00:00
|
|
|
def find_todos
|
2017-03-21 15:25:00 +00:00
|
|
|
@todos ||= TodosFinder.new(current_user, params).execute
|
2016-02-20 19:53:25 +00:00
|
|
|
end
|
2016-07-11 06:10:04 +00:00
|
|
|
|
|
|
|
def todos_counts
|
|
|
|
{
|
2017-02-22 07:19:09 +00:00
|
|
|
count: number_with_delimiter(current_user.todos_pending_count),
|
|
|
|
done_count: number_with_delimiter(current_user.todos_done_count)
|
2016-07-11 06:10:04 +00:00
|
|
|
}
|
|
|
|
end
|
2016-02-20 13:59:59 +00:00
|
|
|
end
|