Skip to content

Commit

Permalink
:as and :scope in routes is deprecated. Use :path and :singular inste…
Browse files Browse the repository at this point in the history
…ad. Closes heartcombo#199.
  • Loading branch information
josevalim committed Apr 15, 2010
1 parent 4e674ab commit 6bd0c7f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Devise.orm is deprecated, just require "devise/orm/YOUR_ORM" instead.
* Devise.default_url_options is deprecated, just modify ApplicationController.default_url_options.
* All messages under devise.sessions, except :signed_in and :signed_out, should be moved to devise.failure.
* :as and :scope in routes is deprecated. Use :path and :singular instead.

== 1.0.6

Expand Down
2 changes: 1 addition & 1 deletion app/mailers/devise/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setup_mail(record, action)
@resource = instance_variable_set("@#{@devise_mapping.name}", record)

template_path = ["devise/mailer"]
template_path.unshift "#{@devise_mapping.as}/mailer" if self.class.scoped_views?
template_path.unshift "#{@devise_mapping.plural}/mailer" if self.class.scoped_views?

headers = {
:subject => translate(@devise_mapping, action),
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/controllers/scoped_views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def render_with_scope(action, options={})

if self.class.scoped_views?
begin
render :template => "#{devise_mapping.as}/#{controller_name}/#{action}"
render :template => "#{devise_mapping.plural}/#{controller_name}/#{action}"
rescue ActionView::MissingTemplate
render :template => "#{controller_path}/#{action}"
end
Expand Down
30 changes: 21 additions & 9 deletions lib/devise/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ module Devise
# # is the modules included in the class
#
class Mapping #:nodoc:
attr_reader :name, :as, :controllers, :path_names, :path_prefix
attr_reader :singular, :plural, :path, :controllers, :path_names, :path_prefix
alias :name :singular

# Loop through all mappings looking for a map that matches with the requested
# path (ie /users/sign_in). If a path prefix is given, it's taken into account.
def self.find_by_path(path)
Devise.mappings.each_value do |mapping|
route = path.split("/")[mapping.as_position]
return mapping if route && mapping.as == route.to_sym
route = path.split("/")[mapping.segment_position]
return mapping if route && mapping.path == route.to_sym
end
nil
end
Expand All @@ -50,9 +51,20 @@ def self.find_scope!(duck)
end

def initialize(name, options) #:nodoc:
@as = (options.delete(:as) || name).to_sym
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
@name = (options.delete(:scope) || name.to_s.singularize).to_sym
if as = options.delete(:as)
ActiveSupport::Deprecation.warn ":as is deprecated, please use :path instead."
options[:path] ||= as
end

if scope = options.delete(:scope)
ActiveSupport::Deprecation.warn ":scope is deprecated, please use :singular instead."
options[:singular] ||= scope
end

@plural = name.to_sym
@path = (options.delete(:path) || name).to_sym
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
@singular = (options.delete(:singular) || name.to_s.singularize).to_sym

@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")

Expand Down Expand Up @@ -96,13 +108,13 @@ def allowed_controllers
end

# Return in which position in the path prefix devise should find the as mapping.
def as_position
def segment_position
self.path_prefix.count("/")
end

# Returns the raw path using path_prefix and as.
def path
path_prefix + as.to_s
def full_path
path_prefix + path.to_s
end

def authenticatable?
Expand Down
43 changes: 22 additions & 21 deletions lib/devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ 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
# This method is going to look inside your User model and create the
# needed routes:
#
# # Session routes for Authenticatable (default)
Expand All @@ -44,44 +45,44 @@ class Mapper
# * :class_name => setup a different class to be looked up by devise,
# if it cannot be correctly find by the route name.
#
# devise_for :users, :class_name => 'Account'
# devise_for :users, :class_name => 'Account'
#
# * :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:
# * :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:
#
# devise_for :users, :as => 'accounts'
# devise_for :users, :path => 'accounts'
#
# * :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:
# * :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.
#
# devise_for :users, :scope => :account
# devise_for :users, :singular => :user
#
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
# :password, :confirmation, :unlock.
#
# devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
# 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"
# devise_for :users, :path_prefix => "/:locale"
#
# 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:
#
# class ApplicationController < ActionController::Base
# def self.default_url_options
# { :locale => I18n.locale }
# class ApplicationController < ActionController::Base
# def self.default_url_options
# { :locale => I18n.locale }
# end
# end
# end
#
# * :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" }
# devise_for :users, :controllers => { :sessions => "users/sessions" }
#
# * :skip => tell which controller you want to skip routes from being created:
#
# devise_for :users, :skip => :sessions
# devise_for :users, :skip => :sessions
#
def devise_for(*resources)
options = resources.extract_options!
Expand All @@ -108,33 +109,33 @@ def devise_for(*resources)
protected

def devise_session(mapping, controllers)
scope mapping.path do
scope mapping.full_path do
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"
end
end

def devise_password(mapping, controllers)
scope mapping.path, :name_prefix => mapping.name do
scope mapping.full_path, :name_prefix => mapping.name do
resource :password, :only => [:new, :create, :edit, :update], :path => mapping.path_names[:password], :controller => controllers[:passwords]
end
end

def devise_confirmation(mapping, controllers)
scope mapping.path, :name_prefix => mapping.name do
scope mapping.full_path, :name_prefix => mapping.name do
resource :confirmation, :only => [:new, :create, :show], :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
end
end

def devise_unlock(mapping, controllers)
scope mapping.path, :name_prefix => mapping.name do
scope mapping.full_path, :name_prefix => mapping.name do
resource :unlock, :only => [:new, :create, :show], :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
end
end

def devise_registration(mapping, controllers)
scope mapping.path[1..-1], :name_prefix => mapping.name do
scope mapping.full_path[1..-1], :name_prefix => mapping.name do
resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => "",
:path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations]
end
Expand Down
20 changes: 11 additions & 9 deletions test/mapping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ class MappingTest < ActiveSupport::TestCase
mapping = Devise.mappings[:user]
assert_equal User, mapping.to
assert_equal User.devise_modules, mapping.modules
assert_equal :users, mapping.as
assert_equal :users, mapping.plural
assert_equal :user, mapping.singular
assert_equal :users, mapping.path
end

test 'allows as to be given' do
assert_equal :admin_area, Devise.mappings[:admin].as
test 'allows path to be given' do
assert_equal :admin_area, Devise.mappings[:admin].path
end

test 'allows custom scope to be given' do
assert_equal :accounts, Devise.mappings[:manager].as
test 'allows custom singular to be given' do
assert_equal :accounts, Devise.mappings[:manager].path
end

test 'allows a controller depending on the mapping' do
Expand Down Expand Up @@ -91,13 +93,13 @@ class MappingTest < ActiveSupport::TestCase
end

test 'retrieve as from the proper position' do
assert_equal 1, Devise.mappings[:user].as_position
assert_equal 2, Devise.mappings[:manager].as_position
assert_equal 1, Devise.mappings[:user].segment_position
assert_equal 2, Devise.mappings[:manager].segment_position
end

test 'path is returned with path prefix and as' do
assert_equal '/users', Devise.mappings[:user].path
assert_equal '/:locale/accounts', Devise.mappings[:manager].path
assert_equal '/users', Devise.mappings[:user].full_path
assert_equal '/:locale/accounts', Devise.mappings[:manager].full_path
end

test 'magic predicates' do
Expand Down
4 changes: 2 additions & 2 deletions test/rails_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
resources :admins, :only => [:index]

devise_for :users
devise_for :admin, :as => "admin_area", :controllers => { :sessions => "sessions" }, :skip => :passwords
devise_for :accounts, :scope => "manager", :path_prefix => ":locale",
devise_for :admin, :path => "admin_area", :controllers => { :sessions => "sessions" }, :skip => :passwords
devise_for :accounts, :singular => "manager", :path_prefix => ":locale",
:class_name => "User", :path_names => {
:sign_in => "login", :sign_out => "logout",
:password => "secret", :confirmation => "verification",
Expand Down

0 comments on commit 6bd0c7f

Please sign in to comment.