mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Move mappings to routes.
This commit is contained in:
parent
5848f12cc1
commit
f1cecacc99
10 changed files with 91 additions and 100 deletions
|
@ -1,20 +0,0 @@
|
||||||
ActionController::Routing::Routes.draw do |map|
|
|
||||||
Devise.mappings.each_value do |mapping|
|
|
||||||
map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m|
|
|
||||||
if mapping.authenticable?
|
|
||||||
m.resource :session,
|
|
||||||
:only => [:new, :create, :destroy]
|
|
||||||
end
|
|
||||||
|
|
||||||
if mapping.recoverable?
|
|
||||||
m.resource :password,
|
|
||||||
:only => [:new, :create, :edit, :update]
|
|
||||||
end
|
|
||||||
|
|
||||||
if mapping.confirmable?
|
|
||||||
m.resource :confirmation,
|
|
||||||
:only => [:new, :create, :show]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -7,6 +7,7 @@ end
|
||||||
|
|
||||||
require 'devise/initializers/warden'
|
require 'devise/initializers/warden'
|
||||||
require 'devise/mapping'
|
require 'devise/mapping'
|
||||||
|
require 'devise/routes'
|
||||||
|
|
||||||
# Ensure to include Devise modules only after Rails initialization.
|
# Ensure to include Devise modules only after Rails initialization.
|
||||||
# This way application should have already defined Devise mappings and we are
|
# This way application should have already defined Devise mappings and we are
|
||||||
|
@ -14,10 +15,4 @@ require 'devise/mapping'
|
||||||
#
|
#
|
||||||
Rails.configuration.after_initialize do
|
Rails.configuration.after_initialize do
|
||||||
ActiveRecord::Base.extend Devise::ActiveRecord
|
ActiveRecord::Base.extend Devise::ActiveRecord
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,17 +6,20 @@ module Devise
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
class Mapping
|
class Mapping
|
||||||
attr_reader :name, :as, :for
|
attr_reader :name, :as
|
||||||
|
|
||||||
def initialize(name, options)
|
def initialize(name, options)
|
||||||
@name = name
|
@as = (options[:as] || name).to_sym
|
||||||
@for = Array(options[:for])
|
@klass = (options[:class_name] || name.to_s.classify).to_s
|
||||||
@klass = (options[:to] || name.to_s.classify).to_s
|
@name = (options[:singular] || name.to_s.singularize).to_sym
|
||||||
@as = (options[:as] || name.to_s.pluralize).to_sym
|
end
|
||||||
|
|
||||||
|
# Return modules for the mapping.
|
||||||
|
def for
|
||||||
|
@for ||= to.devise_modules
|
||||||
end
|
end
|
||||||
|
|
||||||
# Reload mapped class each time when cache_classes is false.
|
# Reload mapped class each time when cache_classes is false.
|
||||||
#
|
|
||||||
def to
|
def to
|
||||||
return @to if @to
|
return @to if @to
|
||||||
klass = @klass.constantize
|
klass = @klass.constantize
|
||||||
|
@ -24,28 +27,23 @@ module Devise
|
||||||
klass
|
klass
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Check if the respective controller has a module in the mapping class.
|
||||||
|
def allows?(controller)
|
||||||
|
self.for.include?(CONTROLLERS[controller.to_sym])
|
||||||
|
end
|
||||||
|
|
||||||
CONTROLLERS.values.each do |m|
|
CONTROLLERS.values.each do |m|
|
||||||
class_eval <<-METHOD, __FILE__, __LINE__
|
class_eval <<-METHOD, __FILE__, __LINE__
|
||||||
def #{m}?
|
def #{m}?
|
||||||
@for.include?(:#{m})
|
self.for.include?(:#{m})
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
end
|
end
|
||||||
|
|
||||||
def allows?(controller)
|
|
||||||
@for.include?(CONTROLLERS[controller.to_sym])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mattr_accessor :mappings
|
mattr_accessor :mappings
|
||||||
self.mappings = {}
|
self.mappings = {}
|
||||||
|
|
||||||
def self.map(mapping, options={})
|
|
||||||
raise ArgumentError, "Need to provide :for option for Devise.map" unless options.key?(:for)
|
|
||||||
options.assert_valid_keys(:to, :for, :as)
|
|
||||||
self.mappings[mapping] = Mapping.new(mapping, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.find_mapping_by_path(path)
|
def self.find_mapping_by_path(path)
|
||||||
route = path.split("/")[1]
|
route = path.split("/")[1]
|
||||||
return nil unless route
|
return nil unless route
|
||||||
|
|
44
lib/devise/routes.rb
Normal file
44
lib/devise/routes.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
module ActionController::Routing
|
||||||
|
class RouteSet #:nodoc:
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
alias_method_chain :load_routes!, :devise
|
||||||
|
|
||||||
|
class Mapper #:doc:
|
||||||
|
def devise_for(*resources)
|
||||||
|
options = resources.extract_options!
|
||||||
|
|
||||||
|
resources.map!(&:to_sym)
|
||||||
|
options.assert_valid_keys(:class_name, :as)
|
||||||
|
|
||||||
|
resources.each do |resource|
|
||||||
|
mapping = Devise::Mapping.new(resource, options)
|
||||||
|
Devise.mappings[mapping.name] = mapping
|
||||||
|
|
||||||
|
namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m|
|
||||||
|
if mapping.authenticable?
|
||||||
|
m.resource :session, :only => [:new, :create, :destroy]
|
||||||
|
end
|
||||||
|
|
||||||
|
if mapping.recoverable?
|
||||||
|
m.resource :password, :only => [:new, :create, :edit, :update]
|
||||||
|
end
|
||||||
|
|
||||||
|
if mapping.confirmable?
|
||||||
|
m.resource :confirmation, :only => [:new, :create, :show]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,71 +1,49 @@
|
||||||
require 'test/test_helper'
|
require 'test/test_helper'
|
||||||
|
|
||||||
class Participant < User; end
|
|
||||||
class Organizer < User; end
|
|
||||||
|
|
||||||
class MapTest < ActiveSupport::TestCase
|
class MapTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def setup
|
|
||||||
@mappings = Devise.mappings
|
|
||||||
Devise.mappings = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def teardown
|
|
||||||
Devise.mappings = @mappings
|
|
||||||
end
|
|
||||||
|
|
||||||
test 'store options' do
|
test 'store options' do
|
||||||
Devise.map :participant, :to => Participant, :for => :authenticable
|
mapping = Devise.mappings[:user]
|
||||||
|
|
||||||
mappings = Devise.mappings
|
assert_equal User, mapping.to
|
||||||
assert_not mappings.empty?
|
assert_equal User.devise_modules, mapping.for
|
||||||
|
assert_equal :users, mapping.as
|
||||||
assert_equal Participant, mappings[:participant].to
|
|
||||||
assert_equal [:authenticable], mappings[:participant].for
|
|
||||||
assert_equal :participants, mappings[:participant].as
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'require :for option' do
|
test 'allows as to be given' do
|
||||||
assert_raise ArgumentError do
|
assert_equal :admin_area, Devise.mappings[:admin].as
|
||||||
Devise.map :participant, :to => Participant
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test 'assert valid keys in options' do
|
|
||||||
assert_raise ArgumentError do
|
|
||||||
Devise.map :participant, :to => Participant, :for => [:authenticable], :other => 123
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test 'use map name pluralized to :as option if none is given' do
|
|
||||||
Devise.map :participant, :for => [:authenticable]
|
|
||||||
assert_equal :participants, Devise.mappings[:participant].as
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'allows a controller depending on the mapping' do
|
test 'allows a controller depending on the mapping' do
|
||||||
Devise.map :participant, :for => [:authenticable, :confirmable]
|
mapping = Devise.mappings[:user]
|
||||||
|
assert mapping.allows?(:sessions)
|
||||||
|
assert mapping.allows?(:confirmations)
|
||||||
|
assert mapping.allows?(:passwords)
|
||||||
|
|
||||||
assert Devise.mappings[:participant].allows?(:sessions)
|
mapping = Devise.mappings[:admin]
|
||||||
assert Devise.mappings[:participant].allows?(:confirmations)
|
assert mapping.allows?(:sessions)
|
||||||
assert_not Devise.mappings[:participant].allows?(:passwords)
|
assert_not mapping.allows?(:confirmations)
|
||||||
|
assert_not mapping.allows?(:passwords)
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'return mapping by path' do
|
test 'return mapping by path' do
|
||||||
Devise.map :participant, :for => [:authenticable, :confirmable]
|
|
||||||
assert_equal Devise.mappings[:participant], Devise.find_mapping_by_path("/participants/session")
|
|
||||||
assert_nil Devise.find_mapping_by_path("/foo/bar")
|
assert_nil Devise.find_mapping_by_path("/foo/bar")
|
||||||
|
assert_equal Devise.mappings[:user], Devise.find_mapping_by_path("/users/session")
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'return mapping by customized path' do
|
test 'return mapping by customized path' do
|
||||||
Devise.map :participant, :for => [:authenticable, :confirmable], :as => "participantes"
|
assert_equal Devise.mappings[:admin], Devise.find_mapping_by_path("/admin_area/session")
|
||||||
assert_equal Devise.mappings[:participant], Devise.find_mapping_by_path("/participantes/session")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'magic predicates' do
|
test 'magic predicates' do
|
||||||
Devise.map :participant, :for => [:authenticable, :confirmable]
|
mapping = Devise.mappings[:user]
|
||||||
mapping = Devise.mappings[:participant]
|
|
||||||
assert mapping.authenticable?
|
assert mapping.authenticable?
|
||||||
assert mapping.confirmable?
|
assert mapping.confirmable?
|
||||||
assert !mapping.recoverable?
|
assert mapping.recoverable?
|
||||||
|
|
||||||
|
mapping = Devise.mappings[:admin]
|
||||||
|
assert mapping.authenticable?
|
||||||
|
assert_not mapping.confirmable?
|
||||||
|
assert_not mapping.recoverable?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
class Admin < ActiveRecord::Base
|
class Admin < ActiveRecord::Base
|
||||||
devise :all
|
devise :validatable
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
Devise.map :user, :for => [:authenticable, :recoverable, :confirmable, :validatable]
|
|
||||||
Devise.map :admin, :for => [:authenticable, :confirmable, :validatable], :as => 'admin_area'
|
|
|
@ -1,9 +1,12 @@
|
||||||
ActionController::Routing::Routes.draw do |map|
|
ActionController::Routing::Routes.draw do |map|
|
||||||
|
map.devise_for :users
|
||||||
|
map.devise_for :admin, :as => 'admin_area'
|
||||||
|
|
||||||
map.resources :users, :only => :index
|
map.resources :users, :only => :index
|
||||||
map.resources :admins, :only => :index
|
map.resources :admins, :only => :index
|
||||||
map.root :controller => :home
|
map.root :controller => :home
|
||||||
|
|
||||||
map.connect '/users/password/new', :controller => "passwords", :action => "new"
|
map.connect '/admin_area/password/new', :controller => "passwords", :action => "new"
|
||||||
map.connect ':controller/:action/:id'
|
map.connect ':controller/:action/:id'
|
||||||
map.connect ':controller/:action/:id.:format'
|
map.connect ':controller/:action/:id.:format'
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,14 +18,10 @@ class MapRoutingTest < ActionController::TestCase
|
||||||
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'admin_area/session/new')
|
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'admin_area/session/new')
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'map devise admin confirmation with :as option' do
|
test 'does not map devise admin confirmation' do
|
||||||
|
assert_raise ActionController::RoutingError do
|
||||||
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'admin_area/confirmation/new')
|
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'admin_area/confirmation/new')
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'map devise admin password with :as option' do
|
|
||||||
assert_raise ActionController::RoutingError do
|
|
||||||
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'admin_area/password/new')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,7 +19,6 @@ class ActionController::IntegrationTest
|
||||||
admin = Admin.create!(
|
admin = Admin.create!(
|
||||||
:email => 'admin@test.com', :password => '123456', :password_confirmation => '123456'
|
:email => 'admin@test.com', :password => '123456', :password_confirmation => '123456'
|
||||||
)
|
)
|
||||||
admin.confirm! unless options[:confirm] == false
|
|
||||||
admin
|
admin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue