Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

renaming :class option to :resource for load_and_authorize_resource w…

…hich now supports a symbol for non models - closes #45
  • Loading branch information...
commit 23a5888fe02dd1f1161605d0133a3e9e3eb61eda 1 parent f2a1695
@ryanb authored
View
6 CHANGELOG.rdoc
@@ -1,6 +1,10 @@
1.1.0 (not released)
-* Adding be_able_to RSpec matcher (thanks dchelimsky) - see issue #54
+* Renaming :class option to :resource for load_and_authorize_resource which now supports a symbol for non models - see issue #45
+
+* Properly handle Admin::AbilitiesController in params[:controller] - see issue #46
+
+* Adding be_able_to RSpec matcher (thanks dchelimsky), requires Ruby 1.8.7 or higher - see issue #54
* Support additional arguments to can? which get passed to the block - see issue #48
View
15 lib/cancan/controller_additions.rb
@@ -12,7 +12,7 @@ module ClassMethods
# end
#
def load_and_authorize_resource(options = {})
- before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_and_authorize_resource }
+ ResourceAuthorization.add_before_filter(self, :load_and_authorize_resource, options)
end
# Sets up a before filter which loads the appropriate model resource into an instance variable.
@@ -59,8 +59,8 @@ def load_and_authorize_resource(options = {})
#
# load_resource :nested => [:publisher, :author]
#
- # [:+class+]
- # The class to use for the model.
+ # [:+resource+]
+ # The class to use for the model (string or constant).
#
# [:+collection+]
# Specify which actions are resource collection actions in addition to :+index+. This
@@ -77,7 +77,7 @@ def load_and_authorize_resource(options = {})
# load_resource :new => :build
#
def load_resource(options = {})
- before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_resource }
+ ResourceAuthorization.add_before_filter(self, :load_resource, options)
end
# Sets up a before filter which authorizes the current resource using the instance variable.
@@ -102,11 +102,12 @@ def load_resource(options = {})
# [:+except+]
# Does not apply before filter to given actions.
#
- # [:+class+]
- # The class to use for the model.
+ # [:+resource+]
+ # The class to use for the model (string or constant). Alternatively pass a symbol
+ # to represent a resource which does not have a class.
#
def authorize_resource(options = {})
- before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).authorize_resource }
+ ResourceAuthorization.add_before_filter(self, :authorize_resource, options)
end
end
View
9 lib/cancan/controller_resource.rb
@@ -1,6 +1,7 @@
module CanCan
class ControllerResource # :nodoc:
def initialize(controller, name, parent = nil, options = {})
+ raise "The :class option has been renamed to :resource for specifying the class in CanCan." if options.has_key? :class
@controller = controller
@name = name
@parent = parent
@@ -8,7 +9,13 @@ def initialize(controller, name, parent = nil, options = {})
end
def model_class
- @options[:class] || @name.to_s.camelize.constantize
+ if @options[:resource].nil?
+ @name.to_s.camelize.constantize
+ elsif @options[:resource].kind_of? String
+ @options[:resource].constantize
+ else
+ @options[:resource]
+ end
end
def find(id)
View
6 lib/cancan/resource_authorization.rb
@@ -2,6 +2,12 @@ module CanCan
class ResourceAuthorization # :nodoc:
attr_reader :params
+ def self.add_before_filter(controller_class, method, options = {})
+ controller_class.before_filter(options.slice(:only, :except)) do |controller|
+ new(controller, controller.params, options.except(:only, :except)).send(method)
+ end
+ end
+
def initialize(controller, params, options = {})
@controller = controller
@params = params
View
12 spec/cancan/controller_resource_spec.rb
@@ -43,7 +43,17 @@
it "should use the model class option if provided" do
stub(Person).find(123) { :some_resource }
- CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person).find(123)
+ CanCan::ControllerResource.new(@controller, :ability, nil, :resource => Person).find(123)
@controller.instance_variable_get(:@ability).should == :some_resource
end
+
+ it "should convert string to constant for resource" do
+ CanCan::ControllerResource.new(@controller, :ability, nil, :resource => "Person").model_class.should == Person
+ end
+
+ it "should raise an exception when specifying :class option since it is no longer used" do
+ lambda {
+ CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person)
+ }.should raise_error
+ end
end
View
2  spec/cancan/resource_authorization_spec.rb
@@ -115,7 +115,7 @@
it "should load the model using a custom class" do
stub(Person).find(123) { :some_resource }
- authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:class => Person})
+ authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:resource => Person})
authorization.load_resource
@controller.instance_variable_get(:@ability).should == :some_resource
end
Please sign in to comment.
Something went wrong with that request. Please try again.