Rake Tasks In Rails Project

Posted by SibProgrammer on March 07, 2010|Comments

Playing so long with Rails but still don’t familiar with rake, today I found how to get the list of all available tasks:

rake -T

or

rake --tasks

There are a lot of tasks available out of the box in typical Rails project:

rake auth:gen:site_key                   # Generates config/initializers/site_keys.rb
rake db:abort_if_pending_migrations      # Raises an error if there are pending migrations
rake db:charset                          # Retrieves the charset for the current environment's database
rake db:collation                        # Retrieves the collation for the current environment's database
rake db:create                           # Create the database defined in config/database.yml for the current RAILS_ENV
rake db:create:all                       # Create all the local databases defined in config/database.yml
rake db:drop                             # Drops the database for the current RAILS_ENV
rake db:drop:all                         # Drops all the local databases defined in config/database.yml
...

List also contains the tasks specific to the project, i.e. located at lib/tasks directory.

Going through the list of tasks I found very interesting one: rake stats. Here is the output for one of my projects:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |   590 |   479 |      10 |      42 |   4 |     9 |
| Helpers              |   122 |    59 |       0 |       6 |   0 |     7 |
| Models               |   329 |   254 |       6 |      35 |   5 |     5 |
| Libraries            |   275 |   156 |       2 |      30 |  15 |     3 |
| Integration tests    |     0 |     0 |       0 |       0 |   0 |     0 |
| Functional tests     |   199 |   153 |      11 |      19 |   1 |     6 |
| Unit tests           |    40 |    30 |       5 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |  1555 |  1131 |      34 |     132 |   3 |     6 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 948     Test LOC: 183     Code to Test Ratio: 1:0.2