From ac8221aca72333fe96bed60e2b4d0887584ecbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Jul 2010 21:58:35 +0200 Subject: [PATCH] Add cancel to registrations controller as a way to delete information from session. --- app/controllers/devise/registrations_controller.rb | 10 +++++++++- lib/devise/rails/routes.rb | 11 +++++++++-- test/integration/registerable_test.rb | 12 ++++++++++++ test/rails_app/app/controllers/home_controller.rb | 5 +++++ test/rails_app/config/routes.rb | 3 ++- test/routes_test.rb | 10 ++++++++++ 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/controllers/devise/registrations_controller.rb b/app/controllers/devise/registrations_controller.rb index e59feac2..f173d424 100644 --- a/app/controllers/devise/registrations_controller.rb +++ b/app/controllers/devise/registrations_controller.rb @@ -1,5 +1,5 @@ class Devise::RegistrationsController < ApplicationController - prepend_before_filter :require_no_authentication, :only => [ :new, :create ] + prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ] prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy] include Devise::Controllers::InternalHelpers @@ -45,6 +45,14 @@ class Devise::RegistrationsController < ApplicationController sign_out_and_redirect(self.resource) end + # GET /resource/cancel + # Forces the session data which is usually expired after sign + # in to be expired now. + def cancel + expire_session_data_after_sign_in! + redirect_to new_registration_path(resource_name) + end + protected # Build a devise resource passing in the session. Useful to move diff --git a/lib/devise/rails/routes.rb b/lib/devise/rails/routes.rb index 083ae805..6e5a18ee 100644 --- a/lib/devise/rails/routes.rb +++ b/lib/devise/rails/routes.rb @@ -216,8 +216,15 @@ module ActionDispatch::Routing end def devise_registration(mapping, controllers) #:nodoc: - resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration], - :path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations] + path_names = { + :new => mapping.path_names[:sign_up], + :cancel => mapping.path_names[:cancel] + } + + resource :registration, :except => :show, :path => mapping.path_names[:registration], + :path_names => path_names, :controller => controllers[:registrations] do + get :cancel + end end def devise_oauth_callback(mapping, controllers) #:nodoc: diff --git a/test/integration/registerable_test.rb b/test/integration/registerable_test.rb index 0da7db4d..f256bcab 100644 --- a/test/integration/registerable_test.rb +++ b/test/integration/registerable_test.rb @@ -150,4 +150,16 @@ class RegistrationTest < ActionController::IntegrationTest assert User.all.empty? end + + test 'a user should be able to cancel sign up by deleting data in the session' do + get "/set" + assert_equal "something", @request.session["user_facebook_oauth_token"] + + get "/users/sign_up" + assert_equal "something", @request.session["user_facebook_oauth_token"] + + get "/users/cancel" + assert_nil @request.session["user_facebook_oauth_token"] + assert_redirected_to new_user_registration_path + end end diff --git a/test/rails_app/app/controllers/home_controller.rb b/test/rails_app/app/controllers/home_controller.rb index 78d0d799..039065aa 100644 --- a/test/rails_app/app/controllers/home_controller.rb +++ b/test/rails_app/app/controllers/home_controller.rb @@ -4,4 +4,9 @@ class HomeController < ApplicationController def private end + + def set + session["user_facebook_oauth_token"] = "something" + head :ok + end end diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index b2566cb7..db38ad21 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -39,9 +39,10 @@ Rails.application.routes.draw do :sign_in => "login", :sign_out => "logout", :password => "secret", :confirmation => "verification", :unlock => "unblock", :sign_up => "register", - :registration => "management" + :registration => "management", :cancel => "giveup" } end + match "/set", :to => "home#set" root :to => "home#index" end \ No newline at end of file diff --git a/test/routes_test.rb b/test/routes_test.rb index cb97110f..ff04e08d 100644 --- a/test/routes_test.rb +++ b/test/routes_test.rb @@ -86,6 +86,11 @@ class DefaultRoutingTest < ActionController::TestCase assert_recognizes({:controller => 'devise/registrations', :action => 'destroy'}, {:path => 'users', :method => :delete}) end + test 'map cancel user registration' do + assert_recognizes({:controller => 'devise/registrations', :action => 'cancel'}, {:path => 'users/cancel', :method => :get}) + assert_named_route "/users/cancel", :cancel_user_registration_path + end + test 'map oauth callbacks' do assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'facebook'}, {:path => 'users/oauth/facebook/callback', :method => :get}) assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'github'}, {:path => 'users/oauth/github/callback', :method => :get}) @@ -140,6 +145,11 @@ class CustomizedRoutingTest < ActionController::TestCase test 'map account with custom path name for registration' do assert_recognizes({:controller => 'devise/registrations', :action => 'new', :locale => 'en'}, '/en/accounts/management/register') end + + test 'map account with custom path name for cancel registration' do + assert_recognizes({:controller => 'devise/registrations', :action => 'cancel', :locale => 'en'}, '/en/accounts/management/giveup') + end + end class ScopedRoutingTest < ActionController::TestCase