New Rails app setup
Throughout the book we recommend several tools for developing, testing,
deploying, and monitoring the code quality of your app. In this
section, we pull together in one place a step-by-step list for creating
a new app that takes advantage of all these tools.
This section will only make sense after you have read all the referenced
sections, so use it as a reference and don’t worry if you don’t
understand all the steps now. Many steps are
annotated with the section number(s) from
Engineering Software as a Service
in which the tool or concept
is first introduced.
Set up the app
If you’re doing this as a team, one person should do this
-
Run
rails -v
to ensure you’re running the desired version of Rails. If not, rungem install rails -v
x.x.x with x.x.x set to the version you want; 4.2.9 for example. -
Run
rails new
appname-T
to create the new app.-T
skips creating thetest
subdirectory used by theTest::Unit
testing framework, since we recommend using RSpec instead. -
cd
appname to navigate into your new app’s root directory. From now on, all shell commands should be issued from this directory. -
Edit the
Gemfile
to lock the versions of Ruby and Rails, for example:
# in Gemfile:
ruby '2.2.2' # Ruby version you're running
rails '4.2.1' # Rails version for this app
If you ended up changing the version(s) already present in the
Gemfile, run bundle install --without production
to make sure
you have compatible versions of Rails and other gems.
-
Make sure your app runs by executing
rails server -p $PORT -b $IP
and visiting the app’s root URI. You should see the Rails welcome page. (If not using Cloud9, just userails server
.) -
git init
to set up your app’s root directory as a GitHub repo. (\ref{sec:git}, Screencast \ref{sc:gitbasics})
Connect your app to GitHub, CodeClimate, Travis CI, and Heroku
-
Create a GitHub repo via GitHub’s web interface, and do the initial commit and push of your new app’s repo. (\ref{sec:intro_github})
-
Point CodeClimate at your app’s GitHub repo. (\ref{sec:metrics}) Add a CodeClimate badge to your repo’s
README.md
(``splash page’’) so you can always see the latest CodeClimate results. -
Point your Travis CI account it at your app’s GitHub repo (\ref{sec:ci}). Add a Travis CI badge to
README.md
to see the latest status of running tests. -
Set up a Pivotal Tracker project to track user stories and velocity. (\ref{sec:points})
-
Make the changes necessary to your Gemfile for deploying to production on Heroku. (\ref{sec:Heroku})
# make sure references to sqlite3 gem ONLY appear in dev/test groups
group :development, :test do
gem 'sqlite3'
end
# make sure the following gems are in your production group:
group :production do
gem 'pg' # use PostgreSQL in production (Heroku)
gem 'rails_12factor' # Heroku-specific production settings
end
-
Run
bundle install --without production
if you’ve changed yourGemfile
. Commit the changes toGemfile
andGemfile.lock
. On future changes to the Gemfile, you can just saybundle
with no arguments, since Bundler will remember the option to skip production gems. -
Run
heroku apps:create
appname to create your new app on Heroku -
Run
git push heroku master
to ensure the app deploys correctly.
Set up your testing environment
- Add support in your Gemfile for Cucumber, RSpec, interactive debugging, code coverage, factories, local metric collection (optional), and (if you plan to use JavaScript in your app) Jasmine:
group :development, :test do
gem 'jasmine-rails' # if you plan to use JavaScript/CoffeeScript
end
# setup Cucumber, RSpec, Guard support
group :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'simplecov', :require => false
gem 'cucumber-rails', :require => false
gem 'cucumber-rails-training-wheels' # basic imperative step defs
gem 'database_cleaner' # required by Cucumber
gem 'factory_girl_rails' # if using FactoryGirl
gem 'metric_fu' # collect code metrics
end
(See Section \ref{sec:testing_ajax} for additional gems to support fixtures and AJAX stubbing in your JavaScript tests.)
-
Run
bundle
, since you’ve changed yourGemfile
. Commit the changes toGemfile
andGemfile.lock
. -
If all is well, create the subdirectories and files used by RSpec, Cucumber, Jasmine, and if you’re using them, the basic Cucumber imperative steps:
rails generate rspec:install
rails generate cucumber:install
rails generate cucumber_rails_training_wheels:install
rails generate jasmine_rails:install
bundle exec guard init rspec
- If you’re using SimpleCov, which we recommend, place the following
lines at the top of
spec\slash{
spec_helper.rb} to enable it:
# at TOP of spec/rails_helper.rb:
require 'simplecov'
SimpleCov.start
- If you’re using FactoryGirl to manage factories (\ref{sec:fixtures}), add its setup code:
# For RSpec, create this file as spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# For Cucumber, add at the end of features/support/env.rb:
World(FactoryGirl::Syntax::Methods)
-
git add
and then commit any files created or modified by these steps. -
Ensure Heroku deployment still works:
git push heroku master
Create the first migration
You’re now ready to create and apply the first migration,
(\ref{sec:rails_databases}),
then re-deploy to Heroku and apply the migration in production
(heroku run rake db:migrate
).
Add other useful Gems
Some that we recommend include:
railroady
draws diagrams of your class relationships such as has-many, belongs-to, and so on (\ref{sec:associations})omniauth
adds portable third-party authentication (\ref{sec:authentication})devise
adds user self-signup pages, and optionally works withomniauth