diff --git a/README.rdoc b/README.rdoc index 6bf1521e..8d6906d8 100644 --- a/README.rdoc +++ b/README.rdoc @@ -16,7 +16,7 @@ Right now it's composed of four mainly modules: == Dependencies -Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework from hassox (http://github.com/hassox), so you're gonna need to install this gem. Current warden version is 0.4.0. Please ensure you have it installed in order to user devise (see instalation below). +Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework from hassox (http://github.com/hassox), so you're gonna need to install this gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below). == Installation @@ -41,7 +41,7 @@ We're assuming here you want a User model. First of all you have to setup a migr t.string :encrypted_password, :null => false t.string :password_salt, :null => false # required for recoverable and/or confirmable - t.string :perishable_token, :null => false + t.string :perishable_token # required for confirmable t.datetime :confirmed_at diff --git a/lib/devise/controllers/url_helpers.rb b/lib/devise/controllers/url_helpers.rb index 8160aaf8..cab50293 100644 --- a/lib/devise/controllers/url_helpers.rb +++ b/lib/devise/controllers/url_helpers.rb @@ -6,6 +6,7 @@ module Devise [:path, :url].each do |path_or_url| actions = [ nil, :new_ ] actions << :edit_ if module_name == :password + actions << :destroy_ if module_name == :session actions.each do |action| class_eval <<-URL_HELPERS diff --git a/lib/devise/initializers/warden.rb b/lib/devise/initializers/warden.rb index 14e81132..fea999f6 100644 --- a/lib/devise/initializers/warden.rb +++ b/lib/devise/initializers/warden.rb @@ -54,7 +54,7 @@ Warden::Strategies.add(:authenticable) do if valid_session? && resource = @mapping.to.authenticate(session) success!(resource) else - redirect!("/#{@mapping.as}/session/new", :unauthenticated => true) + redirect!("/#{@mapping.as}/sign_in", :unauthenticated => true) end end diff --git a/lib/devise/routes.rb b/lib/devise/routes.rb index d9973a92..74c80849 100644 --- a/lib/devise/routes.rb +++ b/lib/devise/routes.rb @@ -1,6 +1,9 @@ module ActionController::Routing class RouteSet #:nodoc: + # Alias to include Devise modules after only loading routes, because we need + # devise_for mappings already done to create magic filters and helpers. + # def load_routes_with_devise! load_routes_without_devise! @@ -13,6 +16,8 @@ module ActionController::Routing alias_method_chain :load_routes!, :devise class Mapper #:doc: + # Includes devise_for map for routes. + # def devise_for(*resources) options = resources.extract_options! @@ -23,11 +28,15 @@ module ActionController::Routing mapping = Devise::Mapping.new(resource, options) Devise.mappings[mapping.name] = mapping - namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m| - if mapping.authenticable? - m.resource :session, :only => [:new, :create, :destroy] + if mapping.authenticable? + with_options(:controller => 'sessions', :path_prefix => mapping.as) do |session| + session.send(:"new_#{mapping.name}_session", 'sign_in', :action => 'new', :conditions => { :method => :get }) + session.send(:"#{mapping.name}_session", 'sign_in', :action => 'create', :conditions => { :method => :post }) + session.send(:"destroy_#{mapping.name}_session", 'sign_out', :action => 'destroy', :conditions => { :method => :get }) end + end + namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m| if mapping.recoverable? m.resource :password, :only => [:new, :create, :edit, :update] end diff --git a/test/controllers/url_helpers_test.rb b/test/controllers/url_helpers_test.rb index 96e16963..9344c2de 100644 --- a/test/controllers/url_helpers_test.rb +++ b/test/controllers/url_helpers_test.rb @@ -7,7 +7,7 @@ class RoutesTest < ActionController::TestCase @request.path = '/users/session' prepend_path = "#{prepend_path}_" if prepend_path - # No params + # Resource param assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user), send(:"#{prepend_path}user_#{name}_path") assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user), @@ -31,6 +31,7 @@ class RoutesTest < ActionController::TestCase test 'should alias session to mapped user session' do test_path_and_url :session test_path_and_url :session, :new + test_path_and_url :session, :destroy end test 'should alias password to mapped user password' do diff --git a/test/integration/authenticable_test.rb b/test/integration/authenticable_test.rb index 43a9bd6b..d95023b9 100644 --- a/test/integration/authenticable_test.rb +++ b/test/integration/authenticable_test.rb @@ -34,7 +34,7 @@ class AuthenticationTest < ActionController::IntegrationTest sign_in_as_user sign_in_as_admin - delete user_session_path + get destroy_user_session_path assert_not warden.authenticated?(:user) assert warden.authenticated?(:admin) end @@ -43,7 +43,7 @@ class AuthenticationTest < ActionController::IntegrationTest sign_in_as_user sign_in_as_admin - delete admin_session_path + get destroy_admin_session_path assert_not warden.authenticated?(:admin) assert warden.authenticated?(:user) end @@ -126,7 +126,7 @@ class AuthenticationTest < ActionController::IntegrationTest sign_in_as_admin assert warden.authenticated?(:admin) - delete admin_session_path + get destroy_admin_session_path assert_response :redirect assert_redirected_to root_path @@ -136,7 +136,7 @@ class AuthenticationTest < ActionController::IntegrationTest end test 'not authenticated admin does not set error message on sign out' do - delete admin_session_path + get destroy_admin_session_path assert_response :redirect assert_redirected_to root_path diff --git a/test/routes_test.rb b/test/routes_test.rb index 41ecddf3..6bca5426 100644 --- a/test/routes_test.rb +++ b/test/routes_test.rb @@ -2,20 +2,48 @@ require 'test/test_helper' class MapRoutingTest < ActionController::TestCase - test 'map devise user session' do - assert_recognizes({:controller => 'sessions', :action => 'new'}, 'users/session/new') + test 'map devise new user session' do + assert_recognizes({:controller => 'sessions', :action => 'new'}, {:path => 'users/sign_in', :method => :get}) end - test 'map devise user confirmation' do + test 'map devise create user session' do + assert_recognizes({:controller => 'sessions', :action => 'create'}, {:path => 'users/sign_in', :method => :post}) + end + + test 'map devise destroy user session' do + assert_recognizes({:controller => 'sessions', :action => 'destroy'}, {:path => 'users/sign_out', :method => :get}) + end + + test 'map devise new user confirmation' do assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'users/confirmation/new') end - test 'map devise user password' do + test 'map devise create user confirmation' do + assert_recognizes({:controller => 'confirmations', :action => 'create'}, {:path => 'users/confirmation', :method => :post}) + end + + test 'map devise show user confirmation' do + assert_recognizes({:controller => 'confirmations', :action => 'show'}, {:path => 'users/confirmation', :method => :get}) + end + + test 'map devise new user password' do assert_recognizes({:controller => 'passwords', :action => 'new'}, 'users/password/new') end + test 'map devise create user password' do + assert_recognizes({:controller => 'passwords', :action => 'create'}, {:path => 'users/password', :method => :post}) + end + + test 'map devise edit user password' do + assert_recognizes({:controller => 'passwords', :action => 'edit'}, 'users/password/edit') + end + + test 'map devise update user password' do + assert_recognizes({:controller => 'passwords', :action => 'update'}, {:path => 'users/password', :method => :put}) + end + test 'map devise admin session with :as option' do - assert_recognizes({:controller => 'sessions', :action => 'new'}, 'admin_area/session/new') + assert_recognizes({:controller => 'sessions', :action => 'new'}, {:path => 'admin_area/sign_in', :method => :get}) end test 'does not map devise admin confirmation' do