Module: CanCan::ControllerAdditions::ClassMethods
- Defined in:
- lib/cancan/controller_additions.rb
Instance Method Summary collapse
-
#authorize_resource(*args) ⇒ Object
Sets up a before filter which authorizes the resource using the instance variable.
-
#load_and_authorize_resource(*args) ⇒ Object
Sets up a before filter which loads and authorizes the current resource.
-
#load_resource(*args) ⇒ Object
Sets up a before filter which loads the model resource into an instance variable.
Instance Method Details
#authorize_resource(*args) ⇒ Object
Sets up a before filter which authorizes the resource using the instance variable. For example, if you have an ArticlesController it will check the @article instance variable and ensure the user can perform the current action on it. Under the hood it is doing something like the following.
(params[:action].to_sym, @article || Article)
Call this method directly on the controller class.
class BooksController < ApplicationController
end
If you pass in the name of a resource which does not match the controller it will assume it is a parent resource.
class BooksController < ApplicationController
:author
:book
end
Here it will authorize :show
, @author
on every action before authorizing the book.
That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it.
See load_and_authorize_resource to automatically load the resource too.
Options:
- :
only
-
Only applies before filter to given actions.
- :
except
-
Does not apply before filter to given actions.
- :
parent
-
True or false depending on if the resource is considered a parent resource. This defaults to
true
if a resource name is given which does not match the controller. - :
class
-
The class to use for the model (string or constant). This passed in when the instance variable is not set. Pass
false
if there is no associated class for this resource and it will use a symbol of the resource name. - :
instance_name
-
The name of the instance variable for this resource.
151 152 153 |
# File 'lib/cancan/controller_additions.rb', line 151 def (*args) ControllerResource.add_before_filter(self, :authorize_resource, *args) end |
#load_and_authorize_resource(*args) ⇒ Object
Sets up a before filter which loads and authorizes the current resource. This performs both load_resource and authorize_resource and accepts the same arguments. See those methods for details.
class BooksController < ApplicationController
end
14 15 16 |
# File 'lib/cancan/controller_additions.rb', line 14 def (*args) ControllerResource.add_before_filter(self, :load_and_authorize_resource, *args) end |
#load_resource(*args) ⇒ Object
Sets up a before filter which loads the model resource into an instance variable. For example, given an ArticlesController it will load the current article into the @article instance variable. It does this by either calling Article.find(params) or Article.new(params) depending upon the action. It does nothing for the “index” action.
Call this method directly on the controller class.
class BooksController < ApplicationController
load_resource
end
A resource is not loaded if the instance variable is already set. This makes it easy to override the behavior through a before_filter on certain actions.
class BooksController < ApplicationController
before_filter :find_book_by_permalink, :only => :show
load_resource
private
def find_book_by_permalink
@book = Book.find_by_permalink!(params[:id)
end
end
If a name is provided which does not match the controller it assumes it is a parent resource. Child resources can then be loaded through it.
class BooksController < ApplicationController
load_resource :author
load_resource :book, :through => :author
end
Here the author resource will be loaded before each action using params. The book resource will then be loaded through the @author instance variable.
That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it.
See load_and_authorize_resource to automatically authorize the resource too.
Options:
- :
only
-
Only applies before filter to given actions.
- :
except
-
Does not apply before filter to given actions.
- :
through
-
Load this resource through another one. This should match the name of the parent instance variable.
- :
singleton
-
Pass
true
if this is a singleton resource through ahas_one
association. - :
parent
-
True or false depending on if the resource is considered a parent resource. This defaults to
true
if a resource name is given which does not match the controller. - :
class
-
The class to use for the model (string or constant).
- :
instance_name
-
The name of the instance variable to load the resource into.
- :
find_by
-
Find using a different attribute other than id. For example.
load_resource :find_by => :permalink # will use find_by_permlink!(params[:id])
- :
collection
-
Specify which actions are resource collection actions in addition to :
index
. This is usually not necessary because it will try to guess depending on if the id param is present.load_resource :collection => [:sort, :list]
- :
new
-
Specify which actions are new resource actions in addition to :
new
and :create
. Pass an action name into here if you would like to build a new resource instead of fetch one.load_resource :new => :build
101 102 103 |
# File 'lib/cancan/controller_additions.rb', line 101 def load_resource(*args) ControllerResource.add_before_filter(self, :load_resource, *args) end |