mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Do not include Devise internal helpers in application controller.
This commit is contained in:
parent
b5256d9765
commit
dede8af5b0
10 changed files with 47 additions and 33 deletions
|
@ -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 @@ module Devise
|
|||
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 @@ module Devise
|
|||
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,9 +68,9 @@ module Devise
|
|||
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
|
||||
|
@ -67,6 +78,14 @@ module Devise
|
|||
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,6 +15,8 @@ module Devise
|
|||
#
|
||||
# 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|
|
||||
|
|
|
@ -8,7 +8,6 @@ module ActionController::Routing
|
|||
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 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
|||
@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
|
||||
|
|
|
@ -25,10 +25,10 @@ class FailureTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue