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:
parent
8a15ac6e4a
commit
2761a75437
6 changed files with 24 additions and 27 deletions
|
@ -46,7 +46,7 @@ class DeviseMailer < ::ActionMailer::Base
|
||||||
def mailer_sender(mapping)
|
def mailer_sender(mapping)
|
||||||
if Devise.mailer_sender.is_a?(Proc)
|
if Devise.mailer_sender.is_a?(Proc)
|
||||||
block_args = mapping.name if Devise.mailer_sender.arity > 0
|
block_args = mapping.name if Devise.mailer_sender.arity > 0
|
||||||
Devise.mailer_sender.call(*block_args)
|
Devise.mailer_sender.call(block_args)
|
||||||
else
|
else
|
||||||
Devise.mailer_sender
|
Devise.mailer_sender
|
||||||
end
|
end
|
||||||
|
|
|
@ -51,9 +51,6 @@ module Devise
|
||||||
# Routes for generating url helpers.
|
# Routes for generating url helpers.
|
||||||
ROUTES = [:session, :password, :confirmation, :registration, :unlock]
|
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]
|
STRATEGIES = [:rememberable, :http_authenticatable, :token_authenticatable, :authenticatable]
|
||||||
|
|
||||||
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
||||||
|
|
|
@ -22,11 +22,11 @@ module Devise
|
||||||
Devise::ROUTES.each do |module_name|
|
Devise::ROUTES.each do |module_name|
|
||||||
[:path, :url].each do |path_or_url|
|
[:path, :url].each do |path_or_url|
|
||||||
actions = [ nil, :new_ ]
|
actions = [ nil, :new_ ]
|
||||||
actions << :edit_ if module_name == :password
|
actions << :edit_ if [:password, :registration].include?(module_name)
|
||||||
actions << :destroy_ if module_name == :session
|
actions << :destroy_ if [:session].include?(module_name)
|
||||||
|
|
||||||
actions.each do |action|
|
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)
|
def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args)
|
||||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||||
send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
|
send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
|
||||||
|
|
|
@ -65,11 +65,12 @@ module Devise
|
||||||
@as = (options.delete(:as) || name).to_sym
|
@as = (options.delete(:as) || name).to_sym
|
||||||
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
|
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
|
||||||
@name = (options.delete(:scope) || name.to_s.singularize).to_sym
|
@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 || {}
|
@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
|
end
|
||||||
|
|
||||||
# Return modules for the mapping.
|
# Return modules for the mapping.
|
||||||
|
@ -126,15 +127,5 @@ module Devise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Devise::Mapping.register *ALL
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -88,8 +88,8 @@ module ActionController::Routing
|
||||||
route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
|
route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
|
||||||
|
|
||||||
with_options(route_options) do |routes|
|
with_options(route_options) do |routes|
|
||||||
mapping.for.each do |strategy|
|
mapping.for.each do |mod|
|
||||||
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
|
send(mod, routes, mapping) if self.respond_to?(mod, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -118,10 +118,7 @@ module ActionController::Routing
|
||||||
end
|
end
|
||||||
|
|
||||||
def registerable(routes, mapping)
|
def registerable(routes, mapping)
|
||||||
routes.with_options(:controller => 'registrations', :name_prefix => nil) do |session|
|
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] }
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,7 +59,19 @@ class MapRoutingTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'map create user registration' do
|
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
|
end
|
||||||
|
|
||||||
test 'map admin session with :as option' do
|
test 'map admin session with :as option' do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue