Wednesday, 4 June 2014

Sublime Text3: Keyboard Shortcuts for Mac

New file:


Open file:


Save file:


Close file:


Quit program:


Goto Anything


Goto method:


Goto line:


Goto matching bracket:


Select All:


Expand Selection to Word:


Multiple Selections of same word:



Unselect last Selection of same word:


Expand Selection to Line:


Make batch edits with Multiple Selections:




Find next


Find previous


Find in files


Window commands

Single Plane mode (default)


2 Plane mode


3 Plane mode


4 Plane mode next to each other


4 Plane mode


Switch from one pane to another


Move window from one pane to another


Ruby Test

Switch between code and spec


Run Spec


Run Test Suite


Rerun last selection


Hide test output


Monday, 26 May 2014

Ruby: Create a Gem with TDD

So you want to create a ruby gem? No problem, let's get started by creating a basic Lorem Ipsum generator.

bundle gem lorem

Open up the 'lorem.gemspec' file and fill in the TODO's

Include in the gemspec your development dependencies like this:

s.add_development_dependency 'rspec'

Yes that's right, we want tests

Install dependencies by running:

bundle install

Add the spec directory in the gem root

mkdir spec

Create your rspec config file called '.rspec' in the project root with this content

--format documentation

Create the spec_helper.rb file in this directory with the following content

require 'lorem'

Now write your first test in 'spec/lorem_spec.rb'

require 'spec_helper'
describe Lorem do
  describe '.ipsum' do
    it "should return 'Lorem ipsum dolor sit amet...'" do
      Lorem.ipsum.should include('Lorem ipsum dolor sit amet')

Now run your tests with:


And you should see it fail

Now implement the feature. Edit the lorem.rb file and in the Module place the following code:

def self.ipsum
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis pellentesque sodales. Aliquam ullamcorper diam in metus consequat, ac elementum quam tincidunt. Suspendisse odio nibh, iaculis non turpis eget, bibendum facilisis lacus. Donec ultrices feugiat eros sed cursus. Pellentesque laoreet lacus ac tristique laoreet. Integer feugiat massa quis mi tempus porttitor. Morbi eget mauris arcu. In hac habitasse platea dictumst. Proin id risus sagittis, bibendum sem at, commodo est. Vivamus vestibulum nisi auctor justo fringilla consectetur. Etiam ultricies sapien nibh, ac fermentum dolor ornare malesuada. Quisque commodo purus eu convallis sollicitudin."

Now run your test again:


And the test should pass

Set the version number in your 'version.rb' file

Add to git with

git add .

And commit

git commit -m 'Add .ipsum method'

Now install the gem with:

rake install

This will build the gem and install it locally

You are now ready to use your gem locally

In IRB you can do:

require 'lorem'

Or in rails app include in Gemfile:

gem 'lorem'

When you are ready to share your gem with the world you can upload it to (after creating rubygems account and github account)

rake release

Now make something more useful!

Tuesday, 18 March 2014

Rails: Staging environment on Heroku

A note to myself for everytime I have to do this

Consider you already have a rails app on Heroku but you now want to incorporate a staging environment with your workflow

Local changes first:

Copy the config/environment/production.rb file to config/environment/staging.rb Change the action mailer setup for Exception Notification so you know the email is coming from your staging app.

Add a staging group to your gemfile (this can normally be a copy of the production group)

Change the config/newrelic.yml file to uncomment the app_name under staging

Heroku changes:

From your console in you project directory on your development machine:

heroku apps:create staging-appname

This creates a new heroku app that is also linked to this project.

git remote add staging

This adds the git handle of staging to this app. You can see the different git remotes with:

git remote -v

So when you want to push your development branch to your staging environment you do:

git push staging development:master

You also have to run your migrations (from now on you have to specify the app name):

heroku run rake db:migrate --app staging-appname

Your staging app will be set to production by default so you have to set it to staging environment:

heroku config:set RACK_ENV=staging RAILS_ENV=staging --remote staging

You can now check if it done by doing:

heroku config --app staging-appname

Dont forget to change the default password on Active Admin!

You can now do new feature testing, automated cross browser testing, automated db population and load testing to your hearts content without affecting your real app, its data or its users.

Your staging environment is now complete

When you are happy that the staging app is behaving as intended you can first merge the development branch into master and then delete the development branch:

git checkout master
git merge development
git branch -d development

Now you can push master to heroku (production app)

git push heroku master

Your production app is now updated!

Thursday, 6 June 2013

Rails: TDD Workflow

I try to keep things simple. This is my personal workflow as a single developer. This workflow evolves continually as I grow as a developer and as new tools and practices becomes available. These are my tools: Ruby, Rails, Rspec, Capybara, Git

This is an overview: For production apps I have a development, staging and production environment. As a start to a cycle I only have a master branch. I create a new branch for every feature (named after the feature) and when ready (all tests green) I create the development branch from the feature branch. The development branch pushes to the staging environment. When all tests green on staging I merge with master branch, push to production and delete the feature and development branches.

Here are more detailed steps:

  1. I write out a story for a feature with sketches.
  2. I create a new branch(I try to not code in master branch)
    git checkout -b newfeaturename
  3. I write my Rspec tests to test the feature
  4. I use Spork to pre-load my rails environment (run spork from the console) and a focus tag in the test to only run my new tests.

    In rspec test

    it "can email advertisers with no listings", focus: true do

    Then in console run:

    rspec spec --drb --tag focus

    The --drb option above make use of the spork service allready running

  5. Implement the code to make tests pass.
  6. After each test is green or refactored commit to version control

    git add .

    git commit -am "My commit message"

  7. When all tests are green and the feature is implemented I create the development branch
    git checkout -b development
  8. Push to staging:(I use Heroku)
    git push staging development:master
  9. Do integration tests against staging with saucelabs and a seed database
  10. If all good, merge with master

    git checkout master

    git merge development

  11. Push to git repositry (I use Bitbucket)
    git push bitbucket master
  12. Push to production (I use Heroku)
    git push heroku master
  13. Rinse and repeat with new features

Monday, 3 June 2013

Rails: Custom Rake Tasks

Rake is a great command line utility for rails. Most rails developers will use it for database migrations.

rake db:migrate

You can also see what rake tasks is available to you by running the following command from the command line.

rake -T

This post is about creating custom rake tasks that you can view with the above command and run to do tasks for your rails application that you would not want to do from within the application because it would take too long.

We start with a simple example by creating a file with a demo.rake extension in the lib/tasks/ folder of your rails app with the following code.

task :hello_world do
  puts 'Hello World!'
Now run the command from the command line.
rake hello_world

It should have printed out your puts command. If you run rake -T you will find that it does not list your new task. To do that you have to add a description to your task so that it looks like this.

desc "Prints Hello World!"
task :hello_world do
  puts 'Hello World!'

It will now be in the list when you run rake -T. You saw that a rake task is called by its task name and not file name. You can have multiple tasks in the same rake file with its own desc and task block. You don't wrap the whole file in a block.

I personally use custom rake commands to do db maintenance while offline or to run scripts that collects information from the internet and saves it to database. To have a rake task interact with your rails app you have to load the rails environment and you do that by loading it is a dependency. You add => environment after you task name. Here is an example rake file that resets all the products or users in your db. This saves you having to drop into rails console to methods on models.

desc "Deletes all products in db"
task :reset_products => :environment do

desc "Deletes all users in db"
task :reset_users => :environment do

I use this mainly on my development machine while rails server is not running. Some of my tasks do scraping jobs and can take hours to run. If you ran that from within the app your app would have been frozen waiting for the job to finish. Also be careful to run a rake task that saves to the database while the app is running as it can lock the db (when using webrick).