Skip to content

Commit

Permalink
Add :unless filter option
Browse files Browse the repository at this point in the history
This is a complement to the :only and :except filter options.
See #65

Fixes #155
  • Loading branch information
gonzalo-bulnes committed Apr 9, 2015
1 parent 60485fd commit 35a66de
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -106,6 +106,9 @@ class ApplicationController < ActionController::Base # or ActionController::API
# The token authentication requirement can target specific controller actions:
# acts_as_token_authentication_handler_for User, only: [:create, :update, :destroy]
# acts_as_token_authentication_handler_for User, except: [:index, :show]
#
# or specific controller conditions:
# acts_as_token_authentication_handler_for User, unless: lambda { |controller| controller.request.format.html? }

# Several token authenticatable models can be handled by the same controller.
# If so, for all of them except the last, the fallback_to_devise should be disabled.
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@ def set_token_authentication_hooks(entity, options)
else
:"authenticate_#{entity.name_underscore}_from_token"
end
before_filter authenticate_method, options.slice(:only, :except)
before_filter authenticate_method, options.slice(:only, :except, :unless)
end
end
end
20 changes: 20 additions & 0 deletions spec/configuration/action_controller_callbacks_options_spec.rb
Original file line number Diff line number Diff line change
@@ -50,4 +50,24 @@
end
end
end

describe ':unless option' do

context 'when provided to `acts_as_token_authentication_hanlder_for`' do

it 'is applied to the corresponding callback (1)', rspec_3_error: true, private: true do
some_class = @subjects.first

expect(some_class).to receive(:before_filter).with(:authenticate_user_from_token!, { unless: lambda { |controller| 'some condition' } })
some_class.acts_as_token_authentication_handler_for User, unless: lambda { |controller| 'some condition' }
end

it 'is applied to the corresponding callback (2)', rspec_3_error: true, private: true do
some_child_class = @subjects.last

expect(some_child_class).to receive(:before_filter).with(:authenticate_user_from_token!, { unless: lambda { |controller| 'some condition' } })
some_child_class.acts_as_token_authentication_handler_for User, unless: lambda { |controller| 'some condition' }
end
end
end
end

0 comments on commit 35a66de

Please sign in to comment.