2010-02-16 08:31:49 -05:00
|
|
|
module ActionDispatch::Routing
|
2009-10-12 20:49:51 -04:00
|
|
|
class RouteSet #:nodoc:
|
2009-10-17 11:10:15 -04:00
|
|
|
# Ensure Devise modules are included only after loading routes, because we
|
2010-02-18 11:59:05 -05:00
|
|
|
# need devise_for mappings already declared to create filters and helpers.
|
2010-02-16 08:31:49 -05:00
|
|
|
def finalize_with_devise!
|
|
|
|
finalize_without_devise!
|
2009-12-02 14:51:34 -05:00
|
|
|
return if Devise.mappings.empty?
|
2009-10-12 20:49:51 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
ActionController::Base.send :include, Devise::Controllers::Helpers
|
2009-10-12 20:49:51 -04:00
|
|
|
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
|
|
|
|
|
|
|
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
alias_method_chain :finalize!, :devise
|
|
|
|
end
|
2009-10-12 20:49:51 -04:00
|
|
|
|
2010-02-16 08:31:49 -05:00
|
|
|
class Mapper
|
|
|
|
# Includes devise_for method for routes. This method is responsible to
|
|
|
|
# generate all needed routes for devise, based on what modules you have
|
|
|
|
# defined in your model.
|
|
|
|
# Examples: Let's say you have an User model configured to use
|
|
|
|
# authenticatable, confirmable and recoverable modules. After creating this
|
|
|
|
# inside your routes:
|
|
|
|
#
|
|
|
|
# devise_for :users
|
|
|
|
#
|
|
|
|
# this method is going to look inside your User model and create the
|
|
|
|
# needed routes:
|
|
|
|
#
|
|
|
|
# # Session routes for Authenticatable (default)
|
|
|
|
# new_user_session GET /users/sign_in {:controller=>"sessions", :action=>"new"}
|
|
|
|
# user_session POST /users/sign_in {:controller=>"sessions", :action=>"create"}
|
|
|
|
# destroy_user_session GET /users/sign_out {:controller=>"sessions", :action=>"destroy"}
|
|
|
|
#
|
|
|
|
# # Password routes for Recoverable, if User model has :recoverable configured
|
|
|
|
# new_user_password GET /users/password/new(.:format) {:controller=>"passwords", :action=>"new"}
|
|
|
|
# edit_user_password GET /users/password/edit(.:format) {:controller=>"passwords", :action=>"edit"}
|
|
|
|
# user_password PUT /users/password(.:format) {:controller=>"passwords", :action=>"update"}
|
|
|
|
# POST /users/password(.:format) {:controller=>"passwords", :action=>"create"}
|
|
|
|
#
|
|
|
|
# # Confirmation routes for Confirmable, if User model has :confirmable configured
|
|
|
|
# new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
|
|
|
|
# user_confirmation GET /users/confirmation(.:format) {:controller=>"confirmations", :action=>"show"}
|
|
|
|
# POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"}
|
|
|
|
#
|
|
|
|
# You can configure your routes with some options:
|
|
|
|
#
|
2010-02-17 07:15:19 -05:00
|
|
|
# * :class_name => setup a different class to be looked up by devise,
|
|
|
|
# if it cannot be correctly find by the route name.
|
2010-02-16 08:31:49 -05:00
|
|
|
#
|
|
|
|
# devise_for :users, :class_name => 'Account'
|
|
|
|
#
|
2010-02-17 07:15:19 -05:00
|
|
|
# * :as => allows you to setup path name that will be used, as rails routes does.
|
|
|
|
# The following route configuration would setup your route as /accounts instead of /users:
|
2010-02-16 08:31:49 -05:00
|
|
|
#
|
|
|
|
# devise_for :users, :as => 'accounts'
|
|
|
|
#
|
2010-02-17 07:15:19 -05:00
|
|
|
# * :scope => setup the scope name. This is used as the instance variable name in controller,
|
|
|
|
# as the name in routes and the scope given to warden. Defaults to the singular of the given name:
|
2010-02-16 08:31:49 -05:00
|
|
|
#
|
|
|
|
# devise_for :users, :scope => :account
|
|
|
|
#
|
2010-02-17 07:15:19 -05:00
|
|
|
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
|
|
|
|
# :password, :confirmation, :unlock.
|
2010-02-16 08:31:49 -05:00
|
|
|
#
|
|
|
|
# devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
|
|
|
#
|
|
|
|
# * :path_prefix => the path prefix to be used in all routes.
|
|
|
|
#
|
|
|
|
# devise_for :users, :path_prefix => "/:locale"
|
|
|
|
#
|
2010-02-19 04:13:53 -05:00
|
|
|
# If you are using a dynamic prefix, like :locale above, you need to configure default_url_options in your ApplicationController
|
|
|
|
# class level, so Devise can pick it:
|
2010-02-16 08:31:49 -05:00
|
|
|
#
|
2010-02-19 04:13:53 -05:00
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# def self.default_url_options
|
|
|
|
# { :locale => I18n.locale }
|
|
|
|
# end
|
2010-02-16 08:31:49 -05:00
|
|
|
# end
|
|
|
|
#
|
2010-02-17 07:15:19 -05:00
|
|
|
# * :controllers => the controller which should be used. All routes by default points to Devise controllers.
|
|
|
|
# However, if you want them to point to custom controller, you should do:
|
|
|
|
#
|
|
|
|
# devise_for :users, :controllers => { :sessions => "users/sessions" }
|
|
|
|
#
|
2010-03-29 10:13:19 -04:00
|
|
|
# * :skip => tell which controller you want to skip routes from being created:
|
2010-03-12 03:54:57 -05:00
|
|
|
#
|
2010-03-29 10:13:19 -04:00
|
|
|
# devise_for :users, :skip => :sessions
|
2010-03-12 03:54:57 -05:00
|
|
|
#
|
2010-02-16 08:31:49 -05:00
|
|
|
def devise_for(*resources)
|
|
|
|
options = resources.extract_options!
|
|
|
|
resources.map!(&:to_sym)
|
2010-02-17 07:15:19 -05:00
|
|
|
|
2010-02-16 08:31:49 -05:00
|
|
|
resources.each do |resource|
|
2010-03-28 08:51:03 -04:00
|
|
|
mapping = Devise.register(resource, options)
|
2010-02-17 07:15:19 -05:00
|
|
|
|
2010-02-25 01:54:06 -05:00
|
|
|
unless mapping.to.respond_to?(:devise)
|
|
|
|
raise "#{mapping.to.name} does not respond to 'devise' method. This usually means you haven't " <<
|
2010-03-28 08:51:03 -04:00
|
|
|
"loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " <<
|
2010-02-25 01:54:06 -05:00
|
|
|
"inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
|
|
|
|
end
|
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
routes = mapping.routes
|
|
|
|
routes -= Array(options.delete(:skip)).map { |s| s.to_s.singularize.to_sym }
|
|
|
|
|
|
|
|
routes.each do |mod|
|
|
|
|
send(:"devise_#{mod}", mapping, mapping.controllers)
|
2009-11-06 09:44:10 -05:00
|
|
|
end
|
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
2009-10-12 20:49:51 -04:00
|
|
|
|
2010-02-16 08:31:49 -05:00
|
|
|
protected
|
2009-10-12 20:49:51 -04:00
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def devise_session(mapping, controllers)
|
2010-02-19 04:13:53 -05:00
|
|
|
scope mapping.path do
|
2010-02-17 07:15:19 -05:00
|
|
|
get mapping.path_names[:sign_in], :to => "#{controllers[:sessions]}#new", :as => :"new_#{mapping.name}_session"
|
|
|
|
post mapping.path_names[:sign_in], :to => "#{controllers[:sessions]}#create", :as => :"#{mapping.name}_session"
|
|
|
|
get mapping.path_names[:sign_out], :to => "#{controllers[:sessions]}#destroy", :as => :"destroy_#{mapping.name}_session"
|
2009-10-12 20:49:51 -04:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def devise_password(mapping, controllers)
|
2010-02-19 04:13:53 -05:00
|
|
|
scope mapping.path, :name_prefix => mapping.name do
|
2010-02-17 07:15:19 -05:00
|
|
|
resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password], :controller => controllers[:passwords]
|
2009-11-06 09:44:10 -05:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def devise_confirmation(mapping, controllers)
|
2010-02-19 04:13:53 -05:00
|
|
|
scope mapping.path, :name_prefix => mapping.name do
|
2010-02-17 07:15:19 -05:00
|
|
|
resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def devise_unlock(mapping, controllers)
|
2010-02-19 04:13:53 -05:00
|
|
|
scope mapping.path, :name_prefix => mapping.name do
|
2010-02-17 07:15:19 -05:00
|
|
|
resource :unlock, :only => [:new, :create, :show], :as => mapping.path_names[:unlock], :controller => controllers[:unlocks]
|
2010-01-23 19:26:06 -05:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
2010-01-23 19:26:06 -05:00
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def devise_registration(mapping, controllers)
|
2010-03-16 09:48:30 -04:00
|
|
|
scope mapping.path[1..-1], :name_prefix => "#{mapping.name}_registration" do
|
|
|
|
resource :registration, :only => [:new, :create, :edit, :update, :destroy], :as => "",
|
2010-02-17 07:15:19 -05:00
|
|
|
:path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations]
|
2010-01-23 19:26:06 -05:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|
2009-10-12 20:49:51 -04:00
|
|
|
end
|
2010-02-16 08:31:49 -05:00
|
|
|
end
|