Random things, I think I know

Writing multiline git commits without opening text editor.

Whenever you commit something in git, it opens Vim or nano or whatever editor you have configured. I really don't like opening Vim for writing the commit message and then saving & exiting it requires four more key strokes (Esc + w + q + Enter). It doesn't really sound like a lot, but when you write do a lot of small commits, it gets annoying pretty soon.

Initially I have restricted myself to use the `-m` option to single line commit messages.

git commit -m 'Fixing an obsolete comment.'
 
Recently, I came to the realisation that you can write multiline commit messages just in terminal like this,

git commit -m 'Setting default_url_option for test env.
> 
> * This helps the mailer to generate URL in the test environment'

Note the quotes. You can use either single or double quote, it doesn't matter. You just have to keep the quote un-closed as long as you want to write.

You can amend the commit like

git commit --amend -m 'Setting default_url_option for test env.
>
> * This helps the mailer to generate URL in the test environment.
> * Without this setting you will not be able to test mailers.'

On the other hand, you lose all the goodness of Vim like spell check, line wrap, highlighting long lines etc. So restrict it to really small commits.
Continue Reading

after_commit callbacks not getting triggered in test environment.

You cannot test after_commit callbacks for Rails model, if you are using this RSpec configuration (either in spec_helper or rails_helper) `config.use_transactional_fixtures = true`.

This configuration tells RSpec to run each of your examples within a transaction. In fact, you can see this information in comments, if you have generated the rails_helper file with rspec:install. What this means is, though, there will be no real database COMMIT which normally happens during a database transaction. Thus, the after_commit callbacks will get called.

To circumvent this, you can use database_cleaner gem, which provides various strategies like transaction, truncation or deletion for cleaning your database.
Continue Reading

ENV variables not reflecting in Rails code.

When you are developing in Rails 4, you might have seen that some ENV variables despite being set, will not be available in the program for e.g. rails console or rails server.

For example, you might have some code like,
ActionMailer::Base.smtp_settings = {

....

user_name: ENV['GMAIL_USER_NAME']

password:   ENV['GMAIL_PASSWORD']

....

}
but would have forgot to set the GMAIL_USER_NAME or GMAIL_PASSWORD environment variables. Now you would have tried exiting the rails console and setting the environment. But it still will not be available in the rails console.

This is because Rails 4 comes with Spring, which caches the code in memory and whenever you open a rails console or start the rails server, it will fork a process from the master process.

If you hadn't set the environment variables before the Spring server started running, then the master process will not have the environment variables available in its memory. And the forked child processes will never be able to see them.

To fix this, stop the Spring server using the command `./bin/spring stop`. This will stop the server and the environment variables will be available thereafter.

Note: If you setting some environment variables by typing `export GMAIL_USER_NAME=mygmail_username@gmail.com`, then it will only be available for that particular terminal (tab). Other terminal tabs/windows cannot see them. You should probably use ~/.bashrc or ~/.bash_profile based on your OS and Terminal, to avoid setting up environment variables every time.
Continue Reading

Extract text from image using Google Keep

Let us try grabbing text from an image, a screen shot I took from a web page.


Add that image to a Google Keep note. From options, select Grab image text. It is self explanatory.

The result looks like this,


It is not so accurate, but works well.
Continue Reading