cancan 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,10 @@
1
+ 1.0.1 (Dec 14, 2009)
2
+
3
+ * Adding :class option to load_resource so one can customize which class to use for the model - see issue #17
4
+
5
+ * Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
6
+
7
+
1
8
  1.0.0 (Dec 13, 2009)
2
9
 
3
10
  * Don't set resource instance variable if it has been set already - see issue #13
@@ -65,11 +65,7 @@ Setting this for every action can be tedious, therefore the load_and_authorize_r
65
65
  If the user authorization fails, a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior in the ApplicationController.
66
66
 
67
67
  class ApplicationController < ActionController::Base
68
- rescue_from CanCan::AccessDenied, :with => :access_denied
69
-
70
- protected
71
-
72
- def access_denied
68
+ rescue_from CanCan::AccessDenied do |exception|
73
69
  flash[:error] = "Sorry, you are not allowed to access that page."
74
70
  redirect_to root_url
75
71
  end
@@ -59,6 +59,9 @@ module CanCan
59
59
  #
60
60
  # load_resource :nested => [:publisher, :author]
61
61
  #
62
+ # [:+class+]
63
+ # The class to use for the model.
64
+ #
62
65
  # [:+collection+]
63
66
  # Specify which actions are resource collection actions in addition to :+index+. This
64
67
  # is usually not necessary because it will try to guess depending on if an :+id+
@@ -72,7 +75,7 @@ module CanCan
72
75
  # fetch one.
73
76
  #
74
77
  # load_resource :new => :build
75
- #
78
+ #
76
79
  def load_resource(options = {})
77
80
  before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_resource }
78
81
  end
@@ -99,6 +102,9 @@ module CanCan
99
102
  # [:+except+]
100
103
  # Does not apply before filter to given actions.
101
104
  #
105
+ # [:+class+]
106
+ # The class to use for the model.
107
+ #
102
108
  def authorize_resource(options = {})
103
109
  before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).authorize_resource }
104
110
  end
@@ -1,13 +1,14 @@
1
1
  module CanCan
2
2
  class ControllerResource # :nodoc:
3
- def initialize(controller, name, parent = nil)
3
+ def initialize(controller, name, parent = nil, options = {})
4
4
  @controller = controller
5
5
  @name = name
6
6
  @parent = parent
7
+ @options = options
7
8
  end
8
9
 
9
10
  def model_class
10
- @name.to_s.camelize.constantize
11
+ @options[:class] || @name.to_s.camelize.constantize
11
12
  end
12
13
 
13
14
  def find(id)
@@ -30,14 +30,19 @@ module CanCan
30
30
  private
31
31
 
32
32
  def resource
33
- @resource ||= ControllerResource.new(@controller, model_name, parent_resource)
33
+ @resource ||= ControllerResource.new(@controller, model_name, parent_resource, @options)
34
34
  end
35
35
 
36
36
  def parent_resource
37
37
  parent = nil
38
38
  [@options[:nested]].flatten.compact.each do |name|
39
- parent = ControllerResource.new(@controller, name, parent)
40
- parent.find(@params["#{name}_id".to_sym])
39
+ id = @params["#{name}_id".to_sym]
40
+ if id
41
+ parent = ControllerResource.new(@controller, name, parent)
42
+ parent.find(id)
43
+ else
44
+ parent = nil
45
+ end
41
46
  end
42
47
  parent
43
48
  end
@@ -40,4 +40,10 @@ describe CanCan::ControllerResource do
40
40
  CanCan::ControllerResource.new(@controller, :ability).find(123)
41
41
  @controller.instance_variable_get(:@ability).should == :some_ability
42
42
  end
43
+
44
+ it "should use the model class option if provided" do
45
+ stub(Person).find(123) { :some_resource }
46
+ CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person).find(123)
47
+ @controller.instance_variable_get(:@ability).should == :some_resource
48
+ end
43
49
  end
@@ -96,4 +96,20 @@ describe CanCan::ResourceAuthorization do
96
96
  authorization.load_resource
97
97
  @controller.instance_variable_get(:@ability).should == :some_ability
98
98
  end
99
+
100
+ it "should not load nested resource and build through this if *_id param isn't specified" do
101
+ stub(Person).find(456) { :some_person }
102
+ stub(Ability).new(nil) { :some_ability }
103
+ authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456}, {:nested => [:person, :behavior]})
104
+ authorization.load_resource
105
+ @controller.instance_variable_get(:@person).should == :some_person
106
+ @controller.instance_variable_get(:@ability).should == :some_ability
107
+ end
108
+
109
+ it "should load the model using a custom class" do
110
+ stub(Person).find(123) { :some_resource }
111
+ authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:class => Person})
112
+ authorization.load_resource
113
+ @controller.instance_variable_get(:@ability).should == :some_resource
114
+ end
99
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cancan
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-13 00:00:00 -08:00
12
+ date: 2009-12-14 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15