Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Renames deprecated before_filter method to before_action. #1038

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/cancan/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module ClassMethods
# end
#
def load_and_authorize_resource(*args)
cancan_resource_class.add_before_filter(self, :load_and_authorize_resource, *args)
cancan_resource_class.add_before_action(self, :load_and_authorize_resource, *args)
end

# Sets up a before filter which loads the model resource into an instance variable.
Expand All @@ -32,10 +32,10 @@ def load_and_authorize_resource(*args)
# 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.
# the behavior through a before_action on certain actions.
#
# class BooksController < ApplicationController
# before_filter :find_book_by_permalink, :only => :show
# before_action :find_book_by_permalink, :only => :show
# load_resource
#
# private
Expand Down Expand Up @@ -115,10 +115,10 @@ def load_and_authorize_resource(*args)
# load_resource :new => :build
#
# [:+prepend+]
# Passing +true+ will use prepend_before_filter instead of a normal before_filter.
# Passing +true+ will use prepend_before_action instead of a normal before_action.
#
def load_resource(*args)
cancan_resource_class.add_before_filter(self, :load_resource, *args)
cancan_resource_class.add_before_action(self, :load_resource, *args)
end

# Sets up a before filter which authorizes the resource using the instance variable.
Expand Down Expand Up @@ -174,10 +174,10 @@ def load_resource(*args)
# 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.
# Passing +true+ will use prepend_before_action instead of a normal before_action.
#
def authorize_resource(*args)
cancan_resource_class.add_before_filter(self, :authorize_resource, *args)
cancan_resource_class.add_before_action(self, :authorize_resource, *args)
end

# Skip both the loading and authorization behavior of CanCan for this given controller. This is primarily
Expand Down Expand Up @@ -268,9 +268,9 @@ def check_authorization(options = {})
# skip_authorization_check :only => :index
# end
#
# Any arguments are passed to the +before_filter+ it triggers.
# Any arguments are passed to the +before_action+ it triggers.
def skip_authorization_check(*args)
self.before_filter(*args) do |controller|
self.before_action(*args) do |controller|
controller.instance_variable_set(:@_authorized, true)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module CanCan
# Handle the load and authorization controller logic so we don't clutter up all controllers with non-interface methods.
# This class is used internally, so you do not need to call methods directly on it.
class ControllerResource # :nodoc:
def self.add_before_filter(controller_class, method, *args)
def self.add_before_action(controller_class, method, *args)
options = args.extract_options!
resource_name = args.first
before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
controller_class.send(before_filter_method, options.slice(:only, :except, :if, :unless)) do |controller|
before_action_method = options.delete(:prepend) ? :prepend_before_action : :before_action
controller_class.send(before_action_method, options.slice(:only, :except, :if, :unless)) do |controller|
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except, :if, :unless)).send(method)
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/cancan/controller_additions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@

it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do
stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.load_and_authorize_resource
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) }
mock(@controller_class).before_action({}) { |options, block| block.call(@controller) }
@controller_class.load_and_authorize_resource :foo => :bar
end

it "load_and_authorize_resource should properly pass first argument as the resource name" do
stub(CanCan::ControllerResource).new(@controller, :project, :foo => :bar).mock!.load_and_authorize_resource
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) }
mock(@controller_class).before_action({}) { |options, block| block.call(@controller) }
@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({})
mock(@controller_class).prepend_before_action({})
@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, :if => true) { |options, block| block.call(@controller) }
mock(@controller_class).before_action(:except => :show, :if => true) { |options, block| block.call(@controller) }
@controller_class.authorize_resource :foo => :bar, :except => :show, :if => true
end

it "load_resource should setup a before filter which passes call to ControllerResource" do
stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.load_resource
mock(@controller_class).before_filter(:only => [:show, :index], :unless => false) { |options, block| block.call(@controller) }
mock(@controller_class).before_action(:only => [:show, :index], :unless => false) { |options, block| block.call(@controller) }
@controller_class.load_resource :foo => :bar, :only => [:show, :index], :unless => false
end

it "skip_authorization_check should set up a before filter which sets @_authorized to true" do
mock(@controller_class).before_filter(:filter_options) { |options, block| block.call(@controller) }
mock(@controller_class).before_action(:filter_options) { |options, block| block.call(@controller) }
@controller_class.skip_authorization_check(:filter_options)
@controller.instance_variable_get(:@_authorized).should be_true
end
Expand Down