Allow user to mark each task as done manually

This commit is contained in:
Douglas Barbosa Alexandre 2016-02-15 17:49:28 -02:00
parent c8f2d18abd
commit 77d7910b8b
5 changed files with 43 additions and 1 deletions

View File

@ -1,4 +1,6 @@
class Dashboard::TasksController < Dashboard::ApplicationController
before_action :authorize_destroy_task!, only: [:destroy]
def index
@tasks = case params[:state]
when 'done'
@ -12,4 +14,25 @@ class Dashboard::TasksController < Dashboard::ApplicationController
@pending_count = current_user.tasks.pending.count
@done_count = current_user.tasks.done.count
end
def destroy
task.done!
respond_to do |format|
format.html { redirect_to dashboard_tasks_path, notice: 'Task was successfully marked as done.' }
format.js { render nothing: true }
end
end
private
def authorize_destroy_task!
unless can?(current_user, :destroy_task, task)
return render_404
end
end
def task
@task ||= current_user.tasks.find(params[:id])
end
end

View File

@ -17,6 +17,7 @@ class Ability
when Namespace then namespace_abilities(user, subject)
when GroupMember then group_member_abilities(user, subject)
when ProjectMember then project_member_abilities(user, subject)
when Task then task_abilities(user, subject)
else []
end.concat(global_abilities(user))
end
@ -416,6 +417,16 @@ class Ability
rules
end
def task_abilities(user, task)
rules = []
if task && task.user == user
rules << :destroy_task
end
rules
end
def abilities
@abilities ||= begin
abilities = Six.new

View File

@ -32,6 +32,10 @@ class Task < ActiveRecord::Base
scope :done, -> { with_state(:done) }
state_machine :state, initial: :pending do
event :done do
transition pending: :done
end
state :pending
state :done
end

View File

@ -11,6 +11,10 @@
&middot; #{time_ago_with_tooltip(task.created_at)}
- if task.pending?
.task-actions.pull-right
= link_to 'Done', [:dashboard, task], method: :delete, class: 'btn'
- if task.body?
.task-body
.task-note

View File

@ -333,7 +333,7 @@ Rails.application.routes.draw do
resources :groups, only: [:index]
resources :snippets, only: [:index]
resources :tasks, only: [:index]
resources :tasks, only: [:index, :destroy]
resources :projects, only: [:index] do
collection do