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

Refactor on routes.

This commit is contained in:
José Valim 2010-02-08 20:25:20 +01:00
parent 8a15ac6e4a
commit 2761a75437
6 changed files with 24 additions and 27 deletions

View file

@ -46,7 +46,7 @@ class DeviseMailer < ::ActionMailer::Base
def mailer_sender(mapping)
if Devise.mailer_sender.is_a?(Proc)
block_args = mapping.name if Devise.mailer_sender.arity > 0
Devise.mailer_sender.call(*block_args)
Devise.mailer_sender.call(block_args)
else
Devise.mailer_sender
end

View file

@ -51,9 +51,6 @@ module Devise
# Routes for generating url helpers.
ROUTES = [:session, :password, :confirmation, :registration, :unlock]
# Path names used in routes.
PATH_NAMES = [:sign_in, :sign_out, :sign_up, :password, :confirmation, :unlock]
STRATEGIES = [:rememberable, :http_authenticatable, :token_authenticatable, :authenticatable]
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']

View file

@ -22,11 +22,11 @@ module Devise
Devise::ROUTES.each do |module_name|
[:path, :url].each do |path_or_url|
actions = [ nil, :new_ ]
actions << :edit_ if module_name == :password
actions << :destroy_ if module_name == :session
actions << :edit_ if [:password, :registration].include?(module_name)
actions << :destroy_ if [:session].include?(module_name)
actions.each do |action|
class_eval <<-URL_HELPERS
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args)
scope = Devise::Mapping.find_scope!(resource_or_scope)
send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)

View file

@ -65,11 +65,12 @@ module Devise
@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
@path_names = options.delete(:path_names) || {}
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
@route_options = options || {}
setup_path_names
@path_names = Hash.new { |h,k| h[k] = k.to_s }
@path_names.merge!(options.delete(:path_names) || {})
end
# Return modules for the mapping.
@ -126,15 +127,5 @@ module Devise
end
end
Devise::Mapping.register *ALL
private
# Configure default path names, allowing the user overwrite defaults by
# passing a hash in :path_names.
def setup_path_names
Devise::PATH_NAMES.each do |path_name|
@path_names[path_name] ||= path_name.to_s
end
end
end
end

View file

@ -88,8 +88,8 @@ module ActionController::Routing
route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
with_options(route_options) do |routes|
mapping.for.each do |strategy|
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
mapping.for.each do |mod|
send(mod, routes, mapping) if self.respond_to?(mod, true)
end
end
end
@ -118,10 +118,7 @@ module ActionController::Routing
end
def registerable(routes, mapping)
routes.with_options(:controller => 'registrations', :name_prefix => nil) do |session|
session.send(:"new_#{mapping.name}_registration", mapping.path_names[:sign_up], :action => 'new', :conditions => { :method => :get })
session.send(:"#{mapping.name}_registration", mapping.path_names[:sign_up], :action => 'create', :conditions => { :method => :post })
end
routes.resource :registration, :only => [:new, :create, :edit, :update, :destroy], :as => mapping.raw_path[1..-1], :path_prefix => nil, :path_names => { :new => mapping.path_names[:sign_up] }
end
end
end

View file

@ -59,7 +59,19 @@ class MapRoutingTest < ActionController::TestCase
end
test 'map create user registration' do
assert_recognizes({:controller => 'registrations', :action => 'create'}, {:path => 'users/sign_up', :method => :post})
assert_recognizes({:controller => 'registrations', :action => 'create'}, {:path => 'users', :method => :post})
end
test 'map edit user registration' do
assert_recognizes({:controller => 'registrations', :action => 'edit'}, {:path => 'users/edit', :method => :get})
end
test 'map update user registration' do
assert_recognizes({:controller => 'registrations', :action => 'update'}, {:path => 'users', :method => :put})
end
test 'map destroy user registration' do
assert_recognizes({:controller => 'registrations', :action => 'destroy'}, {:path => 'users', :method => :delete})
end
test 'map admin session with :as option' do