Rails command line options I hardly ever use.

This is the blog post about few things in Rails command line that I hardly ever use.

rake notes

Left a comment like #FIXME or #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 OPTIMIZE, FIXME, OPTIMIZE.

Screen Shot 2016-05-29 at 12.49.47 pm

Also, we can add search for specific annotation, for ex: searching for just FIXME comments would be as simple as firing rake notes:fixme.

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.

rake stats

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.

Screen Shot 2016-06-06 at 11.49.35 pm

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 code_statistics.rb & code_statistics_calculator.rb .

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.

yuk

Did you know ?

a.) reload!

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.

Screen Shot 2016-05-30 at 2.07.40 am

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 reload! false.

Screen Shot 2016-05-30 at 2.10.10 am

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:

Screen Shot 2016-05-30 at 2.14.10 am

The method 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 =begin &  =end to comment any block of code, also in javascript 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.

Advertisements

One thought on “Rails command line options I hardly ever use.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s