Trying out Spin as an alternative to Spork

Even after applying the suggested changes to have Simplecov work nicely with Spork, I wasn’t fully satisfied because coverage report was still not accurate 100% of the time.So I tried Spin to see how it worked and it has been great.

The advantage over Spork is that the application does not need to be modified. Just install the Spin gem, launch the spin server (spin serve –push-results), and in other terminal launch tests when needed (spin push rspec).

The first caveat: no cukes

No cucumber support out of the box. I found a fork by Ricky Robinson that offers cucumber support and works just great. It also enables by default unit/rspec/cucumber testing, so you no longer have to switch processes to execute each type of tests. You can read more on his blog post.

The second caveat: no merge coverage results

I think this is due to the fact that instead of launching directly rspec (or cucumber) you are launching spin and this tags the results as “spin”. Each time spin push XX is launched, “spin” coverage results are overwritten.  I extrapolated from this issue that I could try the same approach and it works just fine:

# at the top of spec/spec_helper.rb
require 'simplecov'
SimpleCov.start 'rails' do
  command_name 'rspec'
end

 

# at the top of features/support/env.rb
require 'simplecov'
SimpleCov.start 'rails' do
  command_name 'cukes'
end

Rspec, SimpleCov and Spork friendship

I have been using simplecov to do simple coverage reports of my apps. It is a beautiful gem as it merges results from your various test suites to give you the big picture (rspec + cucumber tests in my case).
If you run it like rake spec is all good. But if you are also using Spork to speed up your Red-Green-Refactor cicle, Rspec tests won’t show.

Put this code in your test/helpers and you are good to go (thanks to the original poster):

Spork.prefork do
  unless ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end

  # other code ...
end

Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end

  # other code ...
end

Enforcing Uniqueness Validation

When working with rails and ActiveRecord, the “:unique” validation is not enough because it does not check uniqueness at a database level.
To ensure that a field is unique (say user’s email), follow this steps:

1. Create a databse index

This could be optional if you are not using this field a lot, but in a case where a look up is going to repeat a lot it is advisable.

rails generate migration add_index_to_users_email

Then edit the generated migration file:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

And execute the migration:

bundle exec rake db:migrate
2. Make sure case isn’t interfering

We are going to include a callback to the creation of a user to convert the email to lower case before saving it to the database (in the user model file):

before_save { |user| user.email = email.downcase }

PD: this has been extracted from the amazing Ruby on Rails tutorial as a note to self. Go check it out, it is amazing!

Using regular expressions in Ruby

From time to time you will find yourself in need of coming up with a suitable regular expression to find some patterns in text or to validate some input. The most straightforward example is email validation.

We are in luck now thanks to the amazing Rubular web page! It’s usage is pretty simple and you also have a cheatsheet at the bottom, which is quite handy!

Free cookie!

I am using this nice regex to validate email for my User model, as found in the Ruby on Rails Tutorial:

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }

Rubular in action, as seen in RoR tutorial

Using Ruby Pik on Windows

When you start developing in ruby you will soon enough find yourself juggling around with different versions of ruby. This could be a pain, but with the aid of amazing addons like rvm it is easily manageable:

In Windows systems we can’t use rvm, but pik has come to the rescue. Pik is able to do a lot of cool tricks, but its basic use is to manage different Ruby versions.

Installation

Installation is really simple. Follow the instructions here: https://github.com/vertiginous/pik. It is recommended to install it outside of Ruby, so I chose \bins:

gem install pik
pik_install \bins

Adding rubies

pik install ruby 1.9.3
pik install ruby 1.8.7

Usage

You can see which version is currently being used with any of these:

where ruby
pik list

Then you could select the default version:

pik default 193

And to select a specific Ruby version, the appropriate command is use:

pik use 187

Free cookie!

If you find yourself using “pik use xxx” a lot, a great thing to do is create a batch file, including pik selection before calling the program. For instance, to launch Vim with ruby 187:

@echo off
rem -- Run Vim --
call pik use 187
start /wait vim
call pik default

Cheers!

Command-T installation on windows

I have been trying VIM as an editor for software development in Windows and it has been a bit troublesome. If you have never tried, I suggest you do. You will probably need 1 day to configure all to your tasting and understand how to move around; and then 2-3 days to catch up to your previous performance. By the end of the week, having tasted all the candies from the plugins, you will never look back!

Finally, I’ve got it all put together (yay!). This is my 2 cents about how to get command-t plugin working. The trick is that the version of ruby to install the plugin and the version of ruby supported by Vim have to be the same. (Here I am explaining for ruby187, but you can use ruby 197 if you use a version of Vim which targets that same version – read more here https://wincent.com/issues/1647)

  1. Download ruby installer 1.8x: http://rubyinstaller.org/downloads/ (I will assume it will install to \ruby187)
  2. Download the Ruby Dev Kit (again from http://rubyinstaller.org/downloads/ ) and extract it to \ruby187
  3. Get a Vim with ruby support. Type “vim –version” in a shell and look for ruby. If you have +ruby, skip this step. If you have -ruby, you need to download a version with ruby support (http://sourceforge.net/projects/cream/files/Vim/)
  4. Download command-t: http://www.vim.org/scripts/script.php?script_id=3025
  5. Open it with vim and type “:O %” to extract it.
  6. Now we compile the plugin. Execute \ruby187\msys.bat then:
    cd <yourVimInstall>\ruby\command-t
    ruby extconf.rb
    make install

We are done!

Take a tour about command-t in action:

If you still have issues, check this or this.

If you have multiple versions of ruby in your system and using Pik, you could/should modify \Windows\vim.bat and \Windows\gvim.bat to use the desired ruby version like this:

call pik use 187
-> original content here <-
call pik default

Hello world!

Hi there and welcome!

Here I will talk about my experiences learning Ruby and its surroundings. I plan to use Windows and try to overcome its integration with the ruby world (primarily mac).

Let’s see how it goes!