Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
little fixes to inline documentation (rdocs)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Nov 19, 2009
1 parent 0ae41f5 commit 5bd1a85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 29 additions & 3 deletions lib/cancan/ability.rb
Expand Up @@ -17,14 +17,22 @@ module CanCan
# #
module Ability module Ability
attr_accessor :user attr_accessor :user


# Use to check the user's permission for a given action and object.
#
# can? :destroy, @project
#
# You can also pass the class instead of an instance (if you don't have one handy).
#
# can? :create, Project
#
# Not only can you use the can? method in the controller and view (see ControllerAdditions), # Not only can you use the can? method in the controller and view (see ControllerAdditions),
# but you can also call it directly on an ability instance. # but you can also call it directly on an ability instance.
# #
# ability.can? :destroy, @project # ability.can? :destroy, @project
# #
# This makes testing a user's abilities very easy. # This makes testing a user's abilities very easy.
# #
# def test "user can only destroy projects which he owns" # def test "user can only destroy projects which he owns"
# user = User.new # user = User.new
# ability = Ability.new(user) # ability = Ability.new(user)
Expand Down Expand Up @@ -103,17 +111,35 @@ def can(action, target, &block)
@can_history << [action, target, block] @can_history << [action, target, block]
end end


# Finally, you can use the "alias_action" method to alias one or more actions into one. # Alias one or more actions into another one.
# #
# alias_action :update, :destroy, :to => :modify # alias_action :update, :destroy, :to => :modify
# can :modify, Comment # can :modify, Comment
# #
# Then :modify permission will apply to both :update and :destroy requests.
#
# can? :update, Comment # => true
# can? :destroy, Comment # => true
#
# This only works in one direction. Passing the aliased action into the "can?" call
# will not work because aliases are meant to generate more generic actions.
#
# alias_action :update, :destroy, :to => :modify
# can :update, Comment
# can? :modify, Comment # => false
#
# Unless that exact alias is used.
#
# can :modify, Comment
# can? :modify, Comment # => true
#
# The following aliases are added by default for conveniently mapping common controller actions. # The following aliases are added by default for conveniently mapping common controller actions.
# #
# alias_action :index, :show, :to => :read # alias_action :index, :show, :to => :read
# alias_action :new, :to => :create # alias_action :new, :to => :create
# alias_action :edit, :to => :update # alias_action :edit, :to => :update
# #
# This way one can use params[:action] in the controller to determine the permission.
def alias_action(*args) def alias_action(*args)
@aliased_actions ||= default_alias_actions @aliased_actions ||= default_alias_actions
target = args.pop[:to] target = args.pop[:to]
Expand Down
6 changes: 3 additions & 3 deletions lib/cancan/controller_additions.rb
Expand Up @@ -47,8 +47,8 @@ def current_ability
::Ability.new(current_user) ::Ability.new(current_user)
end end


# Use the "can?" method in the controller or view to check the user's permission # Use in the controller or view to check the user's permission for a given action
# for a given action and object. # and object.
# #
# can? :destroy, @project # can? :destroy, @project
# #
Expand All @@ -58,7 +58,7 @@ def current_ability
# <%= link_to "New Project", new_project_path %> # <%= link_to "New Project", new_project_path %>
# <% end %> # <% end %>
# #
# This simply calls "can?" on the current_ability. # This simply calls "can?" on the current_ability. See Ability#can?.
def can?(*args) def can?(*args)
(@current_ability ||= current_ability).can?(*args) (@current_ability ||= current_ability).can?(*args)
end end
Expand Down

0 comments on commit 5bd1a85

Please sign in to comment.