Skip to content

Commit

Permalink
Pre rails 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
craig1410 committed May 16, 2016
1 parent c915d3f commit 772c0d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/cancan/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,16 @@ def skip_authorize_resource(*args)
# check_authorization :unless => :devise_controller?
#
def check_authorization(options = {})
self.after_action(options.slice(:only, :except)) do |controller|
method_name = ActiveSupport.respond_to?(:version) && ActiveSupport.version >= Gem::Version.new("4") ? :after_action : :after_filter

block = Proc.new do |controller|
next if controller.instance_variable_defined?(:@_authorized)
next if options[:if] && !controller.send(options[:if])
next if options[:unless] && controller.send(options[:unless])
raise AuthorizationNotPerformed, "This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check."
end

self.send(method_name, options.slice(:only, :except), &block)
end

# Call this in the class of a controller to skip the check_authorization behavior on the actions.
Expand All @@ -270,9 +274,9 @@ def check_authorization(options = {})
#
# Any arguments are passed to the +before_action+ it triggers.
def skip_authorization_check(*args)
self.before_action(*args) do |controller|
controller.instance_variable_set(:@_authorized, true)
end
method_name = ActiveSupport.respond_to?(:version) && ActiveSupport.version >= Gem::Version.new("4") ? :before_action : :before_filter
block = Proc.new{ |controller| controller.instance_variable_set(:@_authorized, true) }
self.send(method_name, *args, &block)
end

def skip_authorization(*args)
Expand Down
10 changes: 9 additions & 1 deletion lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ class ControllerResource # :nodoc:
def self.add_before_action(controller_class, method, *args)
options = args.extract_options!
resource_name = args.first
before_action_method = options.delete(:prepend) ? :prepend_before_action : :before_action
before_action_method = before_callback_name(options)
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

def self.before_callback_name(options)
if ActiveSupport.respond_to?(:version) && ActiveSupport.version >= Gem::Version.new("4")
options.delete(:prepend) ? :prepend_before_action : :before_action
else
options.delete(:prepend) ? :prepend_before_filter : :before_filter
end
end

def initialize(controller, *args)
@controller = controller
@params = controller.params
Expand Down

0 comments on commit 772c0d2

Please sign in to comment.