Using both Pow and Apache on OSX Lion

I have written previously about using Pow for Ruby/Rails development. Pow is great if you are purely developing in Ruby, however I’ve recently found myself needing to edit a WordPress site.

Pow intercepts requests to port 80 by default meaning that no requests can make it through to the default Apache installation on OSX. I’ve found the best way around this is to disable Pow whenever developing in PHP. The powder gem handles this nicely, so:


gem install powder

Now you can now disable Pow with the following, therefore letting requests through to Apache (assuming you have it enabled):


powder down

And once you’re done with the PHP development, enable it again with:


powder up

As a side note, I usually leave Apache disabled by default i.e. uncheck “Web Sharing” within the “Sharing” section of System Preferences until I am going to be doing some PHP development.

Hope this saves someone some time figuring out how to disable/enable Pow.

Loving MongoDB but Missing Transactions

I’ve been venturing into the world of MongoDB via Mongoid in a Rails app. On one hand it’s a breath of fresh air (no migrations, flexible schemas etc.) but on the other hand I really, really miss transactions.

If somebody asked me if I used transactions much in MySQL, I would probably have said no… however, now that I don’t have them I realise just how much I used them. Things that just work when using ActiveRecord such as creating a record and ensuring that an associated record gets created is sooo much harder.

MongoDB has “Atomic Operations” which means if you have an embedded relationship, all is good and the database will be rolled back if the entire document does not save. As soon as you have two top level collections and need to ensure that a record is created in both, things get a little hairy.

There seems to be a great deal of “if you need transactions just use a RDBMS” talk but what if you need a flexible schema and transactions, the last thing you want to manage is some kind of EAV system like Magento.

I’ve created a thread on the Mongo User forum with a typical example so feel free to join in the discussion. I’d love to hear experienced Mongo user’s views on how to overcome these kinds of situations and what kind of applications people are running with MongoDB.

Naming your Terminal tabs in OSX Lion

If you find yourself in the Terminal app with a bunch of tabs open, the default name of “bash” isn’t very useful when navigating between them. You can change the tab name via the UI by right clicking the tab, then clicking “Inspect Tab” and changing the window or tab names but this is somewhat long winded.

Below are a couple of bash functions I have in my “.profile” file to make this easier:


function tabname {
  printf "\e]1;$1\a"
}

function winname {
  printf "\e]2;$1\a"
}

Now you can easily name your tabs or windows with the following:


# Rename tab
tabname "Funky Tab"

# Rename window
winname "Funky Window"

Thanks to Bubu and Chris Page on the SuperUser site for the right codes.

Using Pow when developing Rails applications on OSX

I heard about Pow a while ago which is “a zero-config Rack server for Mac OS X” but never got around to giving it a go. I’ve just installed it and can instantly see that it is going to make developing with Rails (or any Rack app) a whole lot nicer, especially when dealing with apps that use subdomain account keys.

If you’ve not tried it yet, head over to the Pow website and get it installed with:

curl get.pow.cx | sh

Happy new year everyone!

Pulling changes into a Github fork from the original repository

If you maintain your own ongoing fork of a project on Github, you will inevitably want to pull in changes from the originator’s repository. Here’s how I usually go about it.

First add the other guy’s repository to your list of remotes:


cd my-fork
git remote add other-guy https://github.com/other-guy/other-guys-repo.git 

If you were to then list your remotes, you would have something like:


origin
other-guy 

Now it’s just a case of pulling from the relevant branch on their repo, in this case ‘master':


git pull other-guy master

Hope that saves someone some time.

Writing Javascript that runs in multiple environments (with a one-liner)

With the introduction of server-side javascript, it’s nice to be able to utilise any Javascript libraries you may write in both the browser and on the server. I’ve seen a few different approaches to this but wanted a one-liner that I could just stick at the top of an anonymous function… this is what I came up with:


(function() {
  var MyLib = (typeof exports !== 'undefined') ? exports : this.MyLib = {};

  // MyLib.myFunction = ...
}());

Now in NodeJS you can do:


var MyLib = require('./mylib.js');

This also passes JS Lint using the following:


/*jslint */
/*global exports */
(function () {
    "use strict";
    var MyLib = (typeof exports !== 'undefined') ? exports : this.MyLib = {};

    MyLib.foo = 'bar';
}());

On Open Sourcing Your Competitive Advantage

It’s been a little quiet around here, so I thought I’d share something that’s been on mind for a while.

At what point in the lifecycle of a commercial project is it a good idea to Open Source?

Take Shopify for instance, they open sourced their payment integration code early on in the form of ActiveMerchant. This was undoubtably a wise move, as they benefitted from an entire Ruby community testing their code in the wild and contributing back in the form of new payment gateways.

I don’t know exactly when it was in the development of Shopify that ActiveMerchant was released but at what point are you open sourcing your competitive advantage. Don’t get me wrong, I love open source, I am just trying to determine when is a good time to open source and when are you just giving away your competitive advantage. For instance, if ActiveMerchant had been released 2 years before the release of Shopify, anyone could have used it in the development of a Shopify’esque product… but would anyone have even thought of using it in that way? Who knows?

Bite sized chunks

I feel it is a good idea to open source little and often. Obviously if Shopify had open sourced their entire shopping cart, subscription code and templating solution as a whole, you would have seen Shopify’s popping up all over the interweb and their business would likely have suffered. Instead they opted to open source their payment gateway code (ActiveMerchant) and templating library (Liquid) separately as self-contained projects.

A key ingredient

There comes a point when you simply should not open source certain code, this is when it contains the key ingredient to a commercial project. Do you think Coca-Cola would have been as big as they are today if they had released the complete recipe, including the key ingredient for their revolutionary new black fizzy pop? It’s all about the byproduct, for example if Coca-Cola had released the secret to their new screw on lid (I’m sure they didn’t invent screw on lids but bear with me), they would have got their name out there through recognition of their fancy lid, the pop community may have helped refine it’s design and coke may have stayed fizzy for longer… that’s one hell of a contrived example but hopefully you get my point. It doesn’t matter that every other pop out there will also stay fizzy longer, they’re not Coke.

I’d love to hear peoples examples of commercial projects where code has been open sourced. What result did it have on the project? Was it worthwhile or did it have a detrimental effect on your business?

Using git add -A

In my day-to-day Git workflow I often find myself doing the the following before a commit:


git add .
git add -u
git status

The first line adds any new or modified file contents to the Git index and the second uses the “update” flag which marks any deleted files as deleted in the Git index. I then proceed with a commit and all my changes are committed.

I find myself repeating this before every commit and so had a browse of the documentation to see if there was a simpler way. I found the “-A” or “–all” flag which I had never noticed before (is this new?). Now my commits are usually a two step process:


git add -A
git commit -m "Committing all changed and deleted files"

Hope someone finds this useful.