todos_controller.rb 1.04 KB
Newer Older
1
class Dashboard::TodosController < Dashboard::ApplicationController
2 3 4
  include TodosHelper

  before_action :find_todos, only: [:index, :destroy_all]
Douwe Maan's avatar
Douwe Maan committed
5

6
  def index
7
    @todos = @todos.page(params[:page])
8 9 10
  end

  def destroy
11
    TodoService.new.mark_todos_as_done([todo], current_user)
12 13

    respond_to do |format|
14
      format.html { redirect_to dashboard_todos_path, notice: 'Todo was successfully marked as done.' }
15
      format.js { head :ok }
16
      format.json { render json: { count: todos_pending_count, done_count: todos_done_count } }
17 18 19
    end
  end

Douwe Maan's avatar
Douwe Maan committed
20
  def destroy_all
21
    TodoService.new.mark_todos_as_done(@todos, current_user)
Douwe Maan's avatar
Douwe Maan committed
22 23 24

    respond_to do |format|
      format.html { redirect_to dashboard_todos_path, notice: 'All todos were marked as done.' }
25
      format.js { head :ok }
26
      format.json { render json: { count: todos_pending_count, done_count: todos_done_count } }
Douwe Maan's avatar
Douwe Maan committed
27 28 29
    end
  end

30 31 32
  private

  def todo
33
    @todo ||= find_todos.find(params[:id])
34
  end
Douwe Maan's avatar
Douwe Maan committed
35 36

  def find_todos
37
    @todos ||= TodosFinder.new(current_user, params).execute
Douwe Maan's avatar
Douwe Maan committed
38
  end
39
end