Skip to content

Instantly share code, notes, and snippets.

@alvesjtiago
Last active December 10, 2016 22:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvesjtiago/3594a96618e128498c88 to your computer and use it in GitHub Desktop.
Save alvesjtiago/3594a96618e128498c88 to your computer and use it in GitHub Desktop.
Opinionated Basic Rails Setup

Opinionated Basic Rails Setup

This is a basic Rails project setup on a Mac that helps me get up to speed when building a new app.
It is by no means a guide, reference or best practice.

Up and Run

  1. rails new project_name -d postgresql

  2. cd project_name

  3. cp config/database.yml config/example.database.yml

  4. vim config/database.yml

     development:
       adapter: postgresql
       encoding: unicode
       database: project_name_development
       template: template0
       pool: 5
       username: username
       password: userpass
       
     test:
       adapter: postgresql
       encoding: unicode
       database: project_name_test
       template: template0
       pool: 5
       username: username
       password: userpass
       min_messages: warning
    
  5. vim .gitignore

     (...)
     .DS_Store
     /config/database.yml
    
  6. git init

  7. git add -A

  8. git commit -m "Initial setup."

Database

(Start PostgreSQL daemon if not running)

  1. rake db:create

jQuery

(Make jQuery play nice with turbolinks)

  1. vim Gemfile

     (...)
     # jQuery :heart: Turbolinks
     gem 'jquery-turbolinks'
    
  2. bundle install

  3. vim app/assets/javascripts/application.js

     //= require jquery
     //= require jquery.turbolinks
     //= require jquery_ujs
     //
     // ... your other scripts here ...
     //
     //= require turbolinks
    

Unicorn (aka make your app fly)

  1. vim Gemfile

     (...)
     # Unicorn (aka make your app fly)
     gem 'unicorn'
    
  2. bundle install

  3. vim Procfile

     web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
    
  4. vim config/unicorn.rb

     worker_processes 3
     timeout 30
     
     before_fork do |server, worker|
     
       Signal.trap 'TERM' do
         puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
         Process.kill 'QUIT', Process.pid
       end
     
       defined?(ActiveRecord::Base) and
         ActiveRecord::Base.connection.disconnect!
     end
     
     after_fork do |server, worker|
       Signal.trap 'TERM' do
         puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
       end
     
       defined?(ActiveRecord::Base) and
         ActiveRecord::Base.establish_connection
     end
    
  5. foreman start

Authentication

  1. vim Gemfile

     (...)
     # Auth
     gem "devise"
    
  2. bundle install

  3. rails generate devise:install

  4. vim config/environments/development.rb

     	(# config.action_view.raise_on_missing_translations = true)
     	config.action_mailer.default_url_options = { host: 'localhost', port: 5000 }
     (end)
    
  5. rails g controller home index

  6. vim config/routes.rb

     (Rails.application.routes.draw do)
     	root 'home#index'
     	(...)
    
  7. vim config/application.rb

     	(config.active_record.raise_in_transactional_callbacks = true)
     		config.assets.initialize_on_precompile = false
     (end)
    

Bootstrap

  1. vim Gemfile

     (...)
     # Bootstrapping
     gem "therubyracer"
     gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
     gem "twitter-bootstrap-rails"
    
  2. bundle install

  3. rails generate bootstrap:install less

  4. vim app/assets/stylesheets/application.css

      (* file per style scope.)
      *= require bootstrap_and_overrides
      (*= require_tree .)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment