Browse files

Don't set resource instance variable if it has been set already - clo…

…ses #13
  • Loading branch information...
1 parent a75aee7 commit ffa677b2b06706495ef0409d72fcd3c775f5721b @ryanb committed Dec 13, 2009
View
2 CHANGELOG.rdoc
@@ -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
* Adding :nested option to load resource method - see issue #10
View
22 lib/cancan/controller_additions.rb
@@ -4,8 +4,8 @@ module CanCan
# It also makes the "can?" and "cannot?" methods available to all views.
module ControllerAdditions
module ClassMethods
- # Sets up a before filter which loads and authorizes the current resource. This accepts the
- # same arguments as load_resource and authorize_resource. See those methods for details.
+ # 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
# load_and_authorize_resource
@@ -21,12 +21,26 @@ def load_and_authorize_resource(options = {})
# Article.new(params[:article]) depending upon the action. It does nothing for the "index"
# action.
#
- # You would call this method directly on the controller class.
+ # 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
+ #
# See load_and_authorize_resource to automatically authorize the resource too.
#
# Options:
@@ -70,7 +84,7 @@ def load_resource(options = {})
#
# 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
# authorize_resource
View
6 lib/cancan/controller_resource.rb
@@ -11,14 +11,14 @@ def model_class
end
def find(id)
- self.model_instance = base.find(id)
+ self.model_instance ||= base.find(id)
end
def build(attributes)
if base.kind_of? Class
- self.model_instance = base.new(attributes)
+ self.model_instance ||= base.new(attributes)
else
- self.model_instance = base.build(attributes)
+ self.model_instance ||= base.build(attributes)
end
end
View
6 spec/cancan/controller_resource_spec.rb
@@ -34,4 +34,10 @@
CanCan::ControllerResource.new(@controller, :ability, parent).build(123)
@controller.instance_variable_get(:@ability).should == :some_ability
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

0 comments on commit ffa677b

Please sign in to comment.