Releasing IQ::Color

At SonicIQ we have a number of RubyGems used internally that we haven’t found time to open-source yet i.e. cleanup the documentation and get into state that people can find useful. I’m going to make it my mission to get some of these out there which first involves releasing some of the smaller Gems that the larger ones depend on.

IQ::Color is a really simple Gem for converting colour values to and from CSS(3) notation, this is used by a couple of our larger Gems but I can see it being of use on it’s own. A simple example:


color1 = IQ::Color.RGB.new(127, 127, 255)
color1.to_css #=> "#7f7fff"

color2 = IQ::Color.from_css('rgba(127,127,255,0.4)')
color2.red    #=> 127
color2.green  #=> 127
color2.blue   #=> 255
color2.alpha  #=> 0.4
color2.to_css #=> "rgba(127,127,255,0.4)"

More examples can be found on GitHub.

Fixing {{model}} placeholders in Rails 2.3 when Rails 3 is installed

This was causing me all kinds of grief. I am running Rails 2.3 and Rails 3 apps on the same server and on installing Rails 3, the Rails 2.3 apps started displaying {{model}} and similar strings in views.

It turns out that Rails 3 uses i18n version 0.5.0 and with this version as far as I can see, the placeholder text in translations follow the %{text} format instead of the {{text}} format in 0.4.2 which Rails 2.3 was using. The only way I found to fix this was to declare the specific i18n version before the Rails gem gets required in the “load_rails_gem” method of “config/boot.rb” in my Rails 2.3 apps:


def load_rails_gem
  gem 'i18n', '0.4.2' # Add this line

  if version...
end

Doing this specifies that we want i18n 0.4.2 exactly, whereas Rails 2.3 specifies version “>= 0.1.3″ which would obviously includes “0.5.0”.

In running Rails 2.3 and Rails 3 apps side by side, I also ran into this problem and overcame it with the fixes by “bct” and “ronin-95281” in the comments… apparently Rails Core won’t fix this.

If anyone else has a cleaner fix for this I would love to hear about it.

Sexy Validation in Edge Rails (Rails 3)

I have just had my sexy validations patch accepted into Rails. Much thanks to José Valim for helping me get this applied.

The reason for the name “sexy validations” is that it gives a much more concise way of defining validation and reusing custom validator classes. Much like what sexy migrations did for defining your database schema.

Simple example of using existing Rails validations, the “sexy” way:


class Film < ActiveRecord::Base
  validates :title, :presence => true, :uniqueness => true, :length => { :maximum => 100 }
  validates :budget, :presence => true, :length => { :within => 1..10000000 }
end

The power of the “validates” method comes though, when using in conjunction with custom validators:


class IntenseFilmTitleValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << "must start with 'The'" unless value =~ /^The/
  end
end

class SpendValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    spend = case options[:size]
      when :big then 100000000
      when :small then 100000
    end
    record.errors[attribute] << "must not exceed #{spend}" if value > spend
  end
end

class Film < ActiveRecord::Base
  validates :title, :presence => true, :intense_film_title => true
  validates :budget, :spend => { :size => :big } # using custom options
end

All validations in Rails, along with other common model functionality have been extracted into ActiveModel, so you can also use validations and Validator classes without ActiveRecord e.g.


class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || "is not an email") unless
      value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
  end
end

class Person
  include ActiveModel::Validations
  attr_accessor :name, :email

  validates :name, :presence => true, :length => { :maximum => 100 }
  validates :email, :presence => true, :email => true
end

Have fun!

SonicIQ Hiring! – UK, Ruby on Rails Developer Required

We are looking for a Ruby on Rails, XHTML & CSS Developer to join our team at “SonicIQ”:http://soniciq.com. Head over to “43folders job board to view our ad”:http://jobs.43folders.com/job/6a5255713e8351a5eb2efef7805b7629/?d=1.

These are exiting times with projects like “Propel’r”:http://propelr.com in the pipeline, along with the ever-growing opportunities for new and interesting client projects.

If you are a highly motivated developer and can see yourself in a Ruby on Rails position in sunny (sometimes) Bournemouth, UK then “apply at 43folders”:http://jobs.43folders.com/job/6a5255713e8351a5eb2efef7805b7629/?d=1.

Ruby on Rails Exchange, London

Last Friday, a couple of us from “SonicIQ”:http://www.soniciq.com went to the first “RoR eXchange hosted by Skills Matter”:http://www.skillsmatter.com/rorexchange.

It was a great day with some interesting talks from:

* Chad Fowler
* Tom Locke
* Paul Battley
* James Cox
* Damien Tanner
* Ben Griffiths
* Eleanor McHugh

h3. Chad Fowler – Quick and -Dirty- Clean: Well factored Rails

Chad kicked off with a talk on “Quick and -Dirty- Clean: Well factored Rails”. For me it was one of those talks where you think “If only I heard this 6 months ago!”. The reason I say this is that a great deal of what he was talking about, we now implement on a day-to-day basis, but we have gotten there the hard way through making the mistakes that he mentions.

He talks about never using instance variables in views, I love that someone has confirmed this as I find it generally bad practice. For a while now we have been replacing them with helpers giving much more flexibility when the controller logic changes.

One great point that was made, was that SQL should never be used in controllers, not even in :order options. I have to admit, in the past I have done this a lot but the suggestion of custom finders in the model are a much better alternative and it keeps with the whole “Skinny Controller, Fat Model”:http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model idea.

One thing I really must get into the habit of using are association proxies for has_many and such like. Chad showed some great examples of this.

I like one phrase that Chad came up with… “Bare Model Finds” which made me chuckle. What he was getting at was something like an account based site where everything should be found via the given account i.e. @account.projects.find(:all)

as opposed to a “Bare Model Find” of Project.find(:all, :conditions => [‘account_id = ?’, @account.id])

The main points I have taken away from this talk (which I am also going to put in a large poster on the office wall) are the folowing:

* No instance variables in views
* No SQL in controllers
* No more than 4 lines in controller actions

h3. Tom Locke – Experiments with Very Rapid Development in Rails

Tom gave a demo of his “Hobo”:http://hobocentral.net/blog/ project which is very similar to a plugin we use in-house (which also may go open source at some point).

One thing I like about Hobo is the incremental nature of the “dryml” views, meaning that you can override certain elements in a default view bit-by-bit, i.e. if you just want a different header for a certain page you don’t need to create a duplicate of the view and change it, you only need to redefine certain elements of the page.

I completely agree with Tom’s views on generators, although he was slightly more diplomatic than I would have been. I am going to be slightly controversial and say that I really don’t like generators except for creating stub files. What happens if you have an authentication system generator which you have used in 50 projects and you suddenly find a security hole?? …You now have to go back through those 50 projects to fix it! [end generator rant]

h3. Paul Battley – Rerouting Rails

Paul attempted to explain how he believes the rails routing system should be re-written. I could see a lot of merit in what he was saying, however I don’t believe it lent itself very well to this type of presentation.

h3. James Cox – Managing a high performance rails app without tearing your hair out

James gave some great tips on easy ways to increase the overall performance of Rails apps. We are in the process of configuring a new server so these tips came at a good time.

h3. Damien Tanner – Extend your Rails application using Domain Specific Languages

Damien explained a roundtrip his company went on with DLS’s explaining that they are great in certain situations but can be taken too far or overused. In the example he gave, it turned out that the DSL he created was overkill for the business problem he was trying to solve.

h3. Ben Griffiths – Are your tests working for you?

For me, this and Chad’s were the presentations that made the trip to London worth while. I am ashamed to say that until now, I have not gotten into TDD (I’ve been using Rails for over a year and a half). It’s not through lack of trying but with clients constantly on your back to get projects out the door, it’s all to easy to dive straight into coding the application and neglecting the tests.

He started with a comparison between testing and the film Predator. This sounds like a strange comparison however it really helped to illustrate the need for a good set of tests.

My main problem with tests were “what do I test?” and “my fixtures need too much maintenance!”. I breathed a sigh of relief when Ben suggested not using fixtures at all as maintaining a load of fixtures really turned me off testing.

This was a great talk and I could bang on for hours on the benefits of testing with Ben’s techniques and his whole take on modular design but I’ll save that for another post. As a side-note, I have been doing TDD ever since this talk.

h3. Eleanor McHugh – Where’s my SQL? Platform-independent database design using Migrations

Eleanor gave some examples of how migrations could be extended with plugins. A lot of the examples she gave were based on what has been implemented in Hobo. There were some interesting points in the talk although my concentration levels were frayed by this point.

h3. Group Discussion

The main point of discussion was views. I think everyone was in agreement that something needs to change with the way Rails handles it’s views. I personally believe that they should be purposefully limiting so that it is hard to do bad things in the view. I am looking forward to seeing views taken to the next level and believe that there will be some large changes before 2.0 based on the comments during this discussion.

h3. After Party

The guys at “Skills Matter”:http://www.skillsmatter.com put on the first 100 drinks at “Liquid” – a Japanese-themed bar. We had a chance to mingle and discuss a few of the points that we forgot to ask after the talks.

Thanks to all the speakers, all in all a great day out.

Videos of some of the talks are now “available online”:http://skillsmatter.com/menu/479. Also, my colleague “Andy”:http://darkliquid.co.uk has blogged about his take on the day: “Part 1″:http://darkliquid.co.uk/2007/2/10/ruby-on-rails-exchange, “Part 2″:http://darkliquid.co.uk/2007/2/11/ruby-on-rails-exchange-part-2.

New web-based GTD productivity application

Finally I can mention it. At SonicIQ we’ve been working on a web-based “GTD”:http://davidco.com application. Today the teaser site went live at “propelr.com”:http://www.propelr.com.

If you are are into “GTD”:http://davidco.com, life hacks or productivity in general head over to “propelr.com”:http://www.propelr.com. for a sneak peek!

A break down in communication

We (“SonicIQ”:http://www.soniciq.com) have just completed our second office move in 5 months. Hopefully this one will be slightly more long term.

We had to get the BT man out today as our new phone line hasn’t been working for the past two days (and subsequently our internet connection). The poor guy was scratching his head as to what the problem could be…

BT sold us a “Feature Line” however what they neglected to tell us was that one of the “features” was that you have to dial 9 to get an outside line. It turns out that this was the problem all along!! As for the internet connection the router was set to PPPoE instead of PPPoA, this would have worked from the beginning also.

It’s ironic, the lack of communication a national communications company has… they failed to mention it in the correspondence and it was not mentioned during any of the sales calls.