Wednesday 4 June 2014

Sublime Text3: Keyboard Shortcuts for Mac

New file:

Cmd+N

Open file:

Cmd+O

Save file:

Cmd+S

Close file:

Cmd+W

Quit program:

Cmd+Q

Goto Anything

Cmd+P

Goto method:

Cmd+R

Goto line:

Ctrl+G

Goto matching bracket:

Ctrl+M

Select All:

Cmd+A

Expand Selection to Word:

Cmd+D

Multiple Selections of same word:

Cmd+D

again

Unselect last Selection of same word:

Cmd+U

Expand Selection to Line:

Cmd+L

Make batch edits with Multiple Selections:

Shift+Cmd+L

Find

Cmd+F

Find next

Cmd+G

Find previous

Shift+Cmd+G

Find in files

Shift+Cmd+F

Window commands

Single Plane mode (default)

Alt+Cmd+2

2 Plane mode

Alt+Cmd+2

3 Plane mode

Alt+Cmd+3

4 Plane mode next to each other

Alt+Cmd+4

4 Plane mode

Alt+Cmd+5

Switch from one pane to another

Ctrl+number_of_pane

Move window from one pane to another

Ctrl+Shift+number_of_pane

Ruby Test

Switch between code and spec

Cmd+.

Run Spec

Shift+Cmd+R

Run Test Suite

Shift+Cmd+T

Rerun last selection

Shift+Cmd+E

Hide test output

ESC

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

--color
--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')
    end
  end
end

Now run your tests with:

rspec

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."
end

Now run your test again:

rspec

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'
Lorem.ipsum

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 rubygems.org (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 git@heroku.com:staging-appname

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!