Don't set resource instance variable if it has been set already - closes #13
This commit is contained in:
parent
a75aee751b
commit
ffa677b2b0
|
@ -1,3 +1,5 @@
|
||||||
|
* Don't set resource instance variable if it has been set already - see issue #13
|
||||||
|
|
||||||
* Allowing :nested option to accept an array for deep nesting
|
* Allowing :nested option to accept an array for deep nesting
|
||||||
|
|
||||||
* Adding :nested option to load resource method - see issue #10
|
* Adding :nested option to load resource method - see issue #10
|
||||||
|
|
|
@ -4,8 +4,8 @@ module CanCan
|
||||||
# It also makes the "can?" and "cannot?" methods available to all views.
|
# It also makes the "can?" and "cannot?" methods available to all views.
|
||||||
module ControllerAdditions
|
module ControllerAdditions
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Sets up a before filter which loads and authorizes the current resource. This accepts the
|
# Sets up a before filter which loads and authorizes the current resource. This performs both
|
||||||
# same arguments as load_resource and authorize_resource. See those methods for details.
|
# load_resource and authorize_resource and accepts the same arguments. See those methods for details.
|
||||||
#
|
#
|
||||||
# class BooksController < ApplicationController
|
# class BooksController < ApplicationController
|
||||||
# load_and_authorize_resource
|
# load_and_authorize_resource
|
||||||
|
@ -21,12 +21,26 @@ module CanCan
|
||||||
# Article.new(params[:article]) depending upon the action. It does nothing for the "index"
|
# Article.new(params[:article]) depending upon the action. It does nothing for the "index"
|
||||||
# action.
|
# action.
|
||||||
#
|
#
|
||||||
# You would call this method directly on the controller class.
|
# Call this method directly on the controller class.
|
||||||
#
|
#
|
||||||
# class BooksController < ApplicationController
|
# class BooksController < ApplicationController
|
||||||
# load_resource
|
# load_resource
|
||||||
# end
|
# 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
|
||||||
|
#
|
||||||
# See load_and_authorize_resource to automatically authorize the resource too.
|
# See load_and_authorize_resource to automatically authorize the resource too.
|
||||||
#
|
#
|
||||||
# Options:
|
# Options:
|
||||||
|
@ -70,7 +84,7 @@ module CanCan
|
||||||
#
|
#
|
||||||
# unauthorized! if cannot?(params[:action].to_sym, @article || Article)
|
# unauthorized! if cannot?(params[:action].to_sym, @article || Article)
|
||||||
#
|
#
|
||||||
# You would call this method directly on the controller class.
|
# Call this method directly on the controller class.
|
||||||
#
|
#
|
||||||
# class BooksController < ApplicationController
|
# class BooksController < ApplicationController
|
||||||
# authorize_resource
|
# authorize_resource
|
||||||
|
|
|
@ -11,14 +11,14 @@ module CanCan
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(id)
|
def find(id)
|
||||||
self.model_instance = base.find(id)
|
self.model_instance ||= base.find(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build(attributes)
|
def build(attributes)
|
||||||
if base.kind_of? Class
|
if base.kind_of? Class
|
||||||
self.model_instance = base.new(attributes)
|
self.model_instance ||= base.new(attributes)
|
||||||
else
|
else
|
||||||
self.model_instance = base.build(attributes)
|
self.model_instance ||= base.build(attributes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -34,4 +34,10 @@ describe CanCan::ControllerResource do
|
||||||
CanCan::ControllerResource.new(@controller, :ability, parent).build(123)
|
CanCan::ControllerResource.new(@controller, :ability, parent).build(123)
|
||||||
@controller.instance_variable_get(:@ability).should == :some_ability
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should not load resource if instance variable is already provided" do
|
||||||
|
@controller.instance_variable_set(:@ability, :some_ability)
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability).find(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user