Last active
December 14, 2015 10:48
-
-
Save rsepassi/5074391 to your computer and use it in GitHub Desktop.
Simple rails template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
devise = yes?("Devise? ") | |
cancan = yes?("Cancan? ") | |
omniauth = yes?("Omniauth? ") | |
foundation = yes?("Foundation? ") | |
bootstrap = foundation ? false : yes?("Bootstrap? ") | |
ember = yes?("Ember? ") | |
handlebars = ember ? false : yes?("Handlebars? ") | |
underscore = yes?("Underscore? ") | |
homecontroller = yes?("Home Controller with index page? ") | |
github = yes?("GitHub create and push? ") | |
if github | |
heroku = yes?("Deploy to Heroku? ") | |
if heroku | |
newrelic = yes?("New Relic? ") | |
sendgrid = yes?("SendGrid? ") | |
end | |
end | |
# Postgres setup | |
gem 'pg' | |
gsub_file 'Gemfile', /gem 'sqlite3'/, '' | |
database_config = <<-EOF | |
development: | |
adapter: postgresql | |
database: #{app_name}_dev | |
encoding: utf8 | |
pool: 5 | |
timeout: 5000 | |
host: localhost | |
test: | |
adapter: postgresql | |
database: #{app_name}_test | |
encoding: utf8 | |
pool: 5 | |
timeout: 5000 | |
host: localhost | |
production: | |
adapter: postgresql | |
database: #{app_name}_production | |
encoding: utf8 | |
pool: 5 | |
timeout: 5000 | |
host: localhost | |
EOF | |
remove_file "config/database.yml" | |
file "config/database.yml", database_config | |
rake "db:create:all" | |
# Basic Gems | |
gem 'thin' | |
gem 'simple_form' | |
gem 'will_paginate' | |
gem 'rabl' | |
gem 'oj' | |
gem_group :test, :development do | |
gem 'rspec-rails' | |
gem 'guard-rspec' | |
gem 'capybara' | |
gem 'rb-fsevent', :require => false | |
gem 'growl' | |
gem 'factory_girl_rails' | |
end | |
gem_group :development do | |
gem 'better_errors' | |
gem 'binding_of_caller' | |
gem 'meta_request' | |
gem 'pry-rails' | |
gem 'annotate' | |
gem 'letter_opener' | |
end | |
run 'bundle install' | |
# Letter opener config in development | |
mail_config = <<-EOF | |
config.action_mailer.default_url_options = { host: 'localhost:3000' } | |
config.action_mailer.delivery_method = :letter_opener | |
EOF | |
inject_into_file 'config/environments/development.rb', mail_config, | |
after: "config.action_mailer.raise_delivery_errors = false\n" | |
# Devise | |
if devise | |
gem 'devise' | |
run 'bundle install' | |
generate "devise:install" | |
generate "devise:views" | |
generate "devise", "User" | |
gsub_file 'config/initializers/devise.rb', | |
/[email protected]/, | |
"info@#{app_name}.com" | |
end | |
# Cancan | |
if cancan | |
gem 'cancan' | |
run 'bundle install' | |
generate "cancan:ability" | |
end | |
# Omniauth with Facebook | |
if omniauth | |
gem 'omniauth' | |
gem 'omniauth-facebook' | |
run 'bundle install' | |
remove_file 'app/models/user.rb' | |
file 'app/models/user.rb', <<-EOF | |
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, | |
# :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
devise :omniauthable, :omniauth_providers => [:facebook] | |
attr_accessible :email, :password, :password_confirmation, :remember_me, | |
:provider, :uid | |
def self.find_by_facebook_auth(auth) | |
find_by_omniauth(auth) | |
end | |
def self.find_by_omniauth(auth) | |
user = User.find_by_email(auth.info.email) | |
unless user | |
user = User.create(email: auth.info.email, | |
provider: auth.provider, | |
uid: auth.uid, | |
password: Devise.friendly_token[0, 20]) | |
end | |
user | |
end | |
end | |
EOF | |
file 'app/controllers/users/omniauth_callbacks_controller.rb', <<-EOF | |
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def facebook | |
@user = User.find_by_facebook_auth(request.env['omniauth.auth']) | |
if @user.persisted? | |
sign_in @user | |
redirect_to root_path | |
else | |
flash[:notice] = "Something went wrong. Please try signing in again." | |
redirect_to new_user_session_path | |
end | |
end | |
end | |
EOF | |
gsub_file 'config/routes.rb', | |
/devise_for :users/, 'devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }' | |
fb_config = <<-EOF | |
require 'omniauth-facebook' | |
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Rails.env.development? | |
config.omniauth :facebook, "APP_ID", "APP_SECRET", | |
{:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}} | |
EOF | |
inject_into_file 'config/initializers/devise.rb', fb_config, | |
after: "OmniAuth\n" | |
generate('migration AddOmniauthToUser provider:string uid:string') | |
end | |
# Zurb Foundation | |
if foundation | |
gem_group :assets do | |
gem 'zurb-foundation', version: '< 4.0.0' | |
gem 'compass-rails' | |
gem 'foundation-icons-sass-rails' | |
end | |
run 'bundle install' | |
remove_file 'app/views/layouts/application.html.erb' | |
generate('foundation:install') | |
generate('foundation:layout') | |
end | |
# Twitter Bootstrap (sass) | |
if bootstrap | |
gem_group :assets do | |
gem 'bootstrap-sass' | |
end | |
file 'app/assets/stylesheets/custom.css.scss', '@import "bootstrap";' | |
inject_into_file 'app/assets/javascripts/application.js', | |
"//= require bootstrap\n", | |
before: '//= require_tree .' | |
end | |
# Flash notifications for devise | |
if devise | |
flash = <<-EOF | |
<% if notice %> | |
<p class="notice"><%= notice %></p> | |
<% end %> | |
<% if alert %> | |
<p class="alert"><%= alert %></p> | |
<% end %> | |
EOF | |
inject_into_file 'app/views/layouts/application.html.erb', flash, | |
after: "<body>\n" | |
end | |
# Ember | |
if ember | |
gem 'ember-rails' | |
run 'bundle install' | |
generate 'ember:bootstrap' | |
end | |
# Handlebars | |
if handlebars | |
gem_group :assets do | |
gem 'handlebars_assets' | |
end | |
run 'bundle install' | |
end | |
# Underscore.js | |
if underscore | |
get 'https://raw.github.com/documentcloud/underscore/master/underscore.js', | |
'vendor/assets/javascripts/underscore.js' | |
inject_into_file 'app/assets/javascripts/application.js', | |
"//= require underscore\n", | |
before: '//= require_tree .' | |
end | |
# Testing with RSpec | |
run 'rm -rf test/' | |
generate "rspec:install" | |
run 'guard init rspec' | |
inject_into_file 'spec/spec_helper.rb', "require 'capybara/rails'\n", | |
after: "require 'rspec/rails'\n" | |
# Asset precompilation config | |
inject_into_class 'config/application.rb', 'Application', | |
" config.assets.initialize_on_precompile = false\n" | |
# Remove junk Rails files (index & png) | |
run "rm public/index.html" | |
run "rm app/assets/images/rails.png" | |
# Replace README | |
run "rm README.rdoc" | |
file "README.md", <<-EOF | |
#{app_name} | |
#{"=" * app_name.length} | |
TODO: description | |
EOF | |
# Create Static Pages Controller with home action & view | |
if homecontroller | |
generate "controller", "home" | |
inject_into_class 'app/controllers/home_controller.rb', | |
'HomeController', | |
" def index\n end\n" | |
file 'app/views/home/index.html.erb', <<-EOF | |
<h1>#{app_name}</h1> | |
EOF | |
route "root to: 'home#index'" | |
end | |
# Git initial commit | |
git :init | |
git add: '.' | |
git commit: "-m 'Initial commit'" | |
# GitHub create and push | |
if github | |
run "hub create" | |
run "git push -u origin master" | |
# Heroku setup | |
if heroku | |
run 'heroku create' | |
run 'git push heroku master' | |
run 'heroku run rake db:create' | |
run 'heroku run rake db:migrate' | |
# New Relic add-on | |
if newrelic | |
run 'heroku addons:add newrelic:standard' | |
gem 'newrelic_rpm' | |
run 'bundle install' | |
get 'https://raw.github.com/gist/2253296/newrelic.yml', | |
'config/newrelic.yml' | |
git add: '.' | |
git commit: "-m 'Added New Relic'" | |
end | |
# SendGrid add-on | |
if sendgrid | |
run 'heroku addons:add sendgrid:starter' | |
mail_config = <<-EOF | |
config.action_mailer.raise_delivery_errors = true | |
config.action_mailer.default_url_options = { :host => 'APP_NAME_GOES_HERE.herokuapp.com' } | |
config.action_mailer.delivery_method = :smtp | |
config.action_mailer.smtp_settings = { | |
:address => 'smtp.sendgrid.net', | |
:port => '587', | |
:authentication => :plain, | |
:user_name => ENV['SENDGRID_USERNAME'], | |
:password => ENV['SENDGRID_PASSWORD'], | |
:domain => 'heroku.com' | |
} | |
EOF | |
inject_into_file 'config/environments/production.rb', mail_config, | |
after: "# config.action_mailer.raise_delivery_errors = false\n" | |
git add: '.' | |
git commit: "-m 'Added Send Grid'" | |
end | |
run 'git push heroku master' | |
end | |
end | |
# Open in Sublime and browser | |
run "subl ." | |
run "heroku open" if heroku |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment