Skip to content

Instantly share code, notes, and snippets.

@cs3b
Created May 2, 2012 11:50
Show Gist options
  • Save cs3b/2576043 to your computer and use it in GitHub Desktop.
Save cs3b/2576043 to your computer and use it in GitHub Desktop.
rails 3.2 postgresql rspec factory-girl kameleon headless jasmine guard state_machine inherited_resources drapper devise cancan state-machine kaminari active_admin haml formtastic iconic
# Selleo Application Boostrap Template
# initial based on https://github.com/RailsApps/rails3-application-templates/raw/master/rails3-bootstrap-devise-cancan-template.rb
#
# In difference from original it's not customizable (at least ont interactive) - and it works with Rails 3.2 and above
# Usage: rails new APP_NAME -d postgresql -m https://raw.github.com/gist/2576043/44e01e55e68828f7d1dd996a5c9159a399249a42/selleo-template-rails-3.2.rb -T
# Information and a tutorial:
# https://github.com/RailsApps/rails3-bootstrap-devise-cancan
# Generated using the rails_apps_composer gem:
# https://github.com/RailsApps/rails_apps_composer/
# 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
initializer 'generators.rb', <<-RUBY
Rails.application.config.generators do |g|
end
RUBY
@recipe = nil
def say_custom(name)
@recipe = name
say "\033[1m\033[36m" + "Running #{name} recipe..."
end
@current_recipe = nil
@configs = {}
@after_blocks = []
def after_bundler(&block)
@after_blocks << [@recipe, block]
end
@after_everything_blocks = []
def after_everything(&block)
@after_everything_blocks << [@recipe, block]
end
# >---------------------------[ 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
# >---------------------------------[ DATABASE CREDENTIALS ]----------------------------------<
say_custom("database credential (postgresql)")
username = ask("username: ")
password = ask("password: ")
postgresql_config = <<-TEXT
development:
adapter: postgresql
encoding: unicode
database: #@app_name
pool: 5
username: #{username}
password: #{password}
test:
adapter: postgresql
encoding: unicode
database: #{@app_name}_test
pool: 5
username: #{username}
password: #{password}
TEXT
remove_file 'config/database.yml'
create_file 'config/database.yml' do
postgresql_config
end
create_file 'config/database.yml.example' do
postgresql_config
end
after_bundler do
run "rake db:create"
run "rake db:migrate"
end
# >---------------------------------[ HAML ]----------------------------------<
say_custom("haml")
@configs["haml"] = {'haml' => true}
gem 'haml', '>= 3.1.4'
gem 'haml-rails', '>= 0.3.4', :group => :development
# >---------------------------------[ RSpec ]---------------------------------<
say_custom("rspec")
@configs["rspec"] = {
"rspec" => true,
"factory_girl" => true,
"machinist" => false
}
gem 'rspec-rails', '>= 2.9.0.rc2', :group => [:development, :test]
gem 'database_cleaner', '>= 0.7.2', :group => :test
gem 'factory_girl_rails', '>= 3.2.0', :group => [:development, :test]
# add a collection of RSpec matchers and Cucumber steps to make testing email easy
gem 'email_spec', '>= 1.2.1', :group => :test
# note: there is no need to specify the RSpec generator in the config/application.rb file
after_bundler do
say_custom "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
say_custom "Removing test folder (not needed for RSpec)"
run 'rm -rf test/'
inject_into_file 'config/application.rb', :after => "Rails::Application\n" do
<<-RUBY
config.generators do |g|
g.view_specs false
g.helper_specs false
g.fixture_replacement :factory_girl
end
RUBY
end
create_file 'spec/support/devise.rb' do
<<-RUBY
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
RUBY
end
end
# >-------------------------------[ Kameleon ]--------------------------------<
say_custom 'Kameleon'
gem 'headless', :group => :test
gem 'kameleon', :git => 'git://github.com/cs3b/kameleon.git', :group => :test
after_bundler do
inject_into_file 'spec/spec_helper.rb', "require 'kameleon/ext/rspec/all'\n", :before => "require 'rspec/rails'\n"
end
# >---------------------------------[ guard ]---------------------------------<
say_custom 'guard'
gem 'guard', '>= 0.6.2', :group => :development
gem 'libnotify', :group => :development
gem 'rb-inotify', :group => :development
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'
guard 'rails', '>= 0.0.3'
guard 'livereload', '>= 0.3.0'
guard 'rspec', '>= 0.4.3'
after_bundler do
run 'guard init'
guards.each do |name|
run "guard init #{name}"
end
end
# >-----------------------------[ ActionMailer ]------------------------------<
say_custom 'ActionMailer'
@configs["action_mailer"] = {'gmail' => true}
# 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
after_bundler do
### modifying environment configuration files for ActionMailer
say_custom "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 false to prevent email from being sent during development
config.action_mailer.perform_deliveries = true
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
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: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
TEXT
say_custom gmail_configuration_text
inject_into_file 'config/environments/development.rb', gmail_configuration_text, :after => 'config.action_mailer.default :charset => "utf-8"'
end
# >--------------------------------[ Devise ]---------------------------------<
say_custom 'Devise'
gem 'devise', '>= 2.1.0.rc'
gem 'devise_invitable', '>= 1.0.1'
gem 'cancan', '>= 1.6.7'
gem 'rolify', '>= 3.1.0'
after_bundler do
say_custom "Devise recipe running 'after bundler'"
# Run the Devise generator
generate 'devise:install'
generate 'devise_invitable:install'
# Prevent logging of password_confirmation
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
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
after_everything do
say_custom "Devise recipe running 'after everything'"
say_custom "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"
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_custom "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
# >--------------------------------[ AddUser ]--------------------------------<
say_custom 'AddUser'
after_bundler do
say_custom "AddUser recipe running 'after bundler'"
# Generate models and routes for a User
generate 'devise user'
generate 'cancan:ability'
generate 'rolify:role Role User'
# for ActiveRecord
# Devise created a Users database, we'll modify it
generate 'migration AddNameToUsers name:string'
generate 'migration AddConfirmableToUsers confirmation_token:string confirmed_at:datetime confirmation_sent_at:datetime unconfirmed_email:string'
# 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'
# needed for both mongoid and ActiveRecord
gsub_file 'app/models/user.rb', /:registerable,/, ":registerable, :confirmable,"
gsub_file 'app/models/user.rb', /:remember_me/, ':remember_me, :confirmed_at'
end
# >-------------------------------[ HomePage ]--------------------------------<
say_custom 'HomePage'
after_bundler do
say_custom "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")
remove_file 'app/views/home/index.html.haml'
create_file 'app/views/home/index.html.haml' do
<<-'HAML'
%h3 Home
HAML
end
# set routes
gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
inject_into_file 'config/routes.rb', :before => " root :to" do
<<-RUBY
authenticated :user do
root :to => 'home#index'
end
\n
RUBY
end
end
# >-----------------------------[ SeedDatabase ]------------------------------<
say_custom 'SeedDatabase'
# 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_custom "SeedDatabase recipe running 'after bundler'"
run 'bundle exec rake db:migrate'
generate 'devise_invitable user'
run 'bundle exec rake db:migrate'
# clone the schema changes to the test database
run 'bundle exec rake db:test:prepare'
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
append_file 'db/seeds.rb' do
<<-FILE
user.add_role :admin
FILE
end
end
after_everything do
say_custom "seeding the database"
run 'bundle exec rake db:seed'
end
# >------------------------------[ Formtastic ]-------------------------------<
say_custom 'Formtastic'
gem 'formtastic', '>= 2.1.0'
after_bundler do
generate "formtastic:install"
run "rm -rf /tmp/devise-formtastic && git clone git://github.com/digineo/devise-formtastic.git /tmp/devise-formtastic"
run "cp -rf /tmp/devise-formtastic/app/views/devise ./app/views/"
end
# >--------------------------------[ Cleanup ]--------------------------------<
say_custom 'Cleanup'
after_bundler do
say_custom "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 ]---------------------------------<
say_custom 'Extras'
say_custom "Adding 'rails-footnotes'"
gem 'rails-footnotes', '>= 3.7', :group => :development
after_bundler do
generate 'rails_footnotes:install'
end
gem 'kaminari'
gem 'therubyracer', :group => :assets, :platform => :ruby
say_custom 'Adding Active Admin'
gem 'activeadmin', :git => 'git://github.com/gregbell/active_admin.git'
after_bundler do
generate "active_admin:install"
run 'bundle exec rake db:migrate'
end
after_everything do
generate "active_admin:resource User"
generate "active_admin:resource Role"
generate "active_admin:resource Ability"
generate "active_admin:resource AdminUser"
end
# >----------------------------------[ Git ]----------------------------------<
say_custom 'Git'
after_everything do
say_custom "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"
# 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
# >-----------------------------[ Run Bundler ]-------------------------------<
say_custom "Running 'bundle install'. This will take a while."
run 'bundle install'
run 'bundle update'
say_custom "Running 'after bundler' callbacks."
require 'bundler/setup'
@after_blocks.each { |b| say_custom(b[0]); b[1].call }
say_custom "Running 'after everything' callbacks."
@after_everything_blocks.each { |b| say_custom(b[0]); b[1].call }
say_custom "Finished running the rails_apps_composer app template."
say_custom "Your new Rails app is ready."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment