Permalink
Browse files

Do not include Devise internal helpers in application controller.

  • Loading branch information...
1 parent b5256d9 commit dede8af5b099d79b1478e2a54e3aeb2c63c881b8 @josevalim josevalim committed Oct 27, 2009
@@ -1,5 +1,5 @@
class ConfirmationsController < ApplicationController
- before_filter :is_devise_resource?
+ include Devise::Controllers::Helpers
# GET /resource/confirmation/new
def new
@@ -1,5 +1,7 @@
class PasswordsController < ApplicationController
- before_filter :is_devise_resource?, :require_no_authentication
+ include Devise::Controllers::Helpers
+
+ before_filter :require_no_authentication
# GET /resource/password/new
def new
@@ -1,5 +1,6 @@
class SessionsController < ApplicationController
- before_filter :is_devise_resource?
+ include Devise::Controllers::Helpers
+
before_filter :require_no_authentication, :only => [ :new, :create ]
# GET /resource/sign_in
@@ -1,5 +1,7 @@
module Devise
module Controllers
+ # Those filters are convenience methods added to ApplicationController to
+ # deal with Warden.
module Filters
def self.included(base)
@@ -89,21 +91,6 @@ def #{mapping}_session
METHODS
end
- protected
-
- # Helper for use in before_filters where no authentication is required.
- #
- # Example:
- # before_filter :require_no_authentication, :only => :new
- def require_no_authentication
- redirect_to root_path if warden.authenticated?(resource_name)
- end
-
- # Checks whether it's a devise mapped resource or not.
- def is_devise_resource? #:nodoc:
- raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
- end
-
end
end
end
@@ -1,10 +1,16 @@
module Devise
module Controllers
+ # Those helpers are used only inside Devise controllers and should not be
+ # included in ApplicationController since they all depend on the url being
+ # accessed.
module Helpers
def self.included(base)
base.class_eval do
helper_method :resource, :resource_name, :resource_class, :devise_mapping
+ hide_action :resource, :resource_name, :resource_class, :devise_mapping
+
+ before_filter :is_devise_resource?
end
end
@@ -23,6 +29,11 @@ def resource_class
devise_mapping.to
end
+ # Attempt to find the mapped route for devise based on request path
+ def devise_mapping
+ @devise_mapping ||= Devise.find_mapping_by_path(request.path)
+ end
+
protected
# Redirects to stored uri before signing in or the default path and clear
@@ -57,16 +68,24 @@ def home_or_root_path
respond_to?(home_path, true) ? send(home_path) : root_path
end
- # Attempt to find the mapped route for devise based on request path
- def devise_mapping
- @devise_mapping ||= Devise.find_mapping_by_path(request.path)
+ # Checks whether it's a devise mapped resource or not.
+ def is_devise_resource? #:nodoc:
+ raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
end
# Sets the resource creating an instance variable
def resource=(new_resource)
instance_variable_set(:"@#{resource_name}", new_resource)
end
+ # Helper for use in before_filters where no authentication is required.
+ #
+ # Example:
+ # before_filter :require_no_authentication, :only => :new
+ def require_no_authentication
+ redirect_to root_path if warden.authenticated?(resource_name)
+ end
+
# Sets the flash message with :key, using I18n. By default you are able
# to setup your messages using specific resource scope, and if no one is
# found we look to default scope.
@@ -15,12 +15,14 @@ module Controllers
#
# new_confirmation_path(:user) => new_user_confirmation_path
# confirmation_path(:user) => user_confirmation_path
+ #
+ # Those helpers are added to your ApplicationController.
module UrlHelpers
[:session, :password, :confirmation].each do |module_name|
[:path, :url].each do |path_or_url|
actions = [ nil, :new_ ]
- actions << :edit_ if module_name == :password
+ actions << :edit_ if module_name == :password
actions << :destroy_ if module_name == :session
actions.each do |action|
View
@@ -8,7 +8,6 @@ def load_routes_with_devise!
load_routes_without_devise!
ActionController::Base.send :include, Devise::Controllers::Filters
- ActionController::Base.send :include, Devise::Controllers::Helpers
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
@@ -83,13 +83,6 @@ def setup
@controller.admin_session
end
- test 'require no authentication tests current mapping' do
- @controller.expects(:resource_name).returns(:user)
- @mock_warden.expects(:authenticated?).with(:user).returns(true)
- @controller.expects(:redirect_to).with(root_path)
- @controller.send :require_no_authentication
- end
-
test 'sign in automatically proxy to set user on warden' do
@mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
@controller.sign_in(:user, user)
@@ -1,7 +1,11 @@
require 'test/test_helper'
+class MyController < ApplicationController
+ include Devise::Controllers::Helpers
+end
+
class HelpersTest < ActionController::TestCase
- tests ApplicationController
+ tests MyController
test 'get resource name from request path' do
@request.path = '/users/session'
@@ -37,4 +41,11 @@ class HelpersTest < ActionController::TestCase
test 'resources methods are not controller actions' do
assert @controller.class.action_methods.empty?
end
+
+ test 'require no authentication tests current mapping' do
+ @controller.expects(:resource_name).returns(:user)
+ @mock_warden.expects(:authenticated?).with(:user).returns(true)
+ @controller.expects(:redirect_to).with(root_path)
+ @controller.send :require_no_authentication
+ end
end
View
@@ -25,10 +25,10 @@ def call_failure(env_params={})
end
test 'setup a default message' do
- assert_equal 'You are being redirected to /users/sign_in', call_failure.last
+ assert_equal ['You are being redirected to /users/sign_in'], call_failure.last
end
test 'pass in a different message' do
- assert_equal 'Hello world', call_failure(:message => 'Hello world').last
+ assert_equal ['Hello world'], call_failure(:message => 'Hello world').last
end
end

0 comments on commit dede8af

Please sign in to comment.