Skip to content

Instantly share code, notes, and snippets.

@tjjjwxzq
Last active May 16, 2016 02:42
Show Gist options
  • Save tjjjwxzq/92d27e643ba823cc2d59de28f3132ca2 to your computer and use it in GitHub Desktop.
Save tjjjwxzq/92d27e643ba823cc2d59de28f3132ca2 to your computer and use it in GitHub Desktop.
Ruby on Rails notes

Rails Notes

Setup

Set up rbenv

$ cd
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc'
$ ~/.rbenv/bin/rbenv init

Follow the instructions on how to initialize rbenv to enable shims and autocompletion.

$ source .bashrc # or restart your shell
$ type rbenv
#=> "rbenv is a function"
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install 2.3.1 # or whatever ruby version

Set a ruby version for your project; create a .ruby-version file at the project root:

echo '2.3.1` > .ruby-version

Install bundler

gem install bundler

Set up PostgreSQL

Ubuntu:

sudo apt-get install postgresql

Follow set-up instructions here

Arch Linux:

sudo pacman -S postgresql

Follow set-up instructions here

Mac OS: Use Postgres.app

Create a rails project with PostgreSQL and no TestUnit

rails new myappname --database=postgresql -T # use rspec instead

Check that config/database.yml is set up correctly for postgres. Ensure that the username and password match your postgresql user. (To check the list of users, log into the postgres shell by psql -U postgres, then run \du in the shell.)

Example config/database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  username: aqi
  password: 

development:
  <<: *default
  database: sample_app_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: sample_app_test

production:
  <<: *default
  database: sample_app_production

To set up the development database:

./bin/rake db:create

Get Slim

Add gem 'slim-rails to your Gemfile and bundle install. Generated templates will now use slim by default.

Get Simple Form

Add gem 'simple_form' to your Gemfile and bundle install.

Get Boostrap

Add gem 'bootstrap-sass' to your Gemfile and bundle install.

RSpec, Capybara, FactoryGirl, and Shoulda Matchers

Add the gems to your Gemfile and bundle install:

group :development, :test do
  gem 'rspec-rails'
end

group :test do
  gem "factory_girl_rails"
  gem "capybara"
  gem "shoulda-matchers"
end

Add the following to spec/rails_helper.rb:

# rails_helper.rb
# Keep the test and development databases in sync
# so there is no need to `rake db:test:prepare`
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# ... default configs ...

  # Make routes available in 
  config.include Rails.application.routes.url_helpers

  # Use Capybara
  config.include Capybara::DSL

  # Use Shoulda-Matchers
  config.include(Shoulda::Matchers::ActiveModel, type: :model)
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)

  # Use FactoryGirl
  config.include FactoryGirl::Syntax::Methods

end

Add the following to spec/spec_helper.rb:

# spec_helper.rb
RSpec.configure do |config|
  # ...
  # Use only the new 'expect' syntax
  config.expect_with :rspec do |c|
    c.syntax = :expect
  end
end

If the project was generated with TestUnit, remove the test directory:

rm -rf test

Then set up rspec:

rails g rspec:install

Create a binstub for the rspec command so you don't have to keep prefixing it with bundle exec:

bundle binstubs rspec-core

To run all specs:

./bin/rspec 

Devise or Sorcery

Devise: Add gem 'devise' to your Gemfile and bundle install

Run the generator:

rails g devise:install

To use Devise helpers in your specs, include the following in spec/rails_helper.rb:

require 'spec_helper'
require 'rspec/rails'
require 'devise'

RSpec.configure do |config|

# Use Devise helpers
  config.include Devise::TestHelpers, type: :controller
  config.include Devise::TestHelpers, type: :view

end

Sorcery: Add gem 'sorcery' to your Gemfile and bundle install

Run the generator:

rails g sorcery:install

To use Sorcery helpers in your specs, include the following in spec/rails_helper.rb:

RSpec.configure do |config|

  # Use Sorcery helpers
  config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
  config.include Sorcery::TestHelpers::Rails::Integration, type: :feature

end

Now go do REAL(s) stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment