2010-02-16 13:31:49 +00:00
|
|
|
module ActionDispatch::Routing
|
2009-10-13 00:49:51 +00:00
|
|
|
class RouteSet #:nodoc:
|
2009-10-17 15:10:15 +00:00
|
|
|
# Ensure Devise modules are included only after loading routes, because we
|
2010-02-18 16:59:05 +00:00
|
|
|
# need devise_for mappings already declared to create filters and helpers.
|
2010-02-16 13:31:49 +00:00
|
|
|
def finalize_with_devise!
|
|
|
|
finalize_without_devise!
|
2010-03-31 20:04:48 +00:00
|
|
|
Devise.configure_warden!
|
2010-01-13 17:12:13 +00:00
|
|
|
ActionController::Base.send :include, Devise::Controllers::Helpers
|
2009-10-13 00:49:51 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
alias_method_chain :finalize!, :devise
|
|
|
|
end
|
2009-10-13 00:49:51 +00:00
|
|
|
|
2010-02-16 13:31:49 +00: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.
|
2010-04-15 06:21:13 +00:00
|
|
|
#
|
2010-02-16 13:31:49 +00:00
|
|
|
# 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
|
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# This method is going to look inside your User model and create the
|
2010-02-16 13:31:49 +00:00
|
|
|
# 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 12:15:19 +00: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 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :class_name => 'Account'
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# * :path => 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 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :path => 'accounts'
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# * :singular => setup the singular name for the given resource. This is used as the instance variable name in
|
|
|
|
# controller, as the name in routes and the scope given to warden.
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :singular => :user
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-02-17 12:15:19 +00:00
|
|
|
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
|
|
|
|
# :password, :confirmation, :unlock.
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
|
|
|
# * :path_prefix => the path prefix to be used in all routes.
|
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :path_prefix => "/:locale"
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-02-19 09:13:53 +00: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 13:31:49 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# def self.default_url_options
|
|
|
|
# { :locale => I18n.locale }
|
|
|
|
# end
|
2010-02-19 09:13:53 +00:00
|
|
|
# end
|
2010-02-16 13:31:49 +00:00
|
|
|
#
|
2010-02-17 12:15:19 +00: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:
|
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :controllers => { :sessions => "users/sessions" }
|
2010-02-17 12:15:19 +00:00
|
|
|
#
|
2010-03-29 14:13:19 +00:00
|
|
|
# * :skip => tell which controller you want to skip routes from being created:
|
2010-03-12 08:54:57 +00:00
|
|
|
#
|
2010-04-15 06:21:13 +00:00
|
|
|
# devise_for :users, :skip => :sessions
|
2010-03-12 08:54:57 +00:00
|
|
|
#
|
2010-02-16 13:31:49 +00:00
|
|
|
def devise_for(*resources)
|
|
|
|
options = resources.extract_options!
|
|
|
|
resources.map!(&:to_sym)
|
2010-02-17 12:15:19 +00:00
|
|
|
|
2010-02-16 13:31:49 +00:00
|
|
|
resources.each do |resource|
|
2010-04-22 17:59:52 +00:00
|
|
|
mapping = Devise.add_model(resource, options)
|
2010-02-17 12:15:19 +00:00
|
|
|
|
2010-06-09 09:50:28 +00:00
|
|
|
begin
|
2010-06-12 18:48:37 +00:00
|
|
|
raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
|
2010-06-24 14:51:30 +00:00
|
|
|
rescue NameError => e
|
|
|
|
raise unless mapping.class_name == resource.to_s.classify
|
|
|
|
warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
|
|
|
|
"no model #{mapping.class_name} defined in your application"
|
|
|
|
next
|
2010-06-09 09:50:28 +00:00
|
|
|
rescue NoMethodError => e
|
|
|
|
raise unless e.message.include?("undefined method `devise'")
|
2010-06-12 18:48:37 +00:00
|
|
|
raise_no_devise_method_error!(mapping.class_name)
|
2010-02-25 06:54:06 +00:00
|
|
|
end
|
|
|
|
|
2010-03-29 14:13:19 +00: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 14:44:10 +00:00
|
|
|
end
|
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
2009-10-13 00:49:51 +00:00
|
|
|
|
2010-05-24 21:02:54 +00:00
|
|
|
def authenticate(scope)
|
|
|
|
constraint = lambda do |request|
|
|
|
|
request.env["warden"].authenticate!(:scope => scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
constraints(constraint) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-16 13:31:49 +00:00
|
|
|
protected
|
2009-10-13 00:49:51 +00:00
|
|
|
|
2010-03-29 14:13:19 +00:00
|
|
|
def devise_session(mapping, controllers)
|
2010-04-15 06:21:13 +00:00
|
|
|
scope mapping.full_path do
|
2010-02-17 12:15:19 +00: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-13 00:49:51 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-29 14:13:19 +00:00
|
|
|
def devise_password(mapping, controllers)
|
2010-06-23 10:42:23 +00:00
|
|
|
scope mapping.full_path, :as => mapping.name do
|
2010-04-05 09:46:26 +00:00
|
|
|
resource :password, :only => [:new, :create, :edit, :update], :path => mapping.path_names[:password], :controller => controllers[:passwords]
|
2009-11-06 14:44:10 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-29 14:13:19 +00:00
|
|
|
def devise_confirmation(mapping, controllers)
|
2010-06-23 10:42:23 +00:00
|
|
|
scope mapping.full_path, :as => mapping.name do
|
2010-04-05 09:46:26 +00:00
|
|
|
resource :confirmation, :only => [:new, :create, :show], :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
|
2009-12-30 17:19:33 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-29 14:13:19 +00:00
|
|
|
def devise_unlock(mapping, controllers)
|
2010-06-23 10:42:23 +00:00
|
|
|
scope mapping.full_path, :as => mapping.name do
|
2010-04-05 09:46:26 +00:00
|
|
|
resource :unlock, :only => [:new, :create, :show], :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
|
2010-01-24 00:26:06 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
2010-01-24 00:26:06 +00:00
|
|
|
|
2010-03-29 14:13:19 +00:00
|
|
|
def devise_registration(mapping, controllers)
|
2010-06-23 10:42:23 +00:00
|
|
|
scope mapping.full_path[1..-1], :as => mapping.name do
|
2010-05-11 15:29:00 +00:00
|
|
|
resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration],
|
2010-02-17 12:15:19 +00:00
|
|
|
:path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations]
|
2010-01-24 00:26:06 +00:00
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
end
|
2010-06-09 09:50:28 +00:00
|
|
|
|
|
|
|
def raise_no_devise_method_error!(klass)
|
|
|
|
raise "#{klass} does not respond to 'devise' method. This usually means you haven't " <<
|
|
|
|
"loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " <<
|
|
|
|
"inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
|
|
|
|
end
|
2009-10-13 00:49:51 +00:00
|
|
|
end
|
2010-05-11 15:29:00 +00:00
|
|
|
end
|