todos_controller.rb 1.7 KB
Newer Older
1
class Dashboard::TodosController < Dashboard::ApplicationController
2 3
  include ActionView::Helpers::NumberHelper

4
  before_action :find_todos, only: [:index, :destroy_all]
D
Douwe Maan 已提交
5

6
  def index
F
Felipe Artur 已提交
7
    @sort = params[:sort]
8 9
    @todos = @todos.page(params[:page])
    if @todos.out_of_range? && @todos.total_pages != 0
10
      redirect_to url_for(params.merge(page: @todos.total_pages, only_path: true))
11
    end
12 13 14
  end

  def destroy
15
    TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
16 17

    respond_to do |format|
18
      format.html { redirect_to dashboard_todos_path, notice: 'Todo was successfully marked as done.' }
19
      format.js { head :ok }
20
      format.json { render json: todos_counts }
21 22 23
    end
  end

D
Douwe Maan 已提交
24
  def destroy_all
J
Jacopo 已提交
25
    updated_ids = TodoService.new.mark_todos_as_done(@todos, current_user)
D
Douwe Maan 已提交
26 27 28

    respond_to do |format|
      format.html { redirect_to dashboard_todos_path, notice: 'All todos were marked as done.' }
29
      format.js { head :ok }
J
Jacopo 已提交
30
      format.json { render json: todos_counts.merge(updated_ids: updated_ids) }
D
Douwe Maan 已提交
31 32 33
    end
  end

J
Jacopo 已提交
34
  def restore
35
    TodoService.new.mark_todos_as_pending_by_ids(params[:id], current_user)
J
Jacopo 已提交
36 37 38 39

    render json: todos_counts
  end

J
Jacopo 已提交
40 41 42 43 44 45
  def bulk_restore
    TodoService.new.mark_todos_as_pending_by_ids(params[:ids], current_user)

    render json: todos_counts
  end

46 47 48 49 50
  # Used in TodosHelper also
  def self.todos_count_format(count)
    count >= 100 ? '99+' : count
  end

51 52
  private

D
Douwe Maan 已提交
53
  def find_todos
54
    @todos ||= TodosFinder.new(current_user, params).execute
D
Douwe Maan 已提交
55
  end
56 57 58

  def todos_counts
    {
59 60
      count: number_with_delimiter(current_user.todos_pending_count),
      done_count: number_with_delimiter(current_user.todos_done_count)
61 62
    }
  end
63
end