Skip to content

Instantly share code, notes, and snippets.

@gangster
Created November 19, 2010 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangster/706063 to your computer and use it in GitHub Desktop.
Save gangster/706063 to your computer and use it in GitHub Desktop.
# Application Generator Template
# Devise, Cucumber, RSpec... for use with Rails 3
# http://gist.github.com/513564
# Based on the Mongoid/Devise template: http://github.com/fortuity/rails3-mongoid-devise/raw/master/template.rb
#
#
# Installs my js_lib rakefile, found: http://gist.github.com/raw/628715/js_lib.rake
# (to keep js libraries out of source control.)
#
# Usage:
# rails new app_name -m http://gist.github.com/raw/513564/tpl-devise-rspec-cuke.rb
#
# Will bundle gem versions:
# rspec, rspec-rails 2.0.0
# devise 1.1.3
# factory_girl_rails 1.0
# capybara 0.3.9
# cucumber 0.9.2, cucumber-rails 0.3.2 (optional)
# launchy 0.3.7
require 'open-uri'
unless yes?("WARNING: Gems will be installed. You should set up your gemsets first if using rvm. Continue? (yes/no)")
exit
end
puts "Modifying a new Rails app to use Devise, RSpec, Cucumber..."
cuke_flag = yes?('Use Cucumber? (yes/no)')
jquery_flag = yes?('Use jQuery? (yes/no)')
puts "removing unneeded files..."
run 'rm public/index.html'
run 'rm public/favicon.ico'
run 'rm public/images/rails.png'
run 'rm README'
run 'touch README'
puts "ban spiders..."
gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
puts "setting up Gemfile for #{ "Cucumber, " if cuke_flag }Capybara, RSpec..."
append_file 'Gemfile', "\n# Bundle gems needed for #{ "Cucumber and " if cuke_flag }RSpec\n"
gem 'rspec', '2.0.0', :group => :test
gem 'rspec-rails', '2.0.0', :group => :development
puts "installing rspec gems..."
run 'bundle install'
puts "install rspec..."
run 'rails g rspec:install'
if cuke_flag
gem 'cucumber', '0.9.2'
gem 'cucumber-rails', '0.3.2', :group => :cucumber
gem 'capybara', '0.3.9', :group => :cucumber
gem 'launchy', '0.3.7', :group => :cucumber
puts "installing cuke/capy/launchy gems..."
run 'bundle install'
puts "install cucumber..."
run 'rails g cucumber:install'
end
puts "setting up Gemfile for Factory girl..."
gem 'factory_girl_rails', '1.0', :group => :test
puts "installing launchy/factory girl gems..."
run 'bundle install'
puts "setting up Gemfile for Devise..."
append_file 'Gemfile', "\n# Bundle gem needed for Devise\n"
gem 'devise', '1.1.3'
puts "installing Devise gem..."
run 'bundle install'
puts "creating 'config/initializers/devise.rb' Devise configuration file..."
run 'rails generate devise:install'
puts "modifying environment configuration files for Devise..."
gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '### ActionMailer Config'
gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
<<-RUBY
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# A dummy setup for development - no deliveries, but logged
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
RUBY
end
gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
<<-RUBY
config.i18n.fallbacks = true
config.action_mailer.default_url_options = { :host => 'yourhost.com' }
### ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
RUBY
end
puts "creating a User model w/ added ame string, modifying routes for Devise..."
run 'rails generate devise User'
run 'rails generate migration add_name_to_user name:string'
gsub_file 'app/models/user.rb', /attr_accessible :email, :password, :password_confirmation, :remember_me/ do
<<-RUBY
validates_uniqueness_of :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
RUBY
end
puts "creating a default user"
append_file 'db/seeds.rb' do <<-FILE
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => "Foob Ar", :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name
FILE
end
puts "migrating and seeding the database"
run 'rake db:migrate'
run 'rake db:test:prepare'
run 'rake db:seed'
puts "create a home controller and view"
generate(:controller, "home index")
gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
puts "set up a simple demonstration of Devise"
gsub_file 'app/controllers/home_controller.rb', /def index/ do
<<-RUBY
def index
@users = User.all
RUBY
end
append_file 'app/views/home/index.html.erb' do <<-FILE
<% @users.each do |user| %>
<p>User: <%=link_to user.email, user %></p>
<% end %>
FILE
end
generate(:controller, "users show")
gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
gsub_file 'config/routes.rb', /devise_for :users/ do
<<-RUBY
devise_for :users
resources :users, :only => :show
RUBY
end
gsub_file 'app/controllers/users_controller.rb', /def show/ do
<<-RUBY
before_filter :authenticate_user!
def show
@user = User.find(params[:id])
RUBY
end
append_file 'app/views/users/show.html.erb' do <<-FILE
<p>User: <%= @user.email %></p>
FILE
end
create_file "app/views/devise/menu/_login_items.html.erb" do <<-FILE
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path) %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
FILE
end
create_file "app/views/devise/menu/_registration_items.html.erb" do <<-FILE
<% if user_signed_in? %>
<li>
<%= link_to('Edit account', edit_user_registration_path) %>
</li>
<% else %>
<li>
<%= link_to('Sign up', new_user_registration_path) %>
</li>
<% end %>
FILE
end
gsub_file 'app/views/layouts/application.html.erb', /<%= yield %>/ do
<<-RUBY
<ul class="hmenu">
<%= render 'devise/menu/registration_items' %>
<%= render 'devise/menu/login_items' %>
</ul>
<p style="color: green"><%= notice %></p>
<p style="color: red"><%= alert %></p>
<%= yield %>
RUBY
end
create_file 'public/stylesheets/application.css' do <<-FILE
ul.hmenu {
list-style: none;
margin: 0 0 2em;
padding: 0;
}
ul.hmenu li {
display: inline;
}
FILE
end
if jquery_flag
puts "setting up jQuery"
puts "libraries will not be downloaded here... run rake js:install"
run 'rm public/javascripts/*.js'
run 'touch public/javascripts/application.js'
run 'mkdir -p public/javascripts/lib'
gsub_file('config/application.rb', /# config.action_view.javascript_expansions\[:defaults\] = %w\(jquery rails\)/) do
'config.action_view.javascript_expansions[:defaults] = %w(lib/jquery lib/rails)'
end
create_file 'lib/tasks/js_lib.rake' do
open('http://gist.github.com/raw/628715/js_lib.rake').read
end
end
puts "setting up source control with 'git'..."
# OS X, vim
append_file '.gitignore' do
"public/javascripts/lib\n.DS_Store\n*.swp"
end
git :init
git :add => '.'
git :commit => "-m 'Initial commit'"
puts "Done setting up your Rails app with #{"Cucumber, " if cuke_flag } RSpec and Devise."
puts "Note: Devise is currently using a user's email as an identifier. You may want to change this."
puts "The seed user is email 'user@test.com' pass 'please'."
puts "Run 'rake js:install' to install the jquery and rails-ujs libraries." if jquery_flag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment