Skip to content

Commit

Permalink
Add generator to copy default routes to the app
Browse files Browse the repository at this point in the history
The generator configures clearance to turn off its internal routes and
then copies the default routes to the host application's routes file
where they can be customized to the developer's content.
  • Loading branch information
Jacob Morris authored and derekprior committed Oct 17, 2014
1 parent 446f41c commit 2a09afe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions features/copy_routes_to_host_application.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: copy routes to host application

Background:
Given I have a project with clearance

Scenario:
When I successfully run `bundle exec rails generate clearance:install`
And I successfully run `bundle exec rails generate clearance:routes`
Then the file "config/initializers/clearance.rb" should contain "config.routes = false"
And the file "config/routes.rb" should contain "get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'"
31 changes: 31 additions & 0 deletions lib/generators/clearance/routes/routes_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails/generators/base'

module Clearance
module Generators
class RoutesGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

def disable_clearance_routes_in_config
inject_into_file(
'config/initializers/clearance.rb',
' config.routes = false\n',
after: /^Clearance\.configure do |config|\n/
)
end

def inject_clearance_routes_into_application_routes
route(clearance_routes)
end

private

def clearance_routes
File.read(routes_file_path)
end

def routes_file_path
File.expand_path(find_in_source_paths('routes.rb'))
end
end
end
end
12 changes: 12 additions & 0 deletions lib/generators/clearance/routes/templates/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resources :passwords, controller: 'clearance/passwords', only: [:create, :new]
resource :session, controller: 'clearance/sessions', only: [:create]

resources :users, controller: 'clearance/users', only: [:create] 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_up' => 'clearance/users#new', as: 'sign_up'

0 comments on commit 2a09afe

Please sign in to comment.