Skip to content
Browse files

Add `redirect_url` config option

* Clarify in README that the config example shows the default values.
* Wrap long line at 80 characters.
* Move `redirect_to_root` from `lib/clearance/authorization.rb` to where
  it is used, in `app/controllers/clearance/sessions_controller.rb`, and
  better reveal its intent by re-naming it to `avoid_sign_in`.
* Re-set `config.secure_cookie` to its original value in an `after`
  block in a test to teardown and avoid leakage across tests.
* Use `_url` suffix in config name to match [RFC 2616 spec, section 14.
  14.30](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30),
  which states the Location response-header field should use an absolute
  URI for 3xx responses.
* The default value for `redirect_url` is the string path `'/'` as a
  previous compromise to make it less likely users will run into an
  issue if they don't want to define a root route. The string value avoids
  potentially confusing `NoMethodError: undefined method 'root_url'`
  errors.

#281
  • Loading branch information...
1 parent 6c22c6a commit d9d71c18a31a6b74b3a1b7d16a45f55485dfda5e @localhots localhots committed with croaky Mar 22, 2013
View
3 README.md
@@ -51,14 +51,15 @@ Use [0.16.3](http://rubygems.org/gems/clearance/versions/0.16.3) for Ruby 1.8.7.
Configure
---------
-Override any of the defaults in `config/initializers/clearance.rb`:
+Override any of these defaults in `config/initializers/clearance.rb`:
Clearance.configure do |config|
config.cookie_expiration = lambda { 1.year.from_now.utc }
config.secure_cookie = false
config.mailer_sender = 'reply@example.com'
config.password_strategy = Clearance::PasswordStrategies::BCrypt
config.user_model = User
+ config.redirect_path = '/'
@localhots
localhots added a note Mar 26, 2013

config.redirect_url = '/'

@croaky
thoughtbot, inc. member
croaky added a note Mar 26, 2013

Thanks, done: d1700fa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
Use
View
5 app/controllers/clearance/passwords_controller.rb
@@ -55,7 +55,8 @@ def find_user_by_id_and_confirmation_token
end
def find_user_for_create
- Clearance.configuration.user_model.find_by_normalized_email params[:password][:email]
+ Clearance.configuration.user_model.
+ find_by_normalized_email params[:password][:email]
end
def find_user_for_edit
@@ -97,6 +98,6 @@ def url_after_create
end
def url_after_update
- '/'
+ Clearance.configuration.redirect_url
end
end
View
2 app/controllers/clearance/sessions_controller.rb
@@ -32,7 +32,7 @@ def flash_failure_after_create
end
def url_after_create
- '/'
+ Clearance.configuration.redirect_url
end
def url_after_destroy
View
8 app/controllers/clearance/users_controller.rb
@@ -1,6 +1,6 @@
class Clearance::UsersController < ApplicationController
skip_before_filter :authorize, :only => [:create, :new]
- before_filter :redirect_to_root, :only => [:create, :new], :if => :signed_in?
+ before_filter :avoid_sign_in, :only => [:create, :new], :if => :signed_in?
def new
@user = user_from_params
@@ -20,8 +20,12 @@ def create
private
+ def avoid_sign_in
+ redirect_to Clearance.configuration.redirect_url
+ end
+
def url_after_create
- '/'
+ Clearance.configuration.redirect_url
end
def user_from_params
View
6 lib/clearance/authorization.rb
@@ -43,16 +43,12 @@ def redirect_back_or(default)
clear_return_to
end
- def redirect_to_root
- redirect_to('/')
- end
-
def return_to
session[:return_to] || params[:return_to]
end
def url_after_denied_access_when_signed_in
- '/'
+ Clearance.configuration.redirect_url
end
def url_after_denied_access_when_signed_out
View
2 lib/clearance/configuration.rb
@@ -4,13 +4,15 @@ class Configuration
:cookie_expiration,
:mailer_sender,
:password_strategy,
+ :redirect_url,
:secure_cookie,
:user_model
def initialize
@cookie_expiration = lambda { 1.year.from_now.utc }
@mailer_sender = 'reply@example.com'
@secure_cookie = false
+ @redirect_url = '/'
end
def user_model
View
32 spec/configuration_spec.rb
@@ -41,6 +41,12 @@
end
end
+ after do
+ Clearance.configure do |config|
+ config.secure_cookie = false
+ end
+ end
+
it 'returns true' do
Clearance.configuration.secure_cookie.should be_true
end
@@ -56,4 +62,30 @@
Clearance.configuration.secure_cookie.should be_false
end
end
+
+ describe 'when no redirect URL specified' do
+ it 'should return "/" as redirect URL' do
+ Clearance::Configuration.new.redirect_url.should == '/'
+ end
+ end
+
+ describe 'when redirect URL is specified' do
+ let(:new_redirect_url) { '/admin' }
+
+ before do
+ Clearance.configure do |config|
+ config.redirect_url = new_redirect_url
+ end
+ end
+
+ after do
+ Clearance.configure do |config|
+ config.redirect_url = '/'
+ end
+ end
+
+ it 'should return new redirect URL' do
+ Clearance.configuration.redirect_url.should == new_redirect_url
+ end
+ end
end
View
4 spec/controllers/users_controller_spec.rb
@@ -68,15 +68,15 @@
before { get :new }
it 'redirects to the home page' do
- should redirect_to(root_url)
+ should redirect_to(Clearance.configuration.redirect_url)
end
end
describe 'POST to create' do
before { post :create, :user => {} }
it 'redirects to the home page' do
- should redirect_to(root_url)
+ should redirect_to(Clearance.configuration.redirect_url)
end
end
end

0 comments on commit d9d71c1

Please sign in to comment.
Something went wrong with that request. Please try again.