Archive | January 2011

Use FactoryGirl steps in Cucumber features

I came across something VERY useful recently: factory_girl‘s built in cucumber steps. That’s right, factory_girl comes with some built in cucumber steps that can be used in your cucumber features.

Just add the following to your cucumber env.rb file:

require "factory_girl/step_definitions"

Then, assuming you have the actual factory definitions in the first place, you can use all kinds of loveliness, including the use of associations!

For more detailed information, check out Brandon Keepers’ Practical Cucumber series.

Readline Error With RVM and Rails 3 on Ubuntu

I’ve been setting up my clean development machine using Rails 3 and RVM on Ubuntu 10.04. All very nice and squeaky clean until I run rails console and the whole thing barfs!

After trying a few things (even following the help on the RVM site!) I found this gem (sorry) of a post by Jason Meridth:

http://blog.jasonmeridth.com/2010/11/25/readline-error-with-rvm-and-rails-3.html

Works a charm.

FreeTDS tweak for truncated responses

I use the FreeTDS drivers so that my Rails app can talk to SqlServer. I’ve been running into strange problems where I’m expecting a large XML response from a service to be saved in my database. Although the save operation completes successfully, the XML response is truncated.

The reason for this can be found in the /etc/freetds/freetds.conf file. If you simply appended your own DSN details to the default config file, you will see the following near the top of the file:

..
..
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
..
..

The important line is the text size. This setting imposes a size limit on database text fields the driver sends and receives between your database and your app. I’m getting XML responses far bigger than 64K so simply commenting out that line solves my truncation issue.

Make you config file look like this:

..
..
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
# text size = 64512
..
..

Simples.

Setting up Ubuntu 10.04 for Rails development

Every time a new version of Ubuntu is released use it as an opportunity to rebuild my development environment. It keeps things clean and fresh, and because I develop exclusively in virtual machines it’s easy and fast to do if you don’t have to do too much searching around.

Here’s what I want in my development environment on Ubuntu:

  • RVM
  • Apache and Passenger
  • MySql and SQLite
  • RubyMine development IDE
  • Optional: Install UnixODBC and FreeTDS for SqlServer connectivity

Fairly standard setup, so let’s get started…

RVM

Before I install RVM, I always install Ruby 1.8.7. I’ve run into strange problems when I’ve installed RVM without first installing Ruby. I also like having a system Ruby installed. Typically this will be 1.8.7 simply because this is the version of Ruby that’s in the Ubuntu repositories.

sudo apt-get install irb libopenssl-ruby libreadline-ruby rdoc ri ruby ruby1.8 ruby-dev rake libopenssl-ruby git-core gitk git-gui libxml2 libxml2-dev libxslt1-dev libncurses5-dev libreadline5-dev

Then install RVM (see the RVM website for the latest instructions).

If RVM doesn’t already have Rubygems installed, here’s the manual recipe::

cd /usr/local/src
sudo wget http://production.cf.rubygems.org/rubygems/rubygems-1.4.1.tgz
sudo tar xzvf rubygems-1.4.1.tgz
cd rubygems-1.4.1
sudo ruby setup.rb
sudo update-alternatives --install /usr/bin/gem gem /usr/bin/gem1.8 1
sudo gem update --system

Before we move on, a couple of quick tweaks to two files in your home folder. First, add the following to the .gemrc file:

---
:backtrace: false
:benchmark: false
:bulk_threshold: 1000
:sources:
- http://rubygems.org/
- http://gems.github.com
:update_sources: true
:verbose: true
gem: --no-ri --no-rdoc

Then add this to the .gitconfig file:

[user]
     name = [your name]
     email = [your email]
[color]
     ui = auto

Apache and Passenger

A basic installation of Apache and Passenger is easy:

sudo aptitude update
sudo aptitude install apache2
sudo gem install passenger
sudo passenger-install-apache2-module

The final command will check your system for dependencies. In my case the following command will sort that out:

sudo apt-get install build-essential
sudo apt-get install libcurl4-openssl-dev libssl-dev zlib1g-dev apache2-prefork-dev libapr1-dev libaprutil1-dev

Rerun the passenger install command and it should succeed after all dependencies have been satisfied.

MySQL and SQLite

MySQL is essential, but I usually install SQLite too, because it’s simple and comes in handy sometimes.

sudo apt-get install libsqlite3-dev sqlite3 sqlite3-doc
sudo gem install sqlite3-ruby
sudo apt-get install libmysqlclient-dev mysql-server emma
sudo gem install mysql

RubyMine development IDE

For RubyMine to run we need to install a java runtime. I’ve found the Sun JRE to be best, and this is how we get it:

sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts

Once that’s done, do a quick test to make sure everything is ok…

java -version

Now download and extract the RubyMine package and create a menu link to start RubyMine. You will need your license key when it starts.

Optional: Install UnixODBC and FreeTDS for SqlServer connectivity

This is optional because unless you need to connect to Microsoft SqlServer (I do for work!) you won’t need to do this. I’ve split these into separate commands because the sequence is very important – RUN EACH COMMAND SEPARATELY!

sudo apt-get install unixodbc
sudo apt-get install unixodbc-dev
sudo apt-get install freetds-dev
sudo apt-get install freetds-bin
sudo apt-get install tdsodbc
sudo apt-get install libiodbc2

Install the Ruby ODBC binaries (we’ve found that this specific version works best for us):

cd /usr/local/src
sudo wget http://www.ch-werner.de/rubyodbc/ruby-odbc-0.9997.tar.gz
sudo tar zvxf ruby-odbc-0.9997.tar.gz
cd ruby-odbc-0.9997
sudo ruby extconf.rb --disable_dlopen
make
make install

Test the connection in irb:

require 'rubygems'
require 'dbi'
dbh = DBI.connect('dbi:ODBC:[dsn_name]', [username], [password])

You will need to manually configure the FreeTDS config files to get this working, but this is easily found on the web.

Follow

Get every new post delivered to your Inbox.