Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
Fix Rails 5 before_filter deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
dteoh committed Jul 7, 2016
1 parent 819161a commit d339ebc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/authority/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/authority/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down

0 comments on commit d339ebc

Please sign in to comment.