1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Ensure routes are loaded before application classes are eager loaded, closes #212.

This commit is contained in:
José Valim 2010-04-22 19:59:52 +02:00
parent b5f892bcdb
commit 4da63c5395
5 changed files with 17 additions and 25 deletions

View file

@ -157,13 +157,10 @@ module Devise
# Register a model in Devise. You can call this manually if you don't want
# to use devise routes. Check devise_for in routes to know which options
# are available.
def self.register(resource, options)
def self.add_model(resource, options)
mapping = Devise::Mapping.new(resource, options)
self.mappings[mapping.name] = mapping
self.default_scope ||= mapping.name
warden_config.default_scope ||= mapping.name
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
mapping
end
@ -233,6 +230,15 @@ module Devise
# A method used internally to setup warden manager from the Rails initialize
# block.
def self.configure_warden! #:nodoc:
return unless warden_config
warden_config.failure_app = Devise::FailureApp
warden_config.default_scope = Devise.default_scope
Devise.mappings.each_value do |mapping|
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
end
@@warden_config_block.try :call, Devise.warden_config
end

View file

@ -5,11 +5,14 @@ module Devise
class Engine < ::Rails::Engine
config.devise = Devise
initializer "devise.ensure_routes_are_loaded", :before => :load_app_classes, :after => :load_config_initializers do |app|
app.reload_routes!
end
initializer "devise.add_middleware" do |app|
app.config.middleware.use Warden::Manager do |config|
Devise.warden_config = config
config.failure_app = Devise::FailureApp
config.default_scope = Devise.default_scope
Devise.configure_warden!
end
end

View file

@ -89,7 +89,7 @@ module ActionDispatch::Routing
resources.map!(&:to_sym)
resources.each do |resource|
mapping = Devise.register(resource, options)
mapping = Devise.add_model(resource, options)
unless mapping.to.respond_to?(:devise)
raise "#{mapping.to.name} does not respond to 'devise' method. This usually means you haven't " <<

View file

@ -265,24 +265,6 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_contain 'Welcome to "sessions/new" view!'
end
# Custom strategy invoking custom!
test 'custom strategy invoking custom on sign up bevahes as expected' do
Warden::Strategies.add(:custom) do
def authenticate!
custom!([401, {"Content-Type" => "text/html"}, ["Custom strategy"]])
end
end
begin
Devise.warden_config.default_strategies(:scope => :user).unshift(:custom)
sign_in_as_user
assert_equal 401, status
assert_contain 'Custom strategy'
ensure
Devise.warden_config.default_strategies(:scope => :user).shift
end
end
# Access
test 'render 404 on roles without routes' do
assert_raise ActionController::RoutingError do

View file

@ -5,4 +5,5 @@ class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :current_user
before_filter :authenticate_user!, :if => :devise_controller?
end