module Flyboy class Ability include ::CanCan::Ability def initialize(user = nil) # Allowed actions (all by default) can [:list, :create, :read, :update, :delete, :open, :close], Flyboy::Goal can [:list, :create, :read, :update, :delete, :complete, :snooze], Flyboy::Task # Restricted actions cannot [:update, :delete], Flyboy::Goal do |goal| goal.closed? end cannot :close, Flyboy::Goal do |goal| not goal.may_close? end cannot :open, Flyboy::Goal do |goal| not goal.may_open? end cannot [:create, :update, :delete], Flyboy::Task do |task| task.goal.closed? end cannot :complete, Flyboy::Task do |task| task.done? end cannot :snooze, Flyboy::Task do |task| task.done? || task.reminder >= Date.today end end end end