summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/todos_controller.rb
blob: 7857af9c5de644ae8d8d777922e86785c90b78a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Dashboard::TodosController < Dashboard::ApplicationController
  before_action :find_todos, only: [:index, :destroy, :destroy_all]

  def index
    @todos = @todos.page(params[:page]).per(PER_PAGE)
  end

  def destroy
    todo.done!

    todo_notice = 'Todo was successfully marked as done.'

    respond_to do |format|
      format.html { redirect_to dashboard_todos_path, notice: todo_notice }
      format.js { render nothing: true }
      format.json do
        render json: { count: @todos.size, done_count: current_user.todos.done.count }
      end
    end
  end

  def destroy_all
    @todos.each(&:done!)

    respond_to do |format|
      format.html { redirect_to dashboard_todos_path, notice: 'All todos were marked as done.' }
      format.js { render nothing: true }
      format.json do
        find_todos
        render json: { count: @todos.size, done_count: current_user.todos.done.count }
      end
    end
  end

  private

  def todo
    @todo ||= current_user.todos.find(params[:id])
  end

  def find_todos
    @todos = TodosFinder.new(current_user, params).execute
  end
end