Skip to content

Commit

Permalink
Easier to define non-Model related abilities, such
Browse files Browse the repository at this point in the history
as :administer_site for example, which is only
based on the user object.

I wanted to be able to set specific abilities that
are not related to any specific model, but rather
based on the user object alone. Why use cancan for
that though? Cause I like having all my ability
definition in one place, cause then there's just
one if statement to modify instead of multiple
if statements all over acting directly on the user
object.

It was doable with cancan, but you had to call
`can?(:administer, nil)` as the second argument
was not optional. So I simply removed the need
for the second argument by defaulting it to nil.
  • Loading branch information
jimeh committed Apr 3, 2010
1 parent 5d4138f commit fc5431b
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/cancan/ability.rb
Expand Up @@ -40,7 +40,7 @@ module Ability
# assert ability.cannot?(:destroy, Project.new)
# end
#
def can?(action, noun)
def can?(action, noun = nil)
(@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
defined_actions = expand_actions(defined_action)
defined_nouns = [defined_noun].flatten
Expand Down Expand Up @@ -103,7 +103,7 @@ def cannot?(*args)
# can :read, :stats
# can? :read, :stats # => true
#
def can(action, noun, &block)
def can(action, noun = :all, &block)
@can_definitions ||= []
@can_definitions << [true, action, noun, block]
end
Expand All @@ -120,7 +120,7 @@ def can(action, noun, &block)
# product.invisible?
# end
#
def cannot(action, noun, &block)
def cannot(action, noun = :all, &block)
@can_definitions ||= []
@can_definitions << [false, action, noun, block]
end
Expand Down

0 comments on commit fc5431b

Please sign in to comment.