Wednesday, November 30, 2011

Exec: I need this feature for the next release in 6 months.
Dev Team: It will take a year.
Exec: If you can't deliver it in 6 months I'll find someone who can.
Dev Team: OK, we'll do it.

Outcome A: The project is late.
Outcome B: The project is delivered on time, but the Dev Team spends
the next year fixing bugs in it, and can't deliver any new features.

Ouch! Unfortunately, this does happen...
"Tom DeMarco: All Late Projects Are the Same, in IEEE software"

Wednesday, November 23, 2011

Backup database before deployment using Capistrano

It is always good to take backup of the database before any deployments which include db migrations, but is often forgotten.

A simple capistrano recipe can automate this for you

namespace :mysql do
  desc "performs a backup (using mysqldump) in app shared dir"
  task :backup, :roles => :db, :only => { :primary => true } do
    filename = "#{application}.db_backup.#{}.sql.bz2"
    filepath = "#{shared_path}/#{filename}"
    text = capture "cat #{deploy_to}/current/config/database.yml"
    yaml = YAML::load(text)

    on_rollback { run "rm #{filepath}" }
    run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filepath}" do |ch, stream, out|
      ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/



 before :deploy, 'mysql:backup' 
This recipe is reading database configurations from the deployed application, so no need to put the db configs inside the recipe.

A simple generic solution!

At 36zeroes we produce amazing applications. If you need an experienced Rails team for your project, Contact us.

Saturday, October 8, 2011

Google Font API brings typography to your application

Its been over a year since Google launched their fonts API and now, its beginning to look really good. Their collection is simply amazing and features hundreds of quality open source fonts.

Using Google fonts:
  1. Head over to Google WebFont application
  2. Pick the fonts you want to use for your 'about-to-look-awesome' application or website. 
  3. Review your collection and choose which style(s) and character set(s) you want for the fonts you have picked
  4. Copy the generated CSS link tag into your application layout
  5. Modify the application CSS to use the new fonts, and we're done!
The generated css link tag looks something like this:

link href="|Open+Sans:600italic,600&subset=latin,vietnamese" rel="stylesheet" type="text/css"

You can easily read this tag is specifying two fonts, with a script subsets of Latin and Vietnamese and specifying 600italic and 600 style for the Open Sans font.

With this rather simple task, Google is doing all the magic (complicated work) working out the correct font files depending on user browser.
The Google Font API hides a lot of complexity behind the scenes. Google’s serving infrastructure takes care of converting the font into a format compatible with any modern browser (including Internet Explorer 6 and up), sends just the styles and weights you select, and the font files and CSS are tuned and optimized for web serving. For example, cache headers are set to maximize the likelihood that the fonts will be served from the browser’s cache with no need for a network roundtrip, even when the same font is linked from different websites. -- From Google Code Blog
Why is the text styled with web fonts so awesome?

  • It looks great!
  • Search engine friendly
  • Screen reader friendly
  • Fonts are faster to render and put less pressure on your network. With Google API, it puts no pressure on your network!

So make sure you use web-fonts for your next project instead of putting styled text into images.

At 36zeroes we produce amazing applications. If you need an experienced Rails team for your project, Contact us.

Thursday, August 11, 2011

Git (no branch) lost my changes on checkout

Branching and merging is the fun part of Git repositories. But, because it is so quick and efficient at merging, you often forget about all the complicated operations, it is doing behind the scenes.

If you are rebasing your codebase while merging (which I do all the time to get a nice & clean timeline), you can often land your self at a 'detached head' , which means that your repository is referencing a commit (using its unique SHA1) and not a branch. At this point in time git will respond with '(no branch)' when asked for current branch name.

While on a no-branch, if you happen to checkout another branch, like I did this morning, you will discover (soon enough) that all your changes are lost! But are they really?

git reflog to your rescue. This command shows the log of your git repository operations. It will simply list out all the commits you have done in recent past with their SHA1 ids.
fdf246b HEAD@{0}: checkout: moving from 4c8f4d86690ca7e7e3b0c9e8ea91abfde2eae265 to product_searching
4c8f4d8 HEAD@{1}: commit (amend):
372ad3e HEAD@{2}: commit: ..........................
fd747eb HEAD@{3}: commit: ..........................
0326d13 HEAD@{4}: commit: ..........................
7ca63e8 HEAD@{5}: commit: ..........................
Just find out the last commit you did while you were on no-branch (unknowingly sometimes) and merge it into the branch of your choice.

git merge 4c8f4d8

Presto! you have brought your lost commits back into a known branch!

Note: This will not work very well if you have performed 'git gc'

Alternative: 'git fsck' can be used to find out the dangling objects in repo with '--lost-found' option and then use merge operation using the SHA1 id of missing commits.

git fsck --lost-found

Good luck hunting the lost commits!

At 36zeroes we produce amazing applications. If you need an experienced Rails team for your project, Contact us.

Monday, June 20, 2011

Giving gem options to bundler install.

As I work more with ArchLinux...  I discovered bundler config options.

For gherkin it seems I need to do this:
gem install gherkin -- --with-cflags="-std=c99"

Yet, in bundler it's not obvious how to pass these along.  You can't do it in the Gemfile -- and you might not want to since another machine could have a different environment.

To pass these flags you create a configuration for the build, which is stored in your machines config only.

From Bundler Gemfile docs:


You can use bundle config to give bundler the flags to pass to the gem installer every time bundler tries to install a particular gem.
A very common example, the mysql gem, requires Snow Leopard users to pass configuration flags to gem install to specify where to find themysql_config executable.
gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Since the specific location of that executable can change from machine to machine, you can specify these flags on a per-machine basis.
bundle config build.mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config
After running this command, every time bundler needs to install themysql gem, it will pass along the flags you specified.

First spotted here:

Guard = Cmd Line File Watcher

Guard is a command line file watcher. 

It watches files you specify in a Guardfile, and runs various actions when they are modified.  Best of all, it's very easy to add your own actions.

  1. Install gem 'guard'
  2. Install gems for file-watching / notification(OS specific)
  3. Create a Guardfile
  4. Run 'guard' in a terminal and wait...

To configure your guard-watch, you create a Guardfile.  A skeleton of this file is auto-generated for you using this command:

$ guard init Guardfile
The commands in the Guardfile consist of three main parts:
  1. Guard
    • Defines a 'guard'... which is a set of files + action to take on change
    • Give it a block
    • Inside that block, define your watchers
  2. Watch
    • Which files to watch
    • Use globs and regexp
  3. Group
    • Define groups of guards
    • Useful if you want to trigger different groups of guards in different terminals, or only run some
Example of running just guards in group 'Infra'
$ guard --group Infra

Guard Actions
Guard takes care of the file matching, and watching, then you use plugins to pull in actions to take.  Some examples of these include:

Example: Bundler
Install 'guard-bundler' gem...
Then, auto run 'bundler' when the Gemfile changes

Example: Annotate
Install gem :guard-annotate
Then, run 'annotate' task when schema changes

Example: SASS
Install gem :guard-sass

Signal handlers
Signal handlers are used to interact with Guard:
    Ctrl-C - Calls each guard's :stop method in order then quits Guard itself
    Ctrl-\ - Calls each guard's run_all method in order
    Ctrl-Z - Calls each guard's :reload method in order

You can read more about configuring the signal keyboard shortcuts on the github wiki, and home page:

Checkout the Available Guards including:
  • guard-annotate by Craig P Jolicoeur
  • guard-bundler by Yann Lugrin
  • guard-chef by James OKelly
  • guard-coffeescript by Michael Kessler
  • guard-compass by Olivier Amblet
  • guard-cucumber by Michael Kessler
  • guard-delayed by David Parry
  • guard-ego by Fabio Kuhn
  • guard-gimli by Fredrik Wallgren
  • guard-jammit by Pelle Braendgaard
  • guard-haml by Immanuel Häussermann
  • guard-jasmine-headless-webkit by John Bintz
  • guard-jstd by arailsdemo
  • guard-krl by Michael Farmer
  • guard-mozrepl by Phil Hofmann
  • guard-less by Brendan Erwin
  • guard-livereload by Thibaud Guillaume-Gentil
  • guard-markdown by Darren Wallace
  • guard-minitest by Yann Lugrin
  • guard-nanoc by Yann Lugrin
  • guard-passenger by Fabio Kuhn
  • guard-phantomjs by Klaus Hartl
  • guard-pow by Thibaud Guillaume-Gentil
  • guard-prove by Marian Schubert
  • guard-pusher by Klaus Hartl
  • guard-rails by John Bintz
  • guard-rspec by Thibaud Guillaume-Gentil
  • guard-shell by Joshua Hawxwell
  • guard-soca by Luke Amdor
  • guard-spork by Thibaud Guillaume-Gentil
  • guard-sprockets by Aaron Cruz
  • guard-stendhal by Josep Mª Bach
  • guard-test by Rémy Coutable
  • guard-uglify by Aaron Cruz
  • guard-webrick by Fletcher Nichol