Over the last couple of days I’ve been working on a Criteria for ActiveRecord. The concept is quite simple; rather than writing strings of SQL to pass to the find() method, we build the criteria using an object-orientated approach. Anyone who has used Hibernate, Torque, Propel et al will know how powerful and useful this can be.

For example, imagine you have a series of filters, all of which may, or may not, add constraints or expressions to your query. Each filter will need to append it’s own SQL to the end of any previously generated SQL and may even need to introspect the existing SQL to determine if its doubling up on statements or resolve potential conflicts.

With Criteria this becomes simpler. For example:

criteria = Criteria.new
criteria.and do |c|
  c.or User.role.eq(:admin)
  c.or User.active.eq(false)
  c.or User.createdAt.gt(10.hours.ago)
end
criteria.and do |c|
  c.or User.role.eq(:editor)
  c.or User.active.eq(true)
  c.or do |c2|
    c2.and User.createdAt.gt(20.days.ago)
    c2.and User.createdAt.lt(10.hours.ago)
  end
end
users = User.find(:all, criteria)

We could, at any point, introspect the state of criteria by calling:

criteria.ands.each do |c|
  put "AND #{c}"
end
criteria.ors.each do |c|
  put "OR #{c}"
end

And, finally, we can remove arbitrary bits of criteria:

criteria.ands.delete_at 2

I hope to have a release out very soon, but for the time being some more examples and API docs are on the Criteria project page on RubyForge

I have just decided to upgrade Eclipse, and thought it best to start from scratch. I do a lot of work in Java, PHP as well as Ruby on Rails and so I tried to set up an integrated environment using Eclipse. I have documented the steps here as much as a reminder for myself as a guide for other people trying to something similar.

First, download and unzip the Ecipse + J2EE package from http://www.eclipse.org/downloads/. You do not need to ‘install’ Eclipse as such, just put the uncompressed directory somewhere logical (/Applications on a Mac, for example).

  1. Launch Eclipse :P
  2. Select: Help -> Software Update -> Final and Install.
  3. Select Search for new features to install
  4. Check the Europa Discovery Service
  5. Select Add Remote Site
  1. Type in name: Aptana (For javascript support)
  2. Type in url: http://update.aptana.com/install/3.2/
  3. Select OK
  • Select Add Remote Site (For Ruby on Rails support)

    1. Type in name: RadRails
    2. Type in url: http://update.aptana.com/install/rails/3.2/
    3. Select OK
  • Select Add Remote Site (For SVN integration)
    1. Type in name: Subclipse
    2. Type in url: http://subclipse.tigris.org/update_1.2.x
    3. Select OK
  • Select Add Remote Site (for PHP support)

    1. Type in name: PDT
    2. Type in url: http://download.eclipse.org/tools/pdt/updates/
    3. Select OK
  • Select Add Remove Site (for Hibernate support)
    1. Type in name: Hibernate Tools
    2. Type in url: http://download.jboss.org/jbosside/updates/development
    3. Select OK
  • Select Finish
  • Select your nearest mirror, when prompted
  • Select Eurpoa Discovery Site -> Programming Languages -> Ruby Development Tools
  • Select PDT
  • Select Aptana
  • Select RadRails
  • Select Subclipse
  • Select Hibernate Tools
  • Select Select Required (to resolve dependencies)
  • Select Next
  • Accept the agreement(s) and select Next
  • Select Finish
  • Go for a walk.
  • Phew, so after all that, you should have an Eclipse IDE set up to support PHP, Ruby, Rails, Javascript, Java, Hibernate and Subversion. You can, of course, select many other tools from the Europa discovery site for other development tools, graphical editing and the like.