One year with Rails

One year since I picked up ‘Agile Web Development with Rails’ and coded my first Rails model/controller/view: seems so long ago now.

Things I’ve mastered in the past year:

  • Test driven development using unit and functional tests.
  • Master-detail forms using Ajax to add and delete rows, fetching product details in JSON for codes entered into a line item.
  • Prototype and jQuery, including jQuery UI datepickers.
  • Loading and parsing XML data files using Nokogiri.
  • Building complex reports using nested views in PostgreSQL, maintained using rails_sql_views.
  • Creating formatted, multi-tab Excel spreadsheets using Spreadsheet gem.
  • Creating pixel-perfect PDF reports using JasperReports loaded using Java-Ruby bridge.
  • Hosting using Apache with Passenger, Mongrel, Thin and Unicorn.
  • Monitoring using ScoutApp.
  • Upgrading a production app from Rails 2.3.4 to Rails 3.0.9.
  • Switching from ERB to HAML.

Installing Nokogiri on Ubuntu

Worked for me with Nokogiri 1.4.4 on Ubuntu 10.04 LTS:

# ruby developer packages
sudo apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8
sudo apt-get install libreadline-ruby1.8 libruby1.8 libopenssl-ruby 

# nokogiri requirements
sudo apt-get install libxslt-dev libxml2-dev
sudo gem install nokogiri

Getting started with Ruby on Windows

To get started my preference is to use RubyInstaller for Windows.  Both 1.8.x and 1.9.x variants are available. I group together my Ruby variants under \Programs\rubies.

Having installed multiple variants you can register and manage them using Pik.  I recommend not changing the installation path from the default C:\pik.  It is in fact possible to install Ruby using Pik (by default Pik installs rubies in %HOMEPATH%\.pik\rubies), but I prefer not to do so.

pik add d:\programs\rubies\ruby-1.8.7-p334\bin
pik add d:\programs\rubies\ruby-1.9.2-p180\bin

To access code repositories you will need to download and install msysGit for Windows.  I also strongly recommend TortoiseGit instead of Git GUI.

To build native extension gems like Nokogiri you will need DevKit.  I tried

pik package devkit install

but this did not work for me (got HTTP 404) so I had to manually download and extract the DevKit.  DevKit also includes MSYS/MinGW.

For an IDE I use Aptana Studio 3.  Studio looks for an existing Git installation or installs its own private copy of portable Git.  Studio then leverages the msysGit bash shell.

RubyInstaller includes gem, which can then be used to install bundler to complete the setup of the environment.

RJB on Ubuntu LTS 10.04

Ruby-Java Bridge (RJB) allows use of Java libraries from within a c-ruby environment, see this slideshow.   RJB provides an alternative when a full port to JRuby is impractical.

First install Java and build-essential

apt-get update
apt-get install openjdk-6-jdk
apt-get install build-essential

Now install rjb gem

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
gem install rjb

The bad news is that RJB currently crashes when used with Passenger so the best alternative is to revert to Mongrel.

Rails Datepickers

There are multiple ways to add date-pickers to a Rails application. Searching for a date picker online I came across one Prototype based gem called calendar_date_select that was featured by Ruby Inside, and another one called Calendar-DatePicker-for-Rails.  On closer inspection, however, calendar_date_select was last updated in 2008 and is looking for a new maintainer: the demo still uses Rails 2.2.2: it does not work with some later versions.

Apart from using a date-picker specifically packaged for Rails it is not that difficult to use a free-standing Javascript solution: I found a nice looking solution called unobtrusive-date-picker but it appears to have limited support. There are also Javascript widget libraries like AUI and jQuery UI.  Finally I came across a Railscast that mentions calendar_date_select but goes on to recommends jQuery UI.  jQuery can be integrated with Rails 2.x using jRails or Rails 3 with jquery-rails and/or jquery-ujs or you can simply forgo the integration and just use jQuery directly. In my case I linked to the libraries hosted by Google like this:

 <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" %>
 <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/humanity/jquery-ui.css" %>

The remaining details of using a jQuery UI datepicker with Rails are covered in the Railscast.

Using Ruby with Postgres on Ubuntu 10.04 LTS

Installation of PostgreSQL on Ubuntu is straightforward, see http://library.linode.com/databases/postgresql/ubuntu-10.04-lucid

To create a simple database with its own user use the command line:

appenv=myapptst
psql <<EOF
  create user ${appenv} createdb password '*****';
EOF
createdb --username=postgres --owner ${appenv} ${appenv}

To access the database from Rails use pg

gem install pg

Entries in database.yml should look like

development:
  adapter: postgresql
  username: myappstst
  password: mypwd
  database: myapptst

Ruby, Python & ODBC

I recently tried extracting data from an Access database using Ruby and ODBC.  The first challenge is that Windows ODBC drivers can be either 32 or 64-bit.  On a 64-bit host only the 64-bit configuration panel is present in the control panel, and the 32-bit panel must be accessed directly by invoking C:WindowsSysWOW64odbcad32.exe.  More seriously, however, the ruby-odbc home page only lists Win32 binaries compiled using Microsoft Visual C, but the popular RubyInstaller for Windows distribution is compiled with MinGW making the two effectively incompatible.

Contrast this with Python which has pyodbc downloads compatible with the canonical distribution from python.org, even if trailing the latest release.  For enterprise applications there is even a commercial ODBC driver supported by egenix.

Both Ruby and Python also have DBI interfaces, but Python DBI has been deprecated, and for Ruby the DBD-ODBC implementation relies on the same underlying ruby-odbc package.  For now it looks like I’ll be sticking with Python for ODBC.

sqlunit & dbUnit

Now that I have my database schema creation and maintenance automated with Incanto and dbMaintain I have been looking at testing tools for stored procedures. Perhaps the best known for Oracle PL/SQL are Feuerstein’s utPLSQL and subsequent Quest Code Tester for Oracle. Other options include plunit, PLUTO and ruby-pl-spec. My main requirement, however, has been to invoke tests from an Ant project continuously integrated using Hudson and for now I have started using sqlunit, which is both mature and portable across different databases, as well as allowing relatively simple testcases to be specified in entirely XML.

My build-common.xml now contains












classname="net.sourceforge.sqlunit.ant.SqlunitTask"
classpathref="sqlunit-lib"/>

In addition to sqlunit I’ve found
dbUnit to be a useful tool for complex data setup prior to running a test.
Initilization of the dbUnit ant task is done as follows:












classname="org.dbunit.ant.DbUnitTask"
classpathref="dbunit-lib"/>

Hudson

This week I’ve been upgrading from CruiseControl to Hudson.  CruiseControl has served well for the past eighteen months, completing hundreds of builds with only the occasional restart.  Once configured with a couple of XML files CruiseControl is low maintenance, but my impression is that now Thoughtworks is offering a commercial product it is no longer getting as much attention as its competitors.  Hudson in particular is an open source CI solution that has received good reviews, and with the need to configure new Maven projects I thought this a good time to switch.

The best way to use Hudson seems to be with a dedicated VM including any infrastructure required to test the project being built, in my case for example Java 6, Maven2 and a dedicated instance of Oracle XE.  Although it is possible to simply start Hudson from the command line it makes more sense to install it as a service, and this is exactly what the Debian/Ubuntu package does.  I use Hudson with a Nexus maven repository, with settings.xml stored in …/tools/_usr_share_maven2/conf/, so far it seems to be working well.

dbMaintain

Since reading about database refactoring some while ago I’ve been seeking to adopt a tool to support automated tracking and application of database changes.  The best known solution for this is LiquiBase (fka Sundog Refactoring Tool), with over 10,000 downloads of LiquiBase Core recorded on SourceForge as I write this.  In order to adopt LiquiBase on an existing project, however, I was faced with replacing dozens of SQL DDL scripts with LiquiBase specific XML files.  There is a generateChangeLog command, but it has limitations: for example it does not recognize the primary key of Oracle index organized tables, and the output is not arranged anything like the documented best practices.

Instead, for now, I have adopted a newer tool called dbMaintain, which was presented at a Belgian Java User Group last April (see below) and a ServerSide news article last July.  dbMaintain provides similar functionality to LiquiBase, but instead of XML it simply passes through native SQL scripts using JDBC, making it much easier to adopt with confidence on an existing project.  I did run into one known issue with setting the default schema, but I’m happy to say Tim Ducheyne responded to my emails and provided a fixed snapshot within days.  I hope this project gains momentum, and I’m looking forward to the next release.

Technorati Tags:

PS. Other less mature alternatives include SQL based Thoughtworks dbdeploy and Carbon Five’s c5-db-migration.  Another custom XML based solution is MIGRATEdb.  Sony Pictures Imageworks have ported ActiveRecord migrations to Scala and there is a relatively inactive port of ActiveRecord migrations to Groovy called Bering.

Posts navigation

1 2 3 4 5 6
Scroll to top