minitest is a great testing library and factory_girl is a handy fixture replace library, especially when working with Rails.

It can get tedious to repeat FactoryGirl. for every invocation of build, create, etcetera. Here is a handy shortcut to help reduce your Carpal Tunnel pain:

1
2
3
4
5
6
7
8
9
# for Test::Unit assertion style
class MiniTest::Rails::ActiveSupport::TestCase
  include FactoryGirl::Syntax::Methods
end

# for Spec expectation style
class MiniTest::Spec
  include FactoryGirl::Syntax::Methods
end

Place this in a file such as test/support/factory_girl.rb.

This allows you to use the core set of syntax methods (build, build_stubbed, create, attributes_for, and their *_list counterparts) without having to call them on FactoryGirl directly:

1
2
3
4
5
6
7
describe Awesome do
  # do this
  subject { create(:awesome) }

  # don't do this
  subject { FactoryGirl.create(:awesome) }
end

The PostgreSQL database keeps getting better with each release. With 9.2 now at beta2, it’s a great time to jump in and get up to speed.

Prerequisites

This guide assumes the following:

  • OS X Mountain Lion
  • The latest XCode
  • XCode Command Line Tools
  • The latest MacPorts

Installation

Make sure MacPorts is up to date, then install the postgresql92-server port:

$ sudo port -v selfupdate
$ sudo port -v install postgresql92-server

Setup the Initial Database

Once the server has been installed, you will need to create the initial database:

$ sudo mkdir -p /opt/local/var/db/postgresql92/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdb
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb'

Starting the Server

Once the initial database has been created, you can start the server.

To run the server manually, execute the following command:

$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/postgres -D /opt/local/var/db/postgresql92/defaultdb'

If you want the server to automatically start at boot time, execute the following command:

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql92-server.plist

Setup your PATH

The executable files for PostgreSQL are in a non-standard location, so you you’ll want to update your PATH to make things easier. Most likely, you’ll want to edit your ~/.bashrc or ~/.zshrc (or similar) profile, though you can set apply the changes for all users system-wide by editing /etc/profile.

Make sure you set the new path before /usr/bin to ensure that you are using the latest versions and not the default apple-supplied tools:

1
export PATH=/opt/local/lib/postgresql92/bin:$PATH

You can verify this works by running which psql and you should see /opt/local/lib/postgresql92/bin/psql as the output.

Create a New User

You will be setup with a postgres user by default, but it is good practice to create a different user account.

To make things easy, create a new database user to match your OS X username:

$ createuser --superuser <my username> -U postgres

You should now be able to create a new database:

$ createdb my_app

Do please consider setting a password for your newly-created user ;)

Configuring a Rails Application

To setup your Rails application with PostgreSQL, you will need to do the following:

  • Add the pg gem to your Gemfile and run the bundle command
Gemfile
1
2
3
4
5
source :rubygems

gem "rails"
gem "pg"
# ...
  • Configure your database.yml to use PostgreSQL:
config/database.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
development:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: myapp_development
  username: <%= ENV["USER"] %>
  password:
  allow_concurrency: true
  pool: 5
  min_messages: warning

test:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: myapp_test
  username: <%= ENV["USER"] %>
  password:
  allow_concurrency: true
  pool: 5
  min_messages: error

Even if you have set the min_messages option, you may still see console output like the following:

WARNING:  there is already a transaction in progress

Edit the file /opt/local/var/db/postgresql92/defaultdb/postgresql.conf and set the following:

client_min_messages = error

Everything should now be running smoothly.

My colleague Kenny Johnston wrote a nice piece about Upgrading to Mountain Lion with an emphasis on using Homebrew. Here are the relevant steps using MacPorts:

XCode:

  • Update to the latest XCode 4.4 from the App Store
  • Install the Command Line Tools

Of course, since it is zero-day, there was an issue fetching the Command Line Tools from the server and I had to install manually from a .pkg file retrieved from the Apple Developer Portal. If you install the CLI tools package manually, you may need to run the following:

sudo xcodebuild -license

That is required to accept the license terms system-wide.

MacPorts

First, you’ll need to update the base MacPorts installation:

sudo port -v selfupdate

If you run in to a compilation error, you may need to edit the file /opt/local/etc/macports/macports.conf and set the developer_dir option to be empty:

sudo vim /opt/local/etc/macports/macports.conf
/opt/local/etc/macports/macports.conf
1
2
3
# Directory containing Xcode Tools (default is to ask xcode-select)
#developer_dir       /Developer
developer_dir

You should now be able to update any outdated ports (grab a beverage while you wait):

sudo port -vu upgrade outdated

GCC 4.2

Now install the apple-gcc42 port to replace the missing GCC 4.2 in Mountain Lion:

sudo port install -vu apple-gcc42

Once that has been installed, you will need to link it to the expected system location:

sudo ln -s /opt/local/bin/gcc-apple-4.2 /usr/bin/gcc-4.2

RVM

Make sure you have the latest RVM version installed:

curl -L https://get.rvm.io | bash -s stable

You should now ensure that you can rebuild your installed Ruby versions:

rvm reinstall 1.9.3
rvm reinstall rbx
...

You may need to reference my earlier tip: Fix Ruby 1.9.x OpenSSL Segfault on OS X

The Internet Defense League is a network of people and sites who use their massive combined reach to defend the open internet and make it better. Because it can sound the alarm quickly to millions of users, people are calling it “a bat-signal for the Internet”.

The Internet Defense League takes the tactic that killed SOPA & PIPA and turns it into a permanent force for defending the internet, and making it better. Think of it like the internet’s Emergency Broadcast System, or its bat signal!

The Internet Blackout was just the beginning. Together, our websites and personal networks can mobilize the planet to defend the internet from bad laws & monopolies. Are you in?

A client of mine was having a hard time remembering the project workflow to keep topic branches updated, so I put together this simple shell function as a workflow reference:

git-sync-master
1
2
3
4
5
6
7
function git-sync-master() {
  git checkout master &&
    git fetch origin &&
    git pull origin master &&
    git checkout $1 &&
    git rebase master
}

As written, that could be added to a ~/.bash_profile or similar location allowing you to run something like:

$ cd myproject
$ git-sync-master mybranch

Taking this one step further, you can “integrate” this command in to git with the following minor modifications:

$ cat > ~/bin/git-sync-master
git checkout master &&
  git fetch origin &&
  git pull origin master &&
  git checkout $1 &&
  git rebase master
^D

$ chmod +x ~/bin/git-sync-master
$ cd myproject
$ git sync-master mybranch

If you have updated Boost to version 1.50.0 via MacPorts, you may have encountered something like the following:

$ port installed | grep mongodb
  mongodb @2.0.6_0 (active)
$ port installed | grep boost
  boost @1.50.0_0 (active)
$ mongo
dyld: Symbol not found: __ZNK5boost15program_options16validation_error4whatEv
  Referenced from: /opt/local/bin/mongo
  Expected in: /opt/local/lib/libboost_program_options-mt.dylib
 in /opt/local/bin/mongo

There are numerous reports on the MacPorts Trac with various ports that are affected. Here is the solution that worked for me:

$ cd /tmp
$ svn co  -r 93341 'http://svn.macports.org/repository/macports/trunk/dports/devel/boost/'
A    boost/files
A    boost/files/patch-boost-foreach.diff
A    boost/files/patch-thread_visibility.diff
A    boost/files/patch-libs-mpi-build-Jamfile.v2.diff
A    boost/files/patch-tools-build-v2-tools-python-2.jam.diff
A    boost/files/patch-tools-build-v2-tools-python.jam.diff
A    boost/files/patch-bootstrap.sh.diff
A    boost/files/patch-tools_build_v2_engine_src_build.jam.diff
A    boost/files/patch-libs-python-src-converter-builtin_converters.cpp
A    boost/files/patch-tools_build_v2_engine_src_build.sh.diff
A    boost/Portfile
Checked out revision 93341.

$ cd boost
$ sudo port install
--->  Computing dependencies for boost
--->  Fetching archive for boost
--->  Attempting to fetch boost-1.49.0_0.darwin_10.x86_64.tbz2 from http://packages.macports.org/boost
--->  Attempting to fetch boost-1.49.0_0.darwin_10.x86_64.tbz2.rmd160 from http://packages.macports.org/boost
--->  Installing boost @1.49.0_0
--->  Deactivating boost @1.50.0_0
--->  Cleaning boost
--->  Activating boost @1.49.0_0
--->  Cleaning boost

$ sudo port activate boost @1.49.0_0
--->  Computing dependencies for boost
--->  Cleaning boost

I recently had to reinstall the mysql gem version 2.7 in to following environment:

  • Ruby 1.8.7
  • OS X 10.7
  • MacPorts MySQL 5

I needed to look up this incantation for the Nth time so I am recording it here:

gem install
1
env ARCHFLAGS="-arch x86_64" gem install mysql -v 2.7 -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

If you install Ruby 1.9.2 or 1.9.3 on OS X, you may run in to the dreaded OpenSSL segfault:

~/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799: [BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

The version of OpenSSL that ships with OS X 10.6 and 10.7 is older and causes this issue.

There are a number of options solve this. Here a few:

MacPorts

$ port selfupdate
$ port install openssl
$ port install libyaml

# RVM
$ rvm install 1.9.3 --with-openssl-dir=/opt/local --with-opt-dir=/opt/local

# rbenv
$ CONFIGURE_OPTS='--with-openssl-dir=/opt/local --with-opt-dir=/opt/local' rbenv install 1.9.3-p194

Homebrew

$ brew update
$ brew install openssl

# RVM
$ rvm install 1.9.3 --with-openssl-dir=`brew --prefix openssl`

# rbenv
$ CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 1.9.3-p194

RVM

$ rvm pkg install openssl
$ rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr

A Rack application running on Heroku can improve performance by using different application servers with different performance characteristics.

Some good choices include Thin (evented I/O), Unicorn (multi-process) and Puma (multi-threaded).

The exact configuration settings will vary by application and how much memory each app takes. A default Rails app with a basic scaffold can easily support 4 unicorn workers or 4 puma threads. A small Sinatra app can support several times that number.

Here are the Procfile settings you can use for each:

Procfile
1
2
3
4
5
6
7
8
# thin
web: bundle exec thin -p $PORT -e $RACK_ENV start

# puma
web: bundle exec puma -t 1:4 -b tcp://0.0.0.0:$PORT

# unicorn
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

Use the New Relic RPM Add-on to monitor your application resources.