Permalink
Browse files

adding :prepend option to load_and_authorize_resource - closes #290

  • Loading branch information...
1 parent 3a07d62 commit 951d70e057d8fec5ddbec612a19da24f17cc99f8 @ryanb committed Mar 8, 2011
View
6 lib/cancan/controller_additions.rb
@@ -109,6 +109,9 @@ def load_and_authorize_resource(*args)
#
# load_resource :new => :build
#
+ # [:+prepend+]
+ # Passing +true+ will use prepend_before_filter instead of a normal before_filter.
+ #
def load_resource(*args)
cancan_resource_class.add_before_filter(self, :load_resource, *args)
end
@@ -162,6 +165,9 @@ def load_resource(*args)
# [:+through+]
# Authorize conditions on this parent resource when instance isn't available.
#
+ # [:+prepend+]
+ # Passing +true+ will use prepend_before_filter instead of a normal before_filter.
+ #
def authorize_resource(*args)
cancan_resource_class.add_before_filter(self, :authorize_resource, *args)
end
View
3 lib/cancan/controller_resource.rb
@@ -5,7 +5,8 @@ class ControllerResource # :nodoc:
def self.add_before_filter(controller_class, method, *args)
options = args.extract_options!
resource_name = args.first
- controller_class.before_filter(options.slice(:only, :except)) do |controller|
+ before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
+ controller_class.send(before_filter_method, options.slice(:only, :except)) do |controller|
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except)).send(method)
end
end
View
5 spec/cancan/controller_additions_spec.rb
@@ -42,6 +42,11 @@
@controller_class.load_and_authorize_resource :project, :foo => :bar
end
+ it "load_and_authorize_resource with :prepend should prepend the before filter" do
+ mock(@controller_class).prepend_before_filter({})
+ @controller_class.load_and_authorize_resource :foo => :bar, :prepend => true
+ end
+
it "authorize_resource should setup a before filter which passes call to ControllerResource" do
stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.authorize_resource
mock(@controller_class).before_filter(:except => :show) { |options, block| block.call(@controller) }

0 comments on commit 951d70e

Please sign in to comment.