This is the blog post about few things in Rails command line that I hardly ever use.
Left a comment like
#TODO on a method, but never actually end up changing anything or fixing anything about the method. If this sounds familiar, then
rake notes helps you avoid just that. It will find all the files with comments beginning with
Also, we can add search for specific annotation, for ex: searching for just
FIXME comments would be as simple as firing
Also it lets us search for any custom annotation that we might be using in our code,
rake notes:custom ANNOTATION=ADDTESTCASE
Read more about
rake notes on the guides.
rails console –sandbox
If you wish to test some code, but you also want the data to be reverted back to original after you have tested the code, running
rails console in
sandbox mode lets you do just that.
Also one of the great use case is when I run
rails console on our production server. I don’t have to worry about accidentally modifying or even worse deleting some data.
You can also run
rails c -s to run the console in sandbox mode.
Rails provide us with this rake task to calculate few statistics about our codebase. It tells us about line of code, code coverage, number of methods in controllers, models and average number of lines of code in a method among other things.
If you are wondering how does it come up with this data, continue reading.
The code for all the command line options in rails can be found under
railties gem. So looking at the internals of
railties , the code for calculating statistics in under
Interesting thing about this option is that when calculating statistics for test cases, it only considers
unit tests. So,
rspec is not considered by default. It is in the
rspec-rails gem that it adds the spec directory in the look up path of
code_statistics. Here is the method which does that in rspec-rails.
Also here are couple of things unrelated to the post, but I thought of sharing as I found out when I was reading some rails internals.
Did you know ?
To reload the rails app on
rails c , we all use the
reload! command, so it will load the changes we have made to our code, except for any changes made in file under config folder. In that case, we have to
exit and run
rails c again.
If you notice, upon running
reload! command, a message is printed on the
console “Reloading…”. You can turn off this log by passing
false to the
reload! method. You can run
Now lets look at the code, how this method reload! is actually defined. It is part of the
railties gem. The method is defined in
lib/rails/console/app.rb file. This is what it looks like:
reload! can accept a parameter whose
default value is true. So now we know why passing
false does not print the log.
b.) Commenting block of code in coffeescript
Like in ruby we use
/* */. Similarly in coffeescript, we can put our block of code between
### some code here
Thats it for now. I will continue to share things via such posts or on twitter.