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.


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.


ActiveSupport – Array

In this blog post, I will write about some of the magical methods or tricks that ActiveSupport#Array provides us.

The first method which blew my mind was ‘in_groups_of’. This method is used to split the array in groups of a number. Ex:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10", nil, nil]

This is pretty straight forward. The cool part is we can pass a parameter to fill the empty places with. By default it is nil. For ex:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, 'my parameter') {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10", 'my parameter', 'my parameter']

And this is not it. It goes one step further. If we do this:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false) {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]

Surprised…!! It does not fill with any value (nil or false) when false is passed as the second parameter. This is pretty neat..!! You can read about it or if you want to see the source code for this method, read here( rails/active_support/lib/active_support/core_ext/array/grouping.rb)

Next up is ‘to_sentence’. to_sentence is used to convert the elements of an array to a sentence using word connectors. For ex:

['one', 'two'].to_sentence
# => "one and two"   

We can even pass the connector for the words. Ex:

['one', 'two'].to_sentence(two_words_connector: '-')
# => "one-two"
['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
# => "one or two or at least three"

And we can even pass which language to use for connections. ex:

['uno', 'dos'].to_sentence(locale: :es)

You can read more about it here, from the source code:

These are pretty neat methods which ActiveSupport provides us. There are more methods which I could have mentioned, but if you want to find out more such methods the best way is to read the rails source code.