Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Gems

heartsentwined edited this page · 13 revisions

Gems


CHANGES This page has been updated for the 9.x branch. Legacy instructions are gone - clone this wiki repo and checkout the 8.x tag.


Modify Gemfile:

source 'https://rubygems.org'

ruby '2.0.0' # or 1.9.3

gem 'rails', '~> 4.0'
gem 'pg', '~> 0.14'
gem 'unicorn', '~> 4.6'

gem 'jquery-rails', '~> 3.0'
gem 'coffee-rails', '~> 4.0'
gem 'sass-rails', '~> 4.0'
gem 'uglifier', '~> 2.1'

gem 'devise', '>= 3.0', '< 3.1' # server-side authentication
                                # 3.1 removes token auth
gem 'bcrypt-ruby', '~> 3.0' # password encryption

gem 'active_model_serializers', '~> 0.7' # json that conforms to ember-data expectation

gem 'ember-rails', '~> 0.13' # ember framework
gem 'ember-data-source', '>= 1.0.0.beta.3', '< 2.0' # ember-data not yet out of beta
gem 'emblem-rails', '~> 0.1' # easier to write templates

gem 'ember-auth-rails', '~> 5.0' # client-side authentication
gem 'ember-auth-request-jquery-rails', '~> 1.0' # auth requests via jQuery.ajax
gem 'ember-auth-response-json-rails', '~> 1.0' # responses in json
gem 'ember-auth-strategy-token-rails', '~> 1.0' # token authentication
gem 'ember-auth-session-cookie-rails', '~> 1.0' # use cookies
gem 'ember-auth-module-ember_data-rails', '~> 1.0' # ember-data integration

group :test do
  gem 'rspec-rails', '~> 2.13' # test framework
  gem 'spork', '>= 1.0.0rc3', '< 2.0' # speedier tests
  gem 'guard-rspec', '~> 3.0' # watch app files and auto-re-run tests
  gem 'guard-spork', '~> 1.5' # spork integration
  gem 'database_cleaner', '~> 1.0' # cleanup database in tests
  gem 'fabrication', '~> 2.6' # model stubber
  gem 'shoulda', '~> 3.3' # model spec tester
  gem 'rb-inotify', require: false  # Linux file notification
  gem 'rb-fsevent', require: false  # OSX file notification
  gem 'rb-fchange', require: false  # Windows file notification
end

group :production do
  gem 'rails_12factor', '~> 0.0' # tweaks for heroku
  gem 'newrelic_rpm', '~> 3.5' # prevent heroku from idling
end

Install gems.

$ bundle update

RSpec, Spork, Guard

$ rails g rspec:install
$ spork --bootstrap
$ guard init rspec spork

Make RSpec use Spork. Optional: use a nice documentation format. Add to .rspec:

--drb
--format documentation # optional

Edit spec/spec_helper.rb. Just moving everything into the Spork.prefork block, except the initial require statements, of course.

Replace Guardfile contents with this:

require 'active_support/core_ext'

guard :rspec, all_after_pass: false, cli: '--drb' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { 'spec' }

  # Rails
  watch(%r{^app/(.+)\.rb$})           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }

  watch(%r{^app/controllers/(.+)_controller\.rb$}) do |m|
    ["spec/routing/#{m[1]}_routing_spec.rb",
    "spec/controllers/#{m[1]}_controller_spec.rb",
    "spec/acceptance/#{m[1]}_spec.rb",
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
                      "spec/requests/#{m[1].singularize}_pages_spec.rb")]
  end
  watch(%r{^app/views/(.+)/}) do |m|
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
                      "spec/requests/#{m[1].singularize}_pages_spec.rb")
  end

  watch(%r{^spec/support/(.+)\.rb$})                 { 'spec' }
  watch('config/routes.rb')                          { 'spec/routing' }
  watch('app/controllers/application_controller.rb') { 'spec/controllers' }

  # Capybara
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) do |m|
    "spec/requests/#{m[1]}_spec.rb"
  end
end

guard 'spork', rspec_env: { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch('config/environments/test.rb')
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
end

Add database_cleaner to RSpec:

spec/spec_helper.rb:

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end
end

Devise

$ rails g devise:install

Edit config/initializers/devise.rb. Required settings:

  config.skip_session_storage = [:http_auth, :token_auth]
  config.token_authentication_key = :auth_token
  config.navigational_formats = [:html]

In a production app, set the mailer email address too:

  config.mailer_sender = "(your email)"

BCrypt

Speed up tests by lowering security - add to config/environments/test.rb:

  # Speed up tests by lowering BCrypt's cost function.
  require 'bcrypt'
  silence_warnings do
    BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
  end

Ember

In config/environments, add to development.rb and test.rb:

config.ember.variant = :development

... and to production.rb:

config.ember.variant = :production

Unicorn and foreman

Apart from some enhancements, the goal is to mirror the heroku environment on the local dev setup.

Create config/unicorn.rb:

worker_processes 3 # get more out of your free heroku hours
timeout 30
preload_app true

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info 'Disconnected from ActiveRecord'
  end

  sleep 1
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info 'Connected to ActiveRecord'
  end
end

Create Procfile:

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

Remove turbolinks reference

We have removed the default turbolinks gem, so we'll remove references to its files too in the application layout.

Remove the "data-turbolinks-track" => true options.

app/views/layouts/application.html.erb:

  <% ... %>
  <%= stylesheet_link_tag    "application", media: "all" %>
  <%= javascript_include_tag "application" %>
  <% ... %>

Also remove it from the assets. Remove this line from app/assets/javascripts/application.js:

//= require turbolinks

CSRF change for API

Change rails' CSRF protection to :null_session - we will be using rails' controllers all for providing API end points.

app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
end

ember-auth gems note

The ~> version constraint operator controls only the versions of the rails integration gems, not the adapters/modules themselves. In a production app, you would want to use (semantic) versioning on the source gems.

Example on the jquery request adapter:

gem 'ember-auth-request-jquery-rails', '~> 1.0'
gem 'ember-auth-request-jquery-source', '~> 1.0' # semver here

For simplicity, we will leave them out of this demo-tutorial.

Continue to Devise.

Something went wrong with that request. Please try again.