This blog post will be one many showing how to create Todo the Rails 3 application. The first post will be creating the Rails application and setting up the testing environment.
The app will be called Just ToDo it, just as famous
Nike slogan. And also Gods of DNS where
good to me so the Domain justtodoit.com
is free so I bought it and the final stage of this
Rails posts will be deployment to VPS and pointing to the domain JustToDoIt
(Currently displays my domain metaintellect).
Create application
The command to create new Rails application and omitting default testing framework Unit::Test
with switch -T
or longer version is --skip-test-unit
1 2 3 4 5 6 7 8 9 |
|
I had capitalized JustToDoIt
before, because the name is used as Ruby class and Pascal case is convention
for Ruby classes.
Then rename folder to just-todo-it
to be more in *nix folder naming convention:
1
|
|
The fun starts when the directory is changed to Rails app directory:
1
|
|
Text editor
I’ll use vim as a default text editor, for TextMate use mate
and for the Sublime Text 2 use
subl
terminal commands for editing files instead of vim
.
Gemfile
First open the Gemfile
, we need to add some gems that will be used in the app and also
for testing:
1
|
|
Edit it to include this gems:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
After changing the Gemfile
run bundler to update and download entered gems:
1
|
|
Main and Production Ruby Gems
- rails - The latest one
3.2.8
for this time of writing. - bcrypt-ruby - Needed for password hashing.
- unicorn - For production, it will run as Rack HTTP Server.
- haml - My favorite View rendering engine.
- thin - Thin as local server instead of default Webrick.
- pg - My default database for production usage.
Test and Development Ruby Gems
- sqlite3 - The database used for development and testing environments.
- rspec-rails - RSpec as default testing framework.
- pry - Using as default Interactive Ruby console instead of
irb
. Needs some configuration to be hooked asrails console
. - factory_girl_rails - The testing factory framework, used instead of the default Fixtures.
- database_cleaner - Used to speed-up tests, in my case to encapsulate the tests into db transaction.
- awesome_print - Used by Pry to pretty prints Ruby objects in full color exposing their internal structure with proper indentation.
- capybara - for simulating the web interaction in the tests.
- guard-rspec - To refresh and run the tests upon saving via rb-fsevent.
- spork - The server to speed up tests, how?, see provided link.
- guard-spork - Refreshes the spork server on changes, so that we don’t need to.
Testing configuration
RSpec
RSpec will be used as the test framework for the Just ToDo it
app.
Run generator to install RSpec to Rails:
1 2 3 4 |
|
Guard
1 2 3 |
|
Configure Guardfile
set the Spork on top and RSpec in bottom:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Spork
Bootstrap the Spork:
1 2 3 4 |
|
RSpec Helper
Edit RSpec helper:
1
|
|
And edit to include this content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
It uses Spork server and the aim is to have most things in prefork
block where is
stuff run on load of Spork.
In each_run
block we want put only necessary things, because it runs each time,
we are now having only reloading of Factory Girl factories, but maybe we will add something
from prefork
if we would have some troubles with testing data.
DatabaseCleaner is used to start, on before and clean it, on after running. The strategy used for DatabaseCleaner is transaction, meaning to rollback changes after the transaction queries are finished.
Run Guard
The testing environment is now configured, the Guard can be run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
or use bundle exec guard
to remove displayed warning.
To exit or stop the guard
command use Ctrl+C
.
Pry as Rails Console
And for the end we will set Pry as our default Interactive Ruby console.
Open the development.rb
:
1
|
|
At the end of source file add code:
1 2 3 4 |
|
Try it out with .pwd
and close the Pry with exit
command:
1 2 3 4 5 |
|
Commit Source
First remove the README.rdoc
file and create markdown README.md
:
1 2 |
|
Add simple description:
1
|
|
Initialize
Initialize the git repository:
1 2 |
|
Status
See the status:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Add
Then add all files:
1 2 3 4 5 6 7 8 9 10 |
|
Commit
Commit the files to local repository:
1 2 3 4 5 6 |
|
Set Github Remote
The app will be on Github. So after the new repository is created on Github, we can add remote to the local repository:
1
|
|
Push to the Github
After we add remote, it is now safe to push changes to Github remote repository:
1 2 3 4 5 6 7 8 9 |
|
Conclusion
It this first part we have created a JustToDoIt
Rails application.
And because we shall use TDD (Test Driven Development) to drive this app, we first configure the testing environment including:
- RSpec
- Factory Girl
- Database Cleaner
- Guard
- Spork
And for the end we setup the Pry to be a default for Rails console and commit the source to the Github repository xajler/just-todo-it.
In second post we shall go with the creating the app logic in TDD way!
Code
The code is hosted on GitHub and can be cloned from the xajler/just-todo-it.
Github xajler/just-todo-it commit for this post:
Initial Commit. Created initial Rails app, added all needed Gems, testing configured