There are a number of rake tools in a rails project a lot of people just don't know about. Here are a few of them.
Every one knows about rake routes right? Well you can limit the results with a couple of
methods. First limit by controllers:
% rake routes CONTROLLER=posts
But I usually resort to just piping to grep. Why? Because the 'term' can be a controller, action, path/url name, even an HTTP verb like 'DELETE'. I find this very effective when I am looking through someone elses work, finding a rarely used path, or jogging my memory on a complex url helper name.
% rake routes | grep term
The following simply prepares the database for your tests. If you have test failing after you changed the database, and they are not failing in development, this is usually the task that needs to be run.
% rake db:test:prepare
Want a nice list of all the migrations with an up/down for which ones have and have not been run? This is nice after a pull and when your are running migrations up and down before a 'larger than it should be commit.' (I never do that...) None the less, it gives a nice high level persepective.
% rake db:migrate:status
database: project_development
Status Migration ID Migration Name
--------------------------------------------------
up 20111210105537 Create organizations
up 20111213020956 Create positions
up 20111213162045 Remove role from position
up 20111213165355 Create roles
down 20111214031957 Add permissions to roles
I was afraid of this one for a long time. It was silly in hind site. All rake db:reset
does is truncate all of the tables, re-run all of your migrations, and run rake db:seeds.
If you want to clean out some crappy data in your development database and get it back to
your starting poing (You use your seeds.rb file right?) then this is the way to go. It is
great for experimenting. Go ahead try something new, risky, or plain stupid and you can get
right back to where yoy were. No fuss.
% rake db:reset
Besides being interesting to look at, I love to watch the 'Code to Test Ratio:' on the bottom.
If your ratio is about 1:1 you are probably only testing the 'happy path', which is better than
nothing, but in reality there are a lot more things that can go wrong than go right.... I have
found (thus far) that a ratio of about 1:2 or 1:3 is usually where I perfer to sit. I have the
highest velocity (forward momentum) on the project here. I loose a bit of speed if I go above 3,
because I am writing too many tests, but that is far preferrable (and sometimes neccessary
depending on the complexity of the code) than the alternative. If I drop below, I greatly loose
speed because I have have to manually test things and refactoring becomes painful.
% rake stats
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 132 | 103 | 8 | 13 | 1 | 5 |
| Helpers | 27 | 20 | 0 | 2 | 0 | 8 |
| Models | 431 | 275 | 13 | 25 | 1 | 9 |
| Libraries | 0 | 0 | 0 | 0 | 0 | 0 |
| Integration tests | 34 | 24 | 1 | 1 | 1 | 22 |
| Functional tests | 378 | 320 | 7 | 8 | 1 | 38 |
| Unit tests | 533 | 417 | 19 | 10 | 0 | 39 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total | 1535 | 1159 | 48 | 59 | 1 | 17 |
+----------------------+-------+-------+---------+---------+-----+-------+
Code LOC: 398 Test LOC: 761 Code to Test Ratio: 1:1.9
In conclusion, the last task to know about is: rake -T. You can list all of the
described tasks with just the -T, but you can also add a term on to the end and
search though the available tasks.
% rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake auth:used_privileges # Lists all privileges used in controllers, views, models
...
% rake -T vlad
rake vlad:app:configure # Write and symlink unicorn config file.
rake vlad:app:start # Alias for vlad:start_app
rake vlad:app:symlink # Symlink unicorn config file.
...
I hope this brief look at the rake tools available in a Rails project has been helpful, and encourages you to find other helpful tasks.
→ Written by Matt Smith on January 17, 2012 in Rake, Rails