From 63627d1439c2ed21e086e3eb1de6146f60c073ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sat, 4 May 2013 14:31:24 -0300 Subject: [PATCH] Refactoring the code a little bit * Rename #get_constraints_given_method to #constraints_for to make the method clear. * The method name now is required. * Use symbols instead of strings. * Make #constraints_for a protected method. --- lib/devise/rails/routes.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/devise/rails/routes.rb b/lib/devise/rails/routes.rb index a7b66c22..9c037892 100644 --- a/lib/devise/rails/routes.rb +++ b/lib/devise/rails/routes.rb @@ -237,16 +237,6 @@ module ActionDispatch::Routing end end - def get_constraints_given_method(scope=nil, block=nil, method_to_apply='authenticate!') - constraint = lambda do |request| - request.env['warden'].send(method_to_apply, {:scope => scope}) && (block.nil? || block.call(request.env["warden"].user(scope))) - end - - constraints(constraint) do - yield - end - end - # Allow you to add authentication request from the router. # Takes an optional scope and block to provide constraints # on the model instance itself. @@ -264,7 +254,7 @@ module ActionDispatch::Routing # end # def authenticate(scope=nil, block=nil) - get_constraints_given_method(scope, block, 'authenticate!') do + constraints_for(:authenticate!, scope, block) do yield end end @@ -288,7 +278,7 @@ module ActionDispatch::Routing # root :to => 'landing#show' # def authenticated(scope=nil, block=nil) - get_constraints_given_method(scope, block, 'authenticate?') do + constraints_for(:authenticate?, scope, block) do yield end end @@ -430,6 +420,17 @@ module ActionDispatch::Routing @scope.merge!(old) end + def constraints_for(method_to_apply, scope=nil, block=nil) + constraint = lambda do |request| + request.env['warden'].send(method_to_apply, :scope => scope) && + (block.nil? || block.call(request.env["warden"].user(scope))) + end + + constraints(constraint) do + yield + end + end + def set_omniauth_path_prefix!(path_prefix) #:nodoc: if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix raise "Wrong OmniAuth configuration. If you are getting this exception, it means that either:\n\n" \