Rails3 Devise Wizard Gem

Creates a ready-to-run Rails web application using Devise for authentication. Makes it easy to create and maintain a starter app using your own preferred options. This is the gem used to create the rails3-mongoid-devise example application, as described in a detailed tutorial.

Based on Michael Bleigh’s RailsWizard Gem

The rails3_devise_wizard gem is a fork of Michael Bleigh’s RailsWizard gem (see credits below). The purpose of this fork is to provide recipes for a ready-to-run Rails web application that uses Devise for authentication. Several recipes provided by the rails3_devise_wizard gem are different from those provided by the rails_wizard gem. Applications generated with the rails3_devise_wizard gem are more complete; for example, they may include a home page with sign-in, sign-out navigation links.

Suggested Use

Any developer can quickly generate a Rails web application using the rails new command. In practice, experienced Rails developers typically add an assortment of useful additional packages (gems) before beginning development of any web application. A developer often uses the same set of packages to get started and may create a “starter app” that can be copied and reused for any new project.

It can be a hassle to integrate some of the most commonly used packages, particularly when new versions are released and there are minor “gotchas” that interfere with packages working together. Despite the apparent convenience of creating a starter app, it can be time consuming to maintain and update a starter app as component packages evolve. This project aims to simplify the process of building and maintaining a starter app by providing mix-and-match recipes to assemble the most commonly used Rails packages.

If you use this gem to create your Rails starter app, you can expect the pieces to work together. If they don’t, you can report problems and look for identified issues (and perhaps contributed fixes).

If you use this gem to create a reusable application template (see the instructions below), you can check for changes to recipes here and reassemble your application template as packages evolve.

Dependencies

Before generating a new Rails app, you will need:

  • The Ruby language (version 1.8.7 or 1.9.2)
  • Rails (version 3.0.4 or newer)

I recommend installing rvm, the Ruby Version Manager, to manage multiple versions of Rails.

If you are using rvm, you can see a list of the Ruby versions currently installed: $ rvm list

Check that appropriate versions of Ruby and Rails are installed in your development environment: $ ruby -v $ rails -v

Installation

Installation is simple:

$ gem install rails3_devise_wizard

Usage

List Recipes

You can display a list of recipes:

$ rails3_devise_wizard list

You’ll find more details about the available recipes by browsing the repository recipes directory.

Generate a Starter App

There are two ways to use the rails3_devise_wizard gem to generate a starter app.

If you want to build a starter app for one-time use, you can build an application by providing a list of recipes with the -r option. This will automatically generate an application using the specified recipes.

If you want to create and save an application template that you can reuse as needed to “clone” identical starter apps, you can download the rails3_devise_wizard project, customize recipes as needed, and use a rake task to save a reusable application template file.

Each of these approaches is described below.

Make Your Own Starter App for One-Time Use

Select Recipes

You can mix and match recipes to create your own customized starter app. Browse the repository recipes directory to see what is available. Then provide your list of recipes to the rails3_devise_wizard gem using the -r option and generate an app as needed. Here’s an example that creates a simple app using jquery and haml:


$ rails3_devise_wizard new APP_NAME -r jquery haml

To build the rails3-mongoid-devise example application, run the command:


$ rails3_devise_wizard new APP_NAME -r jquery haml rspec cucumber mongoid action_mailer devise add_user home_page home_page_users seed_database users_page css_setup application_layout devise_navigation cleanup ban_spiders git

Make Your Own Starter App with a Reusable Application Template

You can modify the recipes and save an application template that creates your own customized starter app.

First, you’ll need to make your own copy of the rails3_devise_wizard gem.

$ git clone git://github.com/fortuity/rails3_devise_wizard.git

$ cd rails3_devise_wizard

Install the mg “minimal gem” which is required for development of the rails3_devise_wizard gem:

$ gem install mg

Customize the Recipes

Modify or write new recipes as you wish (see below for details about writing recipes). You can run rake spec to make sure your recipes conform to the required syntax.

Save the Application Template

The rails3_devise_wizard gem creates an application template as an intermediate step before generating an application. You can generate and save the application template. Here’s an example of generating an application template and saving the template to a file:


$ rake print --silent RECIPES=recipe1,recipe2 > ~/Desktop/template.txt

If you don’t include the --silent option, the rake task will generate the application template with an extraneous first line. Open the template file and remove the first line if you encounter this problem.

The rails3_devise_wizard gem creates an application template that can be used by the rails new command with the -m option. For example:


$ rails new testapp -m ~/Desktop/template.txt

You can specify the -T -O -J flags as needed to skip Test::Unit files, Active Record files, and Prototype files.

That’s all it takes. You’ll have a ready-to-customize Rails web application in minutes.

Template for Rails 3 + Devise + RSpec + Cucumber

To build a reusable application template for the rails3-devise-rspec-cucumber example application, run the command:


$ rake print --silent RECIPES=jquery,haml,rspec,cucumber,action_mailer,devise,add_user,home_page,home_page_users,seed_database,users_page,css_setup,application_layout,devise_navigation,cleanup,ban_spiders,git > ~/Desktop/template.txt

Then generate the application using the -T -J flags:


$ rails new testapp -m ~/Desktop/template.txt -T -J

Template for Rails 3 + Mongoid + Devise

To build a reusable application template for the rails3-mongoid-devise example application, run the command:


$ rake print --silent RECIPES=jquery,haml,rspec,cucumber,mongoid,action_mailer,devise,add_user,home_page,home_page_users,seed_database,users_page,css_setup,application_layout,devise_navigation,cleanup,ban_spiders,git > ~/Desktop/template.txt

Then generate the application using the -T -O -J flags:


$ rails new testapp -m ~/Desktop/template.txt -T -O -J

Template for Rails 3 + Mongoid + OmniAuth

To build a reusable application template for the rails3-mongoid-omniauth example application, run the command:


$ rake print --silent RECIPES=jquery,haml,rspec,cucumber,mongoid,seed_database,add_user,omniauth,home_page,home_page_users,css_setup,application_layout,navigation,users_page,omniauth_email,cleanup,ban_spiders,git > ~/Desktop/template.txt

Then generate the application using the -T -O -J flags:


$ rails new testapp -m ~/Desktop/template.txt -T -O -J

The Recipes

The following recipes are available.

Recipes for General Use

File Dependencies Authors
action_mailer.rb Changes ActionMailer defaults none dkehoe
ban_spiders.rb Ban spiders from your site by changing robots.txt none dkehoe
cleanup.rb Remove unnecessary files, add READMEs none fnichol, dkehoe
git.rb Add a gitignore file, initialize new Git repo Git fnichol, dkehoe
sample_readme.textile Placeholder text for a README file (textile markup) none dkehoe
sample_readme.txt Placeholder text for a README file (ASCII) none dkehoe

Recipes for Testing and BDD

File Dependencies Authors
rspec.rb Install RSpec none rbrooker, dkehoe
cucumber.rb Install Cucumber none rbrooker, dkehoe

Recipes for Mongoid

File Dependencies Authors
mongoid.rb Installs Mongoid with the bson_ext gem Mongoid dkehoe

Recipes for jQuery

File Dependencies Authors
jquery.rb Installs jQuery none dkehoe

Recipes for Devise

File Dependencies Authors
devise.rb Installs Devise none dkehoe

Recipes for OmniAuth

File Dependencies Authors
omniauth.rb OmniAuth for authentication none dkehoe
omniauth_email.rb Requests a new uer’s email address when authenticating with Twitter, etc. OmniAuth dkehoe

Recipes for Haml

File Dependencies Authors
haml.rb Installs Haml none dkehoe

Recipes for the Application Layout

File Dependencies Authors
application_layout.rb Adds application layout with flash messages ERB or Haml dkehoe
css_setup.rb Add a stylesheet with styles for a horizontal menu and flash messages none dkehoe

Recipes for the Example Apps

File Dependencies Authors
add_user.rb adds User model for use with OmniAuth or Devise none dkehoe
navigation.rb Add navigation links to the default application layout none dkehoe
devise_navigation.rb Add navigation links to the default application layout Devise dkehoe
home_page.rb Create a home controller, route, and simple view ERB or Haml dkehoe
home_page_users.rb Modify a home page to display a list of users Home controller, User model, ERB or Haml dkehoe
seed_database.rb Adds a db/seeds.rb file to initialize a database none dkehoe
users_page.rb Add a home page containing links to User pages User model, ERB or Haml dkehoe

Writing New Recipes

You can find the rails3_devise_wizard recipe collection in the GitHub repository’s recipes directory. If you find errors or improve a recipe you can contribute to the project by submitting a pull request or creating a Github issue.

Rails Wizard Basics

For more information on all available options for authoring recipes that can be read by the rails_wizard or rails3_devise_wizard gems, please see the wiki for Michael Bleigh’s RailsWizard gem.

Recipes are made of up template code and YAML back-matter stored in a ruby file. The __END__ parsing convention is used so that each recipe is actually a valid, parseable Ruby file. The structure of a recipe looks something like this:


gem 'supergem'

after_bundler do
  generate "supergem:install"
end

__END__

Rails3 Devise Wizard Differences

The rails3_devise_wizard gem is very similar to the rails_wizard gem, with one significant difference. The rails_wizard gem allows specification of execution order for recipes with run_after and run_after configuration flags. The rails3_devise_wizard gem does not support run_after or run_after; instead, the order which you provide the recipes sets the execution order. This makes it easier to chain a series of recipes in the order you prefer. For example,


$ rails3_devise_wizard new APP_NAME -r git jquery haml

installs jquery before haml.

Recipe Differences

Several recipes provided by the rails3_devise_wizard gem are different from those provided by the rails_wizard gem.

Rails Wizard RSpec Tests

The gem has RSpec tests that automatically validate each recipe in the repository, so you should run rake spec as a basic syntax check. Note that these don’t verify that your recipe code itself works, just that the gem could properly parse and understand your recipe file.

How It Works

Rails generators can use any methods provided by the Thor::Actions module. The flexibility of mixing “recipes” for application templates comes from use of the apply method from the Thor::Actions module. Given a web address or a local filepath, the apply method loads and executes a file within the context of the generator script.

Documentation and Support

This is the only documentation.

Writing Recipes

To understand the code in these templates, take a look at Thor::Actions. Your recipes can use any methods provided by Thor::Actions or Rails::Generators::Actions.

About Rails Application Templates

Cooking Up A Custom Rails 3 Template (11 Oct 2010) by Andrea Singh Rails Application Templates (16 Sept 2010) by Collin Schaafsma Application templates in Rails 3 (18 Sept 2009) by Ben Scofield Railscasts: App Templates in Rails 2.3 (9 Feb 2009) by Ryan Bates Rails templates (4 Dec 2008) by Pratik Naik

Issues

Any issues? Please create a GitHub issue.

Credits

This project is based on Michael Bleigh’s RailsWizard gem. The original idea for a RailsWizard and the innovative implementation is the work of Michael Bleigh.

Fletcher Nichol’s project fnichol/rails-template-recipes provides the basis for several recipes.

RSpec, Cucumber, and Yard recipes were contributed by Ramon Brooker.

Additional recipes by Daniel Kehoe, http://danielkehoe.com/, to implement the rails3-mongoid-devise example application.

Is the gem useful to you? Follow the project on Twitter: http://twitter.com/rails_apps and tweet some praise. I’d love to know you were helped out by the gem.

License

MIT License

The rails3_devise_wizard gem and its recipes are distributed under the MIT License.