From d339ebc387e6e930f454ab2f204b70920e019498 Mon Sep 17 00:00:00 2001 From: Douglas Teoh Date: Thu, 7 Jul 2016 10:15:49 +0900 Subject: [PATCH] Fix Rails 5 before_filter deprecation warning --- lib/authority/controller.rb | 6 +++++- spec/authority/controller_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/authority/controller.rb b/lib/authority/controller.rb index 29c5281..9eacde8 100644 --- a/lib/authority/controller.rb +++ b/lib/authority/controller.rb @@ -45,7 +45,11 @@ def authorize_actions_for(resource_or_finder, options = {}) self.authority_resource = resource_or_finder add_actions(options.fetch(:actions, {})) force_action(options[:all_actions]) if options[:all_actions] - before_filter :run_authorization_check, options + if respond_to? :before_action + before_action :run_authorization_check, options + else + before_filter :run_authorization_check, options + end end # Allows defining and overriding a controller's map of its actions to the model's authorizer methods diff --git a/spec/authority/controller_spec.rb b/spec/authority/controller_spec.rb index 4e3d850..9cc290e 100644 --- a/spec/authority/controller_spec.rb +++ b/spec/authority/controller_spec.rb @@ -89,6 +89,12 @@ def self.before_filter(*args) ; end let(:child_controller) { Class.new(controller_class) } + let(:rails5_controller) do + Class.new(controller_class) do + def self.before_action(*args) ; end + end + end + it "allows specifying the class of the model to protect" do controller_class.authorize_actions_for(resource_class) expect(controller_class.authority_resource).to eq(resource_class) @@ -105,6 +111,12 @@ def self.before_filter(*args) ; end controller_class.authorize_actions_for(resource_class, filter_options) end + it "prefers to set up a before_action over before_filter, passing the options it was given" do + filter_options = {:only => [:show, :edit, :update]} + expect(rails5_controller).to receive(:before_action).with(:run_authorization_check, filter_options) + rails5_controller.authorize_actions_for(resource_class, filter_options) + end + it "if :all_actions option is given, it overrides the action hash to use the action given" do overridden_action_map = controller_class.authority_action_map overridden_action_map.update(overridden_action_map) {|k,v| v = :annihilate}