Skip to content

Commit

Permalink
Add a setting to disable all routes
Browse files Browse the repository at this point in the history
This setting allows clearance routes to be completely customized. Users who
customize the routes to this level may have to edit the views and mailers as
there are references to routes there.
  • Loading branch information
derekprior committed Oct 17, 2014
1 parent 547fb5b commit 446f41c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
38 changes: 20 additions & 18 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
Rails.application.routes.draw do
resources :passwords,
controller: 'clearance/passwords',
only: [:create, :new]
if Clearance.configuration.routes_enabled?
Rails.application.routes.draw do
resources :passwords,
controller: 'clearance/passwords',
only: [:create, :new]

resource :session,
controller: 'clearance/sessions',
only: [:create]
resource :session,
controller: 'clearance/sessions',
only: [:create]

resources :users,
controller: 'clearance/users',
only: Clearance.configuration.user_actions do
resource :password,
controller: 'clearance/passwords',
only: [:create, :edit, :update]
end
resources :users,
controller: 'clearance/users',
only: Clearance.configuration.user_actions do
resource :password,
controller: 'clearance/passwords',
only: [:create, :edit, :update]
end

get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'
delete '/sign_out' => 'clearance/sessions#destroy', as: 'sign_out'
get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'
delete '/sign_out' => 'clearance/sessions#destroy', as: 'sign_out'

if Clearance.configuration.allow_sign_up?
get '/sign_up' => 'clearance/users#new', as: 'sign_up'
if Clearance.configuration.allow_sign_up?
get '/sign_up' => 'clearance/users#new', as: 'sign_up'
end
end
end
7 changes: 6 additions & 1 deletion lib/clearance/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Clearance
class Configuration
attr_writer :allow_sign_up
attr_writer :allow_sign_up, :routes

attr_accessor \
:cookie_domain,
Expand All @@ -21,6 +21,7 @@ def initialize
@httponly = false
@mailer_sender = '[email protected]'
@redirect_url = '/'
@routes = true
@secure_cookie = false
@sign_in_guards = []
end
Expand All @@ -44,6 +45,10 @@ def user_actions
def user_id_parameter
"#{user_model.model_name.singular}_id".to_sym
end

def routes_enabled?
@routes
end
end

def self.configuration
Expand Down
19 changes: 15 additions & 4 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
end

context 'when no redirect URL specified' do
it 'should return "/" as redirect URL' do
it 'returns "/" as redirect URL' do
expect(Clearance::Configuration.new.redirect_url).to eq '/'
end
end
Expand All @@ -66,7 +66,7 @@
end
end

it 'should return new redirect URL' do
it 'returns new redirect URL' do
expect(Clearance.configuration.redirect_url).to eq new_redirect_url
end
end
Expand All @@ -80,7 +80,7 @@
end
end

it 'should return the stack with added guards' do
it 'returns the stack with added guards' do
expect(Clearance.configuration.sign_in_guards).to eq [DummyGuard]
end
end
Expand Down Expand Up @@ -113,7 +113,7 @@
end
end

describe '#sign_up?' do
describe '#allow_sign_up?' do
context 'when allow_sign_up is configured to false' do
it 'returns false' do
Clearance.configure { |config| config.allow_sign_up = false }
Expand Down Expand Up @@ -151,4 +151,15 @@
expect(Clearance.configuration.user_id_parameter).to eq :custom_user_id
end
end

describe '#routes_enabled?' do
it 'is true by default' do
expect(Clearance.configuration.routes_enabled?).to be true
end

it 'is false when routes are set to false' do
Clearance.configure { |config| config.routes = false }
expect(Clearance.configuration.routes_enabled?).to be false
end
end
end
37 changes: 31 additions & 6 deletions spec/routing/clearance_routes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
require 'spec_helper'

describe 'routes for Clearance' do
context 'signup disabled' do
before(:all) do
Clearance.configure do |config|
config.allow_sign_up = false
end
context 'routes enabled' do
it 'draws the default routes' do
expect(get: 'sign_up').to be_routable
expect(get: 'sign_in').to be_routable
expect(get: 'passwords/new').to be_routable
expect(post: 'session').to be_routable
expect(post: 'passwords').to be_routable
expect(post: 'users').to be_routable
end
end

context 'routes disabled' do
around do |example|
Clearance.configure { |config| config.routes = false }
Rails.application.reload_routes!
example.run
Clearance.configuration = Clearance::Configuration.new
Rails.application.reload_routes!
end

after :all do
it 'does not draw any routes' do
expect(get: 'sign_up').not_to be_routable
expect(get: 'sign_in').not_to be_routable
expect(get: 'passwords/new').not_to be_routable
expect(post: 'session').not_to be_routable
expect(post: 'passwords').not_to be_routable
expect(post: 'users').not_to be_routable
end
end

context 'signup disabled' do
around do |example|
Clearance.configure { |config| config.allow_sign_up = false }
Rails.application.reload_routes!
example.run
Clearance.configuration = Clearance::Configuration.new
Rails.application.reload_routes!
end
Expand Down

0 comments on commit 446f41c

Please sign in to comment.