Skip to content

Instantly share code, notes, and snippets.

@mgswolf
Created July 11, 2012 18:24
Show Gist options
  • Save mgswolf/3092167 to your computer and use it in GitHub Desktop.
Save mgswolf/3092167 to your computer and use it in GitHub Desktop.
Rails Template
# Application Generator Template
# Modifies a Rails app to use Devise with RSpec and Cucumber
# Usage: rails new APP_NAME -m rails_template.rb -T
# Information and a tutorial:
# https://github.com/RailsApps/rails3-devise-rspec-cucumber
# Generated using the rails_apps_composer gem:
# https://github.com/RailsApps/rails_apps_composer/
# Based on application template recipes by:
# Michael Bleigh https://github.com/mbleigh
# Fletcher Nichol https://github.com/fnichol
# Daniel Kehoe https://github.com/fortuity
# Ramon Brooker https://github.com/cognition
# If you are customizing this template, you can use any methods provided by Thor::Actions
# http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
# and Rails::Generators::Actions
# http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
# >---------------------------------------------------------------------------<
#
# _____ _ _ __ ___ _
# | __ \ (_) | \ \ / (_) | |
# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
#
# This template was generated by rails_apps_composer, a custom version of
# RailsWizard, the application template builder. For more information, see:
# https://github.com/RailsApps/rails_apps_composer/
#
# >---------------------------------------------------------------------------<
# >----------------------------[ Initial Setup ]------------------------------<
initializer 'generators.rb', <<-RUBY
Rails.application.config.generators do |g|
end
RUBY
@recipes = ["activerecord","haml", "rspec", "cucumber", "guard", "action_mailer", "devise", "add_user", "home_page", "home_page_users", "seed_database", "users_page", "html5", "simple_form", "cleanup", "extras", "git", "paperclip","friendly_id"]
def recipes; @recipes end
def recipe?(name); @recipes.include?(name) end
def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end
def ask_wizard(question)
ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
end
def yes_wizard?(question)
answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
case answer.downcase
when "yes", "y"
true
when "no", "n"
false
else
yes_wizard?(question)
end
end
def no_wizard?(question); !yes_wizard?(question) end
def multiple_choice(question, choices)
say_custom('question', question)
values = {}
choices.each_with_index do |choice,i|
values[(i + 1).to_s] = choice[1]
say_custom (i + 1).to_s + ')', choice[0]
end
answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
values[answer]
end
@current_recipe = nil
@configs = {}
@after_blocks = []
def after_bundler(&block); @after_blocks << [@current_recipe, block]; end
@after_everything_blocks = []
def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end
@before_configs = {}
def before_config(&block); @before_configs[@current_recipe] = block; end
# this application template only supports Rails version 3.1 and newer
case Rails::VERSION::MAJOR.to_s
when "3"
case Rails::VERSION::MINOR.to_s
when "2"
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
when "1"
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
when "0"
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported. Try 3.1 or newer."
raise StandardError.new "Rails #{Rails::VERSION::STRING} is not supported. Try 3.1 or newer."
else
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
end
else
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported. Try 3.1 or newer."
raise StandardError.new "Rails #{Rails::VERSION::STRING} is not supported. Try 3.1 or newer."
end
say_wizard "Checking configuration. Please confirm your preferences."
# >---------------------------[ Autoload Modules/Classes ]-----------------------------<
inject_into_file 'config/application.rb', :after => 'config.autoload_paths += %W(#{config.root}/extras)' do <<-'RUBY'
config.autoload_paths += %W(#{config.root}/lib)
RUBY
end
# >---------------------------------[ Recipes ]----------------------------------<
# >-----------------------------[ ActiveRecord ]------------------------------<
@current_recipe = "activerecord"
@before_configs["activerecord"].call if @before_configs["activerecord"]
say_recipe 'ActiveRecord'
config = {}
config['database'] = multiple_choice("Which database are you using?", [["MySQL", "mysql"], ["Oracle", "oracle"], ["PostgreSQL", "postgresql"], ["SQLite", "sqlite3"], ["Frontbase", "frontbase"], ["IBM DB", "ibm_db"]]) if true && true unless config.key?('database')
config['auto_create'] = yes_wizard?("Automatically create database with default configuration?") if true && true unless config.key?('auto_create')
@configs[@current_recipe] = config
if config['database']
say_wizard "Configuring '#{config['database']}' database settings..."
old_gem = gem_for_database
@options = @options.dup.merge(:database => config['database'])
gsub_file 'Gemfile', "gem '#{old_gem}'", "gem '#{gem_for_database}'"
template "config/databases/#{@options[:database]}.yml", "config/database.yml.new"
run 'mv config/database.yml.new config/database.yml'
db_name = ask('What is the database name?')
db_username = ask("What is the database username?")
db_password = ask 'What is the user password?'
['_test','_development','_production'].each do |opcao|
gsub_file "config/database.yml", "database: #{app_name+opcao}", "database: #{db_name+opcao}"
end
gsub_file "config/database.yml", "username: #{app_name}", "username: #{db_username}"
inject_into_file "config/database.yml", " #{db_password}", :after => "password:", :force => true
end
after_bundler do
rake "db:create:all" if config['auto_create']
end
# >---------------------------------[ HAML ]----------------------------------<
@current_recipe = "haml"
@before_configs["haml"].call if @before_configs["haml"]
say_recipe 'HAML'
config = {}
config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/haml.rb
if config['haml']
gem 'haml', '>= 3.1.6'
gem 'haml-rails', '>= 0.3.4', :group => :development
else
recipes.delete('haml')
end
# >---------------------------------[ RSpec ]---------------------------------<
@current_recipe = "rspec"
@before_configs["rspec"].call if @before_configs["rspec"]
say_recipe 'RSpec'
config = {}
config['rspec'] = yes_wizard?("Would you like to use RSpec instead of TestUnit?") if true && true unless config.key?('rspec')
config['factory_girl'] = yes_wizard?("Would you like to use factory_girl for test fixtures with RSpec?") if true && true unless config.key?('factory_girl')
config['machinist'] = yes_wizard?("Would you like to use machinist for test fixtures with RSpec?") if true && true unless config.key?('machinist')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/rspec.rb
if config['rspec']
gem 'rspec-rails', '>= 2.10.1', :group => [:development, :test]
if recipes.include? 'mongoid'
# use the database_cleaner gem to reset the test database
gem 'database_cleaner', '>= 0.8.0', :group => :test
# include RSpec matchers from the mongoid-rspec gem
gem 'mongoid-rspec', '>= 1.4.4', :group => :test
end
if config['machinist']
gem 'machinist', :group => :test
end
if config['factory_girl']
gem 'factory_girl_rails', '>= 3.3.0', :group => [:development, :test]
end
# add a collection of RSpec matchers and Cucumber steps to make testing email easy
gem 'email_spec', '>= 1.2.1', :group => :test
create_file 'features/support/email_spec.rb' do <<-RUBY
require 'email_spec/cucumber'
RUBY
end
else
recipes.delete('rspec')
end
# note: there is no need to specify the RSpec generator in the config/application.rb file
if config['rspec']
after_bundler do
say_wizard "RSpec recipe running 'after bundler'"
generate 'rspec:install'
generate 'email_spec:steps'
inject_into_file 'spec/spec_helper.rb', "require 'email_spec'\n", :after => "require 'rspec/rails'\n"
inject_into_file 'spec/spec_helper.rb', :after => "RSpec.configure do |config|\n" do <<-RUBY
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
RUBY
end
if config['machinist']
say_wizard "Generating blueprints file for Machinist"
generate 'machinist:install'
end
say_wizard "Removing test folder (not needed for RSpec)"
run 'rm -rf test/'
inject_into_file 'config/application.rb', :after => "Rails::Application\n" do <<-RUBY
# don't generate RSpec tests for views and helpers
config.generators do |g|
g.view_specs false
g.helper_specs false
#{"g.fixture_replacement :machinist" if config['machinist']}
end
RUBY
end
if recipes.include? 'mongoid'
# remove ActiveRecord artifacts
gsub_file 'spec/spec_helper.rb', /config.fixture_path/, '# config.fixture_path'
gsub_file 'spec/spec_helper.rb', /config.use_transactional_fixtures/, '# config.use_transactional_fixtures'
# reset your application database to a pristine state during testing
inject_into_file 'spec/spec_helper.rb', :before => "\nend" do
<<-RUBY
\n
# Clean up the database
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
RUBY
end
# remove either possible occurrence of "require rails/test_unit/railtie"
gsub_file 'config/application.rb', /require 'rails\/test_unit\/railtie'/, '# require "rails/test_unit/railtie"'
gsub_file 'config/application.rb', /require "rails\/test_unit\/railtie"/, '# require "rails/test_unit/railtie"'
# configure RSpec to use matchers from the mongoid-rspec gem
create_file 'spec/support/mongoid.rb' do
<<-RUBY
RSpec.configure do |config|
config.include Mongoid::Matchers
end
RUBY
end
end
if recipes.include? 'devise'
# add Devise test helpers
create_file 'spec/support/devise.rb' do
<<-RUBY
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
RUBY
end
end
end
end
# >-------------------------------[ Cucumber ]--------------------------------<
@current_recipe = "cucumber"
@before_configs["cucumber"].call if @before_configs["cucumber"]
say_recipe 'Cucumber'
config = {}
config['cucumber'] = yes_wizard?("Would you like to use Cucumber for your BDD?") if true && true unless config.key?('cucumber')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/cucumber.rb
if config['cucumber']
gem 'cucumber-rails', '>= 1.3.0', :group => :test, :require => false
gem 'capybara', '>= 1.1.2', :group => :test
gem 'database_cleaner', '>= 0.8.0', :group => :test
gem 'launchy', '>= 2.1.0', :group => :test
else
recipes.delete('cucumber')
end
if config['cucumber']
after_bundler do
say_wizard "Cucumber recipe running 'after bundler'"
generate "cucumber:install --capybara#{' --rspec' if recipes.include?('rspec')}#{' -D' if recipes.include?('mongoid')}"
# make it easy to run Cucumber for single features without adding "--require features" to the command line
gsub_file 'config/cucumber.yml', /std_opts = "/, 'std_opts = "-r features/support/ -r features/step_definitions '
if recipes.include? 'mongoid'
gsub_file 'features/support/env.rb', /transaction/, "truncation"
inject_into_file 'features/support/env.rb', :after => 'begin' do
"\n DatabaseCleaner.orm = 'mongoid'"
end
end
end
end
if config['cucumber']
if recipes.include? 'devise'
after_bundler do
say_wizard "Copying Cucumber scenarios from the rails3-devise-rspec-cucumber examples"
begin
# copy all the Cucumber scenario files from the rails3-devise-rspec-cucumber example app
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/users/sign_in.feature', 'features/users/sign_in.feature'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/users/sign_out.feature', 'features/users/sign_out.feature'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/users/sign_up.feature', 'features/users/sign_up.feature'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/users/user_edit.feature', 'features/users/user_edit.feature'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/users/user_show.feature', 'features/users/user_show.feature'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/step_definitions/user_steps.rb', 'features/step_definitions/user_steps.rb'
remove_file 'features/support/paths.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/features/support/paths.rb', 'features/support/paths.rb'
if recipes.include? 'devise-confirmable'
gsub_file 'features/step_definitions/user_steps.rb', /Welcome! You have signed up successfully./, "A message with a confirmation link has been sent to your email address."
inject_into_file 'features/users/sign_in.feature', :before => ' Scenario: User signs in successfully' do
<<-RUBY
Scenario: User has not confirmed account
Given I exist as an unconfirmed user
And I am not logged in
When I sign in with valid credentials
Then I see an unconfirmed account message
And I should be signed out
RUBY
end
end
rescue OpenURI::HTTPError
say_wizard "Unable to obtain Cucumber example files from the repo"
end
end
end
end
# >---------------------------------[ guard ]---------------------------------<
@current_recipe = "guard"
@before_configs["guard"].call if @before_configs["guard"]
say_recipe 'guard'
config = {}
config['guard'] = multiple_choice("Would you like to use Guard to automate your workflow?", [["No", false], ["Guard default configuration", "standard"], ["Guard with LiveReload", "LiveReload"]]) if true && true unless config.key?('guard')
@configs[@current_recipe] = config
case config['guard']
when 'no'
recipes.delete('guard')
say_wizard "Guard recipe skipped."
when 'standard'
# do nothing
when 'LiveReload'
recipes << 'guard-LiveReload'
else
recipes.delete('guard')
say_wizard "Guard recipe skipped."
end
if recipes.include? 'guard'
gem 'guard', '>= 0.6.2', :group => :development
prepend_file 'Gemfile' do <<-RUBY
require 'rbconfig'
HOST_OS = RbConfig::CONFIG['host_os']
RUBY
end
append_file 'Gemfile' do <<-RUBY
# need newline here!
case HOST_OS
when /darwin/i
gem 'rb-fsevent', :group => :development
gem 'growl', :group => :development
when /linux/i
gem 'libnotify', :group => :development
gem 'rb-inotify', :group => :development
when /mswin|windows/i
gem 'rb-fchange', :group => :development
gem 'win32console', :group => :development
gem 'rb-notifu', :group => :development
end
RUBY
end
def guards
@guards ||= []
end
def guard(name, version = nil)
args = []
if version
args << version
end
args << { :group => :development }
gem "guard-#{name}", *args
guards << name
end
guard 'bundler', '>= 0.1.3'
unless recipes.include? 'pow'
guard 'rails', '>= 0.0.3'
end
if recipes.include? 'guard-LiveReload'
guard 'livereload', '>= 0.3.0'
end
if recipes.include? 'rspec'
guard 'rspec', '>= 0.4.3'
end
if recipes.include? 'cucumber'
guard 'cucumber', '>= 0.6.1'
end
after_bundler do
run 'guard init'
guards.each do |name|
run "guard init #{name}"
end
end
else
recipes.delete 'guard'
end
# >-----------------------------[ ActionMailer ]------------------------------<
@current_recipe = "action_mailer"
@before_configs["action_mailer"].call if @before_configs["action_mailer"]
say_recipe 'ActionMailer'
config = {}
config['mailer'] = multiple_choice("How will you send email?", [["SMTP account", "smtp"], ["Gmail account", "gmail"], ["SendGrid account", "sendgrid"], ["Mandrill by MailChimp account", "mandrill"]]) if true && true unless config.key?('mailer')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/action_mailer.rb
case config['mailer']
when 'smtp'
recipes << 'smtp'
when 'gmail'
recipes << 'gmail'
when 'sendgrid'
gem 'sendgrid'
recipes << 'sendgrid'
when 'mandrill'
gem 'hominid'
recipes << 'mandrill'
end
after_bundler do
### modifying environment configuration files for ActionMailer
say_wizard "ActionMailer recipe running 'after bundler'"
### development environment
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' }
config.action_mailer.delivery_method = :smtp
# change to true to allow email to be sent during development
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
RUBY
end
### test environment
inject_into_file 'config/environments/test.rb', :before => "\nend" do
<<-RUBY
\n
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'example.com' }
RUBY
end
### production environment
gsub_file 'config/environments/production.rb', /config.active_support.deprecation = :notify/ do
<<-RUBY
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'example.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
### modifying environment configuration files to send email using a GMail account
if recipes.include? 'gmail'
GMAIL_USERNAME = ask('What is the Gmail username?')
GMAIL_PASSWORD = ask('What is the Gamil password?')
gmail_configuration_text = <<-TEXT
\n
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "#{GMAIL_USERNAME}",
password: "#{GMAIL_PASSWORD}"
}
TEXT
say_wizard gmail_configuration_text
inject_into_file 'config/environments/development.rb', gmail_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
inject_into_file 'config/environments/production.rb', gmail_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
end
### modifying environment configuration files to send email using a SendGrid account
if recipes.include? 'sendgrid'
sendgrid_configuration_text = <<-TEXT
\n
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 25,
domain: "example.com",
authentication: "plain",
user_name: ENV["SENDGRID_USERNAME"],
password: ENV["SENDGRID_PASSWORD"]
}
TEXT
say_wizard gmail_configuration_text
inject_into_file 'config/environments/development.rb', sendgrid_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
inject_into_file 'config/environments/production.rb', sendgrid_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
end
### modifying environment configuration files to send email using a Mandrill account
if recipes.include? 'mandrill'
mandrill_configuration_text = <<-TEXT
\n
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 25,
:user_name => ENV["MANDRILL_USERNAME"],
:password => ENV["MANDRILL_API_KEY"]
}
TEXT
say_wizard gmail_configuration_text
inject_into_file 'config/environments/development.rb', mandrill_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
inject_into_file 'config/environments/production.rb', mandrill_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
end
end
# >--------------------------------[ Devise ]---------------------------------<
@current_recipe = "devise"
@before_configs["devise"].call if @before_configs["devise"]
say_recipe 'Devise'
config = {}
config['devise'] = multiple_choice("Would you like to use Devise for authentication?", [["No", false], ["Devise with default modules", "standard"], ["Devise with Confirmable module", "confirmable"], ["Devise with Confirmable and Invitable modules", "invitable"]]) if true && true unless config.key?('devise')
config['authorization'] = yes_wizard?("Would you like to manage authorization with CanCan & Rolify?") if true && true unless config.key?('authorization')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/devise.rb
case config['devise']
when 'no'
recipes.delete('devise')
say_wizard "Devise recipe skipped."
when 'standard'
gem 'devise', '>= 2.1.0'
when 'confirmable'
gem 'devise', '>= 2.1.0'
recipes << 'devise-confirmable'
when 'invitable'
gem 'devise', '>= 2.1.0'
gem 'devise_invitable', '>= 1.0.2'
recipes << 'devise-confirmable'
recipes << 'devise-invitable'
else
recipes.delete('devise')
say_wizard "Devise recipe skipped."
end
if config['authorization']
gem 'cancan', '>= 1.6.7'
gem 'rolify', '>= 3.1.0'
recipes << 'authorization'
end
if recipes.include? 'devise'
after_bundler do
say_wizard "Devise recipe running 'after bundler'"
# Run the Devise generator
generate 'devise:install' unless recipes.include? 'datamapper'
generate 'devise_invitable:install' if recipes.include? 'devise-invitable'
if recipes.include? 'mongo_mapper'
gem 'mm-devise'
gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model'
generate 'mongo_mapper:devise User'
elsif recipes.include? 'mongoid'
# Nothing to do (Devise changes its initializer automatically when Mongoid is detected)
# gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
end
# Prevent logging of password_confirmation
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
if recipes.include? 'cucumber'
# Cucumber wants to test GET requests not DELETE requests for destroy_user_session_path
# (see https://github.com/RailsApps/rails3-devise-rspec-cucumber/issues/3)
gsub_file 'config/initializers/devise.rb', 'config.sign_out_via = :delete', 'config.sign_out_via = Rails.env.test? ? :get : :delete'
end
if config['authorization']
inject_into_file 'app/controllers/application_controller.rb', :before => 'end' do <<-RUBY
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
RUBY
end
end
end
after_everything do
say_wizard "Devise recipe running 'after everything'"
if recipes.include? 'rspec'
say_wizard "Copying RSpec files from the rails3-devise-rspec-cucumber examples"
begin
# copy all the RSpec specs files from the rails3-devise-rspec-cucumber example app
remove_file 'spec/factories/users.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/factories/users.rb', 'spec/factories/users.rb'
gsub_file 'spec/factories/users.rb', /# confirmed_at/, "confirmed_at" if recipes.include? 'devise-confirmable'
remove_file 'spec/controllers/home_controller_spec.rb'
remove_file 'spec/controllers/users_controller_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/home_controller_spec.rb', 'spec/controllers/home_controller_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/users_controller_spec.rb', 'spec/controllers/users_controller_spec.rb'
remove_file 'spec/models/user_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/models/user_spec.rb', 'spec/models/user_spec.rb'
rescue OpenURI::HTTPError
say_wizard "Unable to obtain RSpec example files from the repo"
end
remove_file 'spec/views/home/index.html.erb_spec.rb'
remove_file 'spec/views/home/index.html.haml_spec.rb'
remove_file 'spec/views/users/show.html.erb_spec.rb'
remove_file 'spec/views/users/show.html.haml_spec.rb'
remove_file 'spec/helpers/home_helper_spec.rb'
remove_file 'spec/helpers/users_helper_spec.rb'
end
end
end
# >--------------------------------[ AddUser ]--------------------------------<
@current_recipe = "add_user"
@before_configs["add_user"].call if @before_configs["add_user"]
say_recipe 'AddUser'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/add_user.rb
after_bundler do
say_wizard "AddUser recipe running 'after bundler'"
if recipes.include? 'omniauth'
generate(:model, "user provider:string uid:string name:string email:string")
gsub_file 'app/models/user.rb', /\bend\s*\Z/ do
<<-RUBY
attr_accessible :provider, :uid, :name, :email
end
RUBY
end
end
if recipes.include? 'devise'
# Generate models and routes for a User
generate 'devise user'
if recipes.include? 'authorization'
# create'app/models/ability.rb'
generate 'cancan:ability'
# create 'app/models/role.rb'
# create 'config/initializers/rolify.rb'
# create 'db/migrate/...rolify_create_roles.rb'
# insert 'rolify' method in 'app/models/users.rb'
if recipes.include? 'mongoid'
generate 'rolify:role Role User mongoid'
# correct the generation of rolify 3.1 with mongoid
# the call to `rolify` should be *after* the inclusion of mongoid
# (see https://github.com/EppO/rolify/issues/61)
# This isn't needed for rolify>=3.2.0.beta4, but should cause no harm
gsub_file 'app/models/user.rb',
/^\s*(rolify.*?)$\s*(include Mongoid::Document.*?)$/,
" \\2\n extend Rolify\n \\1\n"
else
generate 'rolify:role Role User'
end
end
# Add a 'name' attribute to the User model
if recipes.include? 'mongoid'
# for mongoid
gsub_file 'app/models/user.rb', /\bend\s*\Z/ do
<<-RUBY
# run 'rake db:mongoid:create_indexes' to create indexes
index :email, :unique => true
field :name
validates_presence_of :name
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
RUBY
end
else
# for ActiveRecord
# Devise created a Users database, we'll modify it
generate 'migration AddNameToUsers name:string'
if recipes.include? 'devise-confirmable'
generate 'migration AddConfirmableToUsers confirmation_token:string confirmed_at:datetime confirmation_sent_at:datetime unconfirmed_email:string'
end
# Devise created a Users model, we'll modify it
gsub_file 'app/models/user.rb', /attr_accessible :email/, 'attr_accessible :name, :email'
inject_into_file 'app/models/user.rb', :before => 'validates_uniqueness_of' do
"validates_presence_of :name\n"
end
gsub_file 'app/models/user.rb', /validates_uniqueness_of :email/, 'validates_uniqueness_of :name, :email'
gsub_file 'app/models/user.rb', /# attr_accessible :title, :body/, ''
end
# needed for both mongoid and ActiveRecord
if recipes.include? 'devise-confirmable'
gsub_file 'app/models/user.rb', /:registerable,/, ":registerable, :confirmable,"
gsub_file 'app/models/user.rb', /:remember_me/, ':remember_me, :confirmed_at'
if recipes.include? 'mongoid'
gsub_file 'app/models/user.rb', /# field :confirmation_token/, "field :confirmation_token"
gsub_file 'app/models/user.rb', /# field :confirmed_at/, "field :confirmed_at"
gsub_file 'app/models/user.rb', /# field :confirmation_sent_at/, "field :confirmation_sent_at"
gsub_file 'app/models/user.rb', /# field :unconfirmed_email/, "field :unconfirmed_email"
end
end
if recipes.include? 'devise-invitable'
if recipes.include? 'mongoid'
gsub_file 'app/models/user.rb', /\bend\s*\Z/ do
<<-RUBY
#invitable
field :invitation_token, :type => String
field :invitation_sent_at, :type => Time
field :invitation_accepted_at, :type => Time
field :invitation_limit, :type => Integer
field :invited_by_id, :type => String
field :invited_by_type, :type => String
end
RUBY
end
end
end
unless recipes.include? 'haml'
# Generate Devise views (unless you are using Haml)
run 'rails generate devise:views'
# Modify Devise views to add 'name'
inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
<<-ERB
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
ERB
end
inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
<<-ERB
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
ERB
end
else
# copy Haml versions of modified Devise views
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/devise-views-haml/app/views/devise/shared/_links.html.haml', 'app/views/devise/shared/_links.html.haml'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/devise-views-haml/app/views/devise/registrations/edit.html.haml', 'app/views/devise/registrations/edit.html.haml'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/devise-views-haml/app/views/devise/registrations/new.html.haml', 'app/views/devise/registrations/new.html.haml'
end
end
end
# >-------------------------------[ HomePage ]--------------------------------<
@current_recipe = "home_page"
@before_configs["home_page"].call if @before_configs["home_page"]
say_recipe 'HomePage'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/home_page.rb
after_bundler do
say_wizard "HomePage recipe running 'after bundler'"
# remove the default home page
remove_file 'public/index.html'
# create a home controller and view
generate(:controller, "home index")
# set up a simple home page (with placeholder content)
if recipes.include? 'haml'
remove_file 'app/views/home/index.html.haml'
# There is Haml code in this script. Changing the indentation is perilous between HAMLs.
# We have to use single-quote-style-heredoc to avoid interpolation.
create_file 'app/views/home/index.html.haml' do
<<-'HAML'
%h3 Home
HAML
end
elsif recipes.include? 'slim'
# skip
else
remove_file 'app/views/home/index.html.erb'
create_file 'app/views/home/index.html.erb' do
<<-ERB
<h3>Home</h3>
ERB
end
end
# set routes
gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
if recipes.include? 'devise'
inject_into_file 'config/routes.rb', :before => " root :to" do
<<-RUBY
authenticated :user do
root :to => 'home#index'
end
\n
RUBY
end
end
end
# >-----------------------------[ HomePageUsers ]-----------------------------<
@current_recipe = "home_page_users"
@before_configs["home_page_users"].call if @before_configs["home_page_users"]
say_recipe 'HomePageUsers'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/home_page_users.rb
after_bundler do
say_wizard "HomePageUsers recipe running 'after bundler'"
# Modify the home controller
gsub_file 'app/controllers/home_controller.rb', /def index/ do
<<-RUBY
def index
@users = User.all
RUBY
end
# Replace the home page
if recipes.include? 'haml'
remove_file 'app/views/home/index.html.haml'
# There is Haml code in this script. Changing the indentation is perilous between HAMLs.
# We have to use single-quote-style-heredoc to avoid interpolation.
create_file 'app/views/home/index.html.haml' do
<<-'HAML'
%h3 Home
- @users.each do |user|
%p User: #{user.name}
HAML
end
else
append_file 'app/views/home/index.html.erb' do <<-ERB
<h3>Home</h3>
<% @users.each do |user| %>
<p>User: <%= user.name %></p>
<% end %>
ERB
end
end
end
# >-----------------------------[ SeedDatabase ]------------------------------<
@current_recipe = "seed_database"
@before_configs["seed_database"].call if @before_configs["seed_database"]
say_recipe 'SeedDatabase'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/seed_database.rb
after_bundler do
say_wizard "SeedDatabase recipe running 'after bundler'"
if recipes.include? 'mongoid'
append_file 'db/seeds.rb' do <<-FILE
puts 'EMPTY THE MONGODB DATABASE'
Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop)
FILE
end
end
if recipes.include? 'devise'
if recipes.include? 'devise-confirmable'
append_file 'db/seeds.rb' do <<-FILE
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => Time.now.utc
puts 'New user created: ' << user.name
user2 = User.create! :name => 'Second User', :email => 'user2@example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => Time.now.utc
puts 'New user created: ' << user2.name
FILE
end
else
append_file 'db/seeds.rb' do <<-FILE
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name
user2 = User.create! :name => 'Second User', :email => 'user2@example.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user2.name
FILE
end
end
if recipes.include? 'authorization'
append_file 'db/seeds.rb' do <<-FILE
user.add_role :admin
FILE
end
end
end
end
after_everything do
if recipes.include? 'devise-invitable'
run 'bundle exec rake db:migrate'
generate 'devise_invitable user'
end
unless recipes.include? 'mongoid'
say_wizard "applying migrations and seeding the database"
run 'bundle exec rake db:migrate'
run 'bundle exec rake db:test:prepare'
else
say_wizard "creating indexes and seeding the database"
run 'rake db:mongoid:create_indexes'
end
run 'bundle exec rake db:seed'
end
# >-------------------------------[ UsersPage ]-------------------------------<
@current_recipe = "users_page"
@before_configs["users_page"].call if @before_configs["users_page"]
say_recipe 'UsersPage'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/users_page.rb
after_bundler do
say_wizard "UsersPage recipe running 'after bundler'"
#----------------------------------------------------------------------------
# Create a users controller
#----------------------------------------------------------------------------
generate(:controller, "users show index")
remove_file 'app/controllers/users_controller.rb'
create_file 'app/controllers/users_controller.rb' do <<-RUBY
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
end
RUBY
end
if recipes.include? 'authorization'
inject_into_file 'app/controllers/users_controller.rb', " authorize! :index, @user, :message => 'Not authorized as an administrator.'\n", :after => "def index\n"
end
if recipes.include? 'will_paginate'
gsub_file 'app/controllers/users_controller.rb', /@users = User.all/, '@users = User.paginate(:page => params[:page])'
end
if recipes.include? 'kaminari'
gsub_file 'app/controllers/users_controller.rb', /@users = User.all/, '@users = User.page params[:page]'
end
#----------------------------------------------------------------------------
# Limit access to the users#index page
#----------------------------------------------------------------------------
if recipes.include? 'authorization'
inject_into_file 'app/models/ability.rb', :after => "def initialize(user)\n" do <<-RUBY
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
end
RUBY
end
end
#----------------------------------------------------------------------------
# Modify the routes
#----------------------------------------------------------------------------
# @devise_for :users@ route must be placed above @resources :users, :only => :show@.
gsub_file 'config/routes.rb', /get \"users\/show\"/, ''
gsub_file 'config/routes.rb', /get \"users\/index\"/, ''
gsub_file 'config/routes.rb', /devise_for :users/ do
<<-RUBY
devise_for :users
resources :users, :only => [:show, :index]
RUBY
end
#----------------------------------------------------------------------------
# Create a users index page
#----------------------------------------------------------------------------
if recipes.include? 'haml'
remove_file 'app/views/users/index.html.haml'
# There is Haml code in this script. Changing the indentation is perilous between HAMLs.
# We have to use single-quote-style-heredoc to avoid interpolation.
create_file 'app/views/users/index.html.haml' do <<-'HAML'
%h2 Users
- @users.each do |user|
%br/
#{link_to user.email, user} signed up #{user.created_at.to_date}
HAML
end
if recipes.include? 'will_paginate'
append_file 'app/views/users/index.html.haml', "\n= will_paginate\n"
end
if recipes.include? 'kaminari'
append_file 'app/views/users/index.html.haml', "\n= paginate @users\n"
end
else
append_file 'app/views/users/index.html.erb' do <<-ERB
<ul class="users">
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %> signed up <%= user.created_at.to_date %>
</li>
<% end %>
</ul>
ERB
end
if recipes.include? 'will_paginate'
append_file 'app/views/users/index.html.erb', "\n<%= will_paginate %>\n"
end
if recipes.include? 'kaminari'
append_file 'app/views/users/index.html.erb', "\n<%= paginate @users %>\n"
end
end
#----------------------------------------------------------------------------
# Create a users show page
#----------------------------------------------------------------------------
if recipes.include? 'haml'
remove_file 'app/views/users/show.html.haml'
# There is Haml code in this script. Changing the indentation is perilous between HAMLs.
# We have to use single-quote-style-heredoc to avoid interpolation.
create_file 'app/views/users/show.html.haml' do <<-'HAML'
%p
User: #{@user.name}
%p
Email: #{@user.email if @user.email}
HAML
end
else
append_file 'app/views/users/show.html.erb' do <<-ERB
<p>User: <%= @user.name %></p>
<p>Email: <%= @user.email if @user.email %></p>
ERB
end
end
#----------------------------------------------------------------------------
# Create a home page containing links to user show pages
# (clobbers code from the home_page_users recipe)
#----------------------------------------------------------------------------
# set up the controller
remove_file 'app/controllers/home_controller.rb'
create_file 'app/controllers/home_controller.rb' do
<<-RUBY
class HomeController < ApplicationController
def index
@users = User.all
end
end
RUBY
end
# modify the home page
if recipes.include? 'haml'
remove_file 'app/views/home/index.html.haml'
# There is Haml code in this script. Changing the indentation is perilous between HAMLs.
# We have to use single-quote-style-heredoc to avoid interpolation.
create_file 'app/views/home/index.html.haml' do
<<-'HAML'
%h3 Home
- @users.each do |user|
%p User: #{link_to user.name, user}
HAML
end
else
remove_file 'app/views/home/index.html.erb'
create_file 'app/views/home/index.html.erb' do <<-ERB
<h3>Home</h3>
<% @users.each do |user| %>
<p>User: <%=link_to user.name, user %></p>
<% end %>
ERB
end
end
end
# >---------------------------------[ html5 ]---------------------------------<
@current_recipe = "html5"
@before_configs["html5"].call if @before_configs["html5"]
say_recipe 'html5'
config = {}
config['css_option'] = multiple_choice("Which front-end framework would you like for HTML5 and CSS?", [["None", "nothing"], ["Zurb Foundation", "foundation"], ["Twitter Bootstrap (less)", "bootstrap_less"], ["Twitter Bootstrap (sass)", "bootstrap_sass"], ["Skeleton", "skeleton"], ["Just normalize CSS for consistent styling", "normalize"]]) if true && true unless config.key?('css_option')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/html5.rb
case config['css_option']
when 'foundation'
# https://github.com/zurb/foundation-rails
gem 'zurb-foundation'
when 'bootstrap_less'
# https://github.com/seyhunak/twitter-bootstrap-rails
# http://railscasts.com/episodes/328-twitter-bootstrap-basics
gem 'twitter-bootstrap-rails', '>= 2.0.3', :group => :assets
# please install gem 'therubyracer' to use Less
gem 'therubyracer', :group => :assets, :platform => :ruby
recipes << 'bootstrap'
recipes << 'jsruntime'
when 'bootstrap_sass'
# https://github.com/thomas-mcdonald/bootstrap-sass
# http://rubysource.com/twitter-bootstrap-less-and-sass-understanding-your-options-for-rails-3-1/
gem 'bootstrap-sass', '>= 2.0.3'
recipes << 'bootstrap'
end
after_bundler do
say_wizard "HTML5 recipe running 'after bundler'"
# add a humans.txt file
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/humans.txt', 'public/humans.txt'
# install a front-end framework for HTML5 and CSS3
remove_file 'app/assets/stylesheets/application.css'
remove_file 'app/views/layouts/application.html.erb'
remove_file 'app/views/layouts/application.html.haml'
unless recipes.include? 'bootstrap'
if recipes.include? 'haml'
# Haml version of a simple application layout
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/simple/views/layouts/application.html.haml', 'app/views/layouts/application.html.haml'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/simple/views/layouts/_messages.html.haml', 'app/views/layouts/_messages.html.haml'
else
# ERB version of a simple application layout
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/simple/views/layouts/application.html.erb', 'app/views/layouts/application.html.erb'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/simple/views/layouts/_messages.html.erb', 'app/views/layouts/_messages.html.erb'
end
# simple css styles
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/simple/assets/stylesheets/application.css.scss', 'app/assets/stylesheets/application.css.scss'
else # for Twitter Bootstrap
if recipes.include? 'haml'
# Haml version of a complex application layout using Twitter Bootstrap
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/twitter-bootstrap/views/layouts/application.html.haml', 'app/views/layouts/application.html.haml'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/twitter-bootstrap/views/layouts/_messages.html.haml', 'app/views/layouts/_messages.html.haml'
else
# ERB version of a complex application layout using Twitter Bootstrap
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/twitter-bootstrap/views/layouts/application.html.erb', 'app/views/layouts/application.html.erb'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/twitter-bootstrap/views/layouts/_messages.html.erb', 'app/views/layouts/_messages.html.erb'
end
# complex css styles using Twitter Bootstrap
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/twitter-bootstrap/assets/stylesheets/application.css.scss', 'app/assets/stylesheets/application.css.scss'
end
# get an appropriate navigation partial
if recipes.include? 'haml'
if recipes.include? 'devise'
if recipes.include? 'authorization'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/devise/authorization/_navigation.html.haml', 'app/views/layouts/_navigation.html.haml'
else
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/devise/_navigation.html.haml', 'app/views/layouts/_navigation.html.haml'
end
elsif recipes.include? 'omniauth'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/omniauth/_navigation.html.haml', 'app/views/layouts/_navigation.html.haml'
elsif recipes.include? 'subdomains'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/subdomains/_navigation.html.haml', 'app/views/layouts/_navigation.html.haml'
else
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/none/_navigation.html.haml', 'app/views/layouts/_navigation.html.haml'
end
else
if recipes.include? 'devise'
if recipes.include? 'authorization'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/devise/authorization/_navigation.html.erb', 'app/views/layouts/_navigation.html.erb'
else
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/devise/_navigation.html.erb', 'app/views/layouts/_navigation.html.erb'
end
elsif recipes.include? 'omniauth'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/omniauth/_navigation.html.erb', 'app/views/layouts/_navigation.html.erb'
elsif recipes.include? 'subdomains'
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/subdomains/_navigation.html.erb', 'app/views/layouts/_navigation.html.erb'
else
get 'https://raw.github.com/RailsApps/rails3-application-templates/master/files/navigation/none/_navigation.html.erb', 'app/views/layouts/_navigation.html.erb'
end
end
if recipes.include? 'haml'
gsub_file 'app/views/layouts/application.html.haml', /App_Name/, "#{app_name.humanize.titleize}"
gsub_file 'app/views/layouts/_navigation.html.haml', /App_Name/, "#{app_name.humanize.titleize}"
else
gsub_file 'app/views/layouts/application.html.erb', /App_Name/, "#{app_name.humanize.titleize}"
gsub_file 'app/views/layouts/_navigation.html.erb', /App_Name/, "#{app_name.humanize.titleize}"
end
case config['css_option']
when 'bootstrap_less'
say_wizard 'installing Twitter Bootstrap HTML5 framework (less)'
generate 'bootstrap:install'
remove_file 'app/assets/stylesheets/application.css' # already created application.css.scss above
insert_into_file 'app/assets/stylesheets/bootstrap_and_overrides.css.less', "body { padding-top: 60px; }\n", :after => "@import \"twitter/bootstrap/bootstrap\";\n"
when 'bootstrap_sass'
say_wizard 'installing Twitter Bootstrap HTML5 framework (sass)'
insert_into_file 'app/assets/javascripts/application.js', "//= require bootstrap\n", :after => "jquery_ujs\n"
create_file 'app/assets/stylesheets/bootstrap_and_overrides.css.scss', <<-RUBY
// Set the correct sprite paths
$iconSpritePath: asset-url('glyphicons-halflings.png', image);
$iconWhiteSpritePath: asset-url('glyphicons-halflings-white.png', image);
@import "bootstrap";
body { padding-top: 60px; }
@import "bootstrap-responsive";
RUBY
when 'foundation'
say_wizard 'installing Zurb Foundation HTML5 framework'
insert_into_file 'app/assets/javascripts/application.js', "//= require foundation\n", :after => "jquery_ujs\n"
insert_into_file 'app/assets/stylesheets/application.css.scss', " *= require foundation\n", :after => "require_self\n"
when 'skeleton'
say_wizard 'installing Skeleton HTML5 framework'
get 'https://raw.github.com/necolas/normalize.css/master/normalize.css', 'app/assets/stylesheets/normalize.css.scss'
get 'https://raw.github.com/dhgamache/Skeleton/master/stylesheets/base.css', 'app/assets/stylesheets/base.css.scss'
get 'https://raw.github.com/dhgamache/Skeleton/master/stylesheets/layout.css', 'app/assets/stylesheets/layout.css.scss'
get 'https://raw.github.com/dhgamache/Skeleton/master/stylesheets/skeleton.css', 'app/assets/stylesheets/skeleton.css.scss'
get 'https://raw.github.com/dhgamache/Skeleton/master/javascripts/tabs.js', 'app/assets/javascripts/tabs.js'
when 'normalize'
say_wizard 'normalizing CSS for consistent styling'
get 'https://raw.github.com/necolas/normalize.css/master/normalize.css', 'app/assets/stylesheets/normalize.css.scss'
when 'nothing'
say_wizard 'no HTML5 front-end framework selected'
end
end
# >------------------------------[ SimpleForm ]-------------------------------<
@current_recipe = "simple_form"
@before_configs["simple_form"].call if @before_configs["simple_form"]
say_recipe 'SimpleForm'
config = {}
config['form_option'] = multiple_choice("Which form gem would you like?", [["None", false], ["simple form", "simple_form"], ["simple form (bootstrap)", "simple_form_bootstrap"]]) if true && true unless config.key?('form_option')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/gmgp/rails_apps_composer/blob/master/recipes/simple_form.rb
case config['form_option']
when 'no'
say_wizard "Form help recipe skipped."
when 'simple_form'
gem 'simple_form'
# for external test
recipes << 'simple_form'
when 'simple_form_bootstrap'
gem 'simple_form'
# for external test
recipes << 'simple_form'
recipes << 'simple_form_bootstrap'
else
say_wizard "Form help recipe skipped."
end
after_bundler do
say_wizard "Simple form recipe running 'after bundler'"
case config['form_option']
when 'simple_form'
generate 'simple_form:install'
when 'simple_form_bootstrap'
if recipes.include? 'bootstrap'
generate 'simple_form:install --bootstrap'
else
say_wizard "Bootstrap not found."
end
end
end
# >--------------------------------[ Cleanup ]--------------------------------<
@current_recipe = "cleanup"
@before_configs["cleanup"].call if @before_configs["cleanup"]
say_recipe 'Cleanup'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/cleanup.rb
after_bundler do
say_wizard "Cleanup recipe running 'after bundler'"
# remove unnecessary files
%w{
README
doc/README_FOR_APP
public/index.html
app/assets/images/rails.png
}.each { |file| remove_file file }
# add placeholder READMEs
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/sample_readme.txt", "README"
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/sample_readme.textile", "README.textile"
gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}"
gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}"
# remove commented lines and multiple blank lines from Gemfile
# thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb
gsub_file 'Gemfile', /#.*\n/, "\n"
gsub_file 'Gemfile', /\n^\s*\n/, "\n"
# remove commented lines and multiple blank lines from config/routes.rb
gsub_file 'config/routes.rb', / #.*\n/, "\n"
gsub_file 'config/routes.rb', /\n^\s*\n/, "\n"
end
# >--------------------------------[ Extras ]---------------------------------<
@current_recipe = "extras"
@before_configs["extras"].call if @before_configs["extras"]
say_recipe 'Extras'
config = {}
config['footnotes'] = yes_wizard?("Would you like to use 'rails-footnotes' (it's SLOW!)?") if true && true unless config.key?('footnotes')
config['ban_spiders'] = yes_wizard?("Would you like to set a robots.txt file to ban spiders?") if true && true unless config.key?('ban_spiders')
config['paginate'] = config['form_option'] = multiple_choice("Paginate gem?", [["Kaminari", "kaminari"], ["Will Paginate", "will_paginate"]]) if true && true unless config.key?('paginate')
config['jsruntime'] = yes_wizard?("Add 'therubyracer' JavaScript runtime (for Linux users without node.js)?") if true && true unless config.key?('jsruntime')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/extras.rb
if config['footnotes']
say_wizard "Adding 'rails-footnotes'"
gem 'rails-footnotes', '>= 3.7', :group => :development
after_bundler do
generate 'rails_footnotes:install'
end
end
if config['ban_spiders']
say_wizard "Banning spiders by modifying 'public/robots.txt'"
after_bundler do
# ban spiders from your site by changing robots.txt
gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
end
end
case config['paginate']
when 'kaminari'
gem 'kaminari'
recipes << 'kaminari'
when 'will_paginate'
recipes << 'will_paginate'
if recipes.include? 'mongoid'
gem 'will_paginate_mongoid'
else
gem 'will_paginate', '>= 3.0.3'
end
end
if config['jsruntime']
say_wizard "Adding 'therubyracer' JavaScript runtime gem"
# maybe it was already added by the html5 recipe for bootstrap_less?
unless recipes.include? 'jsruntime'
gem 'therubyracer', :group => :assets, :platform => :ruby
end
end
# >----------------------------------[ Git ]----------------------------------<
@current_recipe = "git"
@before_configs["git"].call if @before_configs["git"]
say_recipe 'Git'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/git.rb
after_everything do
say_wizard "Git recipe running 'after everything'"
# Git should ignore some files
remove_file '.gitignore'
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/gitignore.txt", ".gitignore"
if recipes.include? 'omniauth'
append_file '.gitignore' do <<-TXT
# keep OmniAuth service provider secrets out of the Git repo
config/initializers/omniauth.rb
TXT
end
end
# Initialize new Git repo
git :init
git :add => '.'
git :commit => "-aqm 'new Rails app generated by Rails Apps Composer gem'"
# Create a git branch
git :checkout => ' -b working_branch'
git :add => '.'
git :commit => "-m 'Initial commit of working_branch'"
git :checkout => 'master'
end
# >----------------------------[ Paperclip ]----------------------------------<
@current_recipe = "paperclip"
@before_configs["paperclip"].call if @before_configs["paperclip"]
say_recipe 'Paperclip'
config = {}
config['paperclip'] = yes_wizard?("Would you like to use Paperclip?") if true && true unless config.key?('paperclip')
@configs[@current_recipe] = config
if config['paperclip']
gem "paperclip", "~> 3.0"
else
recipes.delete('paperclip')
end
# >---------------------------[ Friendly Id ]----------------------------------<
@current_recipe = "friendly_id"
@before_configs["friendly_id"].call if @before_configs["friendly_id"]
say_recipe 'Friendly Id'
config = {}
config['friendly_id'] = yes_wizard?("Would you like to use Friendly Id?") if true && true unless config.key?('friendly_id')
@configs[@current_recipe] = config
if config['friendly_id']
gem "friendly_id", "~> 4.0.1"
else
recipes.delete('friendly_id')
end
# >----------------------------[Thin server ]------------------------------------<
@current_recipe = "thin"
@before_configs["thin"].call if @before_configs["thin"]
say_recipe 'Thin Web Server'
config = {}
config['thin'] = yes_wizard?("Would you like to use Thin as default webserver?") if true && true unless config.key?('thin')
@configs[@current_recipe] = config
if config['thin']
gem "thin", :group => :development
else
recipes.delete('thin')
end
# >------------------------------------------------------------------------------<
@current_recipe = nil
# >-----------------------------[ Run Bundler ]-------------------------------<
say_wizard "Running 'bundle install'. This will take a while."
run 'bundle install'
run 'bundle update'
say_wizard "Running 'after bundler' callbacks."
require 'bundler/setup'
@after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
@current_recipe = nil
say_wizard "Running 'after everything' callbacks."
@after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
@current_recipe = nil
say_wizard "Finished running the rails_apps_composer app template."
say_wizard "Your new Rails app is ready."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment