My Scribble Pad

Random things, I think I know

RSpec `let` gotcha! Plus assignment

Recently I tried something like
```ruby
let(:array_of_numbers) { [1, 2, 3, 4, 5] }

it 'should have a description' do
  expect([1, 2, 3, 4, 5, 6]).to eql(array_of_numbers += [6])
end
```
It did not work, as in the plus assignment statement ruby expects `array_of_numbers` to be a variable not a method call.
Continue Reading

Run OpenVPN in background.

**TL;DR** Run it in daemon mode: `openvpn --config Windscribe-Japan.ovpn --daemon`

Passing the configuration (.ovpn) filename to `openvpn` command works only if no other options are specified. If I specify `--daemon` option then openvpn tries to parse the filename as an options parameter and throws **Options error: I'm trying to parse "Windscribe.ovpn" as an --option parameter but I don't see a leading '--'**.

**Answer:**

To avoid this, the file name has to be specified with `--config` option. For example, `openvpn --config Windscribe.ovpn --daemon`. Then tail the syslog with `tail -f /var/log/syslog`, for further inspection.

You can also check the before and after output of this curl command `curl ifconfig.co` to make sure that VPN is connected.

Note: This will keep the daemon running even after you logout from the SSH session.
Continue Reading

Removing recent network playlist in VLC media player.

If you have used VLC to stream videos online, then you might have noticed that there is no way for you to clear the recently played URLs from the dropdown list. This list will persist even if you clear the recent media by Media -> Open Recent Media -> Clear.

It took me 2 hours to find it, by digging through the source code. That is the beauty of open source.

The solution, on Linux, VLC stores the list of recently played files in path, /home/<USER>/.config/vlc/vlc-qt-interface.conf where <USER> is the name of your user name.

Open the vlc-qt-interface.conf file and search for netMRL, there will be comma separated list of URLs assigned to the netMRL variable. Remove everything after the equal sign till the end of line. Or you can remove particular URLs from the CSV.

Similary, you will also find the recentMRL in the same file. You can edit that as well.
Continue Reading

Configure sudo to set $HOME to the home directory of the target user.

According to the documentation if the Defaults        env_reset is set, the $HOME will be set to the home directory of the target user. On the surface it might look straight forward, but if you read carefully, you might notice that the contents of env_keep and env_check also affects this behavior.

The problem is, even if you don't see env_keep configured in your sudoers file, there is a default list that comes when you install sudo. To check the default env_keep variables, login as root sudo su and run sudo -V. Under the section Environment variables to preserve: you will see the list of environment variables that get preserved when you run a sudo command.


So, even though the $HOME is set to the home directory of the target user initially, it gets over-written by these default env_keep variables.

To avoid this, you can configure the Defaults with always_set_home. So setting Defaults        always_set_home will set the $HOME to home directory of the target user despite the list of env_keep variables.
Continue Reading

Replacing old style ruby hashes in Vim. (Updated)

You can use the following command in Vim, in normal mode to replace old style Ruby hashes into new style ruby hashes.


%s/:\(\w\+\)\(\s\==>\)/\1:/gc

Update, a slightly better substitution command, which adds a space after `key:` regardless of the source string.
%s/\V:\(\w\+\)\(\s\*\)\(=>\)\(\s\*\)/\1: /gc
 


Continue Reading

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