February 14th, 2010 — Rails, Ruby, cucumber
I have found on numerous occasions that I need to look at HTML attributes when diffing tables etc. in Cucumber. My current need for this arrises as I have a gallery of thumbnails that all have alt tags who’s values I need to compare as there is not a text equivalent.
A feature statement like this:
Then I should see the following thumbnails
| Mona Lisa |
| Sunflowers |
Could be achieved with the following step:
Then /^I should see the following thumbnails$/ do |table|
nodes = Nokogiri::HTML(response.body).css('ul#gallery li img')
table.diff!(nodes.map { |img| [img.attributes['alt'].to_s] })
end
February 7th, 2010 — Rails, Ruby
…no, not because it leaves a bitter aftertaste, I’m talking about the Pickle step definitions for Cucumber.
I have lately been using Pickle when writing Cucumber features, however I have come to the conclusion that this is a bad idea. The reason being that when using Pickle, you create entries directly, whereas the whole point of Cucumber is that it is for high level integration testing.
What I do now, is to create any entries by filling out and submitting the relevant forms with a step definition, for example I may have the following:
Given an admin has created the following products
| Name | Variants | Featured |
| T-Shirt | Small: 10.99, Medium: 12.99, Large: 14.99 | Yes |
| Keyring | Default: 2.99 | Yes |
I would then write a step definition for this that would log in as the admin user, break this table appart and fill in the relevant forms.
I would go so far as to say that using factories at all in features is a bad idea and instead everything should happen via the user interface for better coverage. For any data that is known to exist when the app is deployed via ‘rake db:seed’, this can be loaded in the ‘env.rb’ file e.g.
Update 11/2/2010: Sometimes this is simply not practical due to slowdown which is a shame, as noted by Amos in the comments.
February 6th, 2010 — General, Rails, Ruby
I’ve had a set of rather bespoke requirements for authentication on a recent project and thought I’d give Devise a go.
Devise uses Warden, which is an authentication solution build on Rack making it extremely flexible and usable across multiple frameworks e.g. Rails/Sinatra. Also, Devise is extremely modular meaning to can easily write custom “strategies” for specific behaviour.
I have used Clearance in the past which is great if you want an engine that will just work. Devise however is by far the most flexible and extensible solution that I have come across with the same ease of use as Clearance. The only thing that you don’t get with devise that you do with Clearance is the signup stage, however as this is normally custom on a per-app basis I can live with this.
One more thing to note is that Devise lets you have multiple auth systems in play e.g. one for users and one for admins.
January 13th, 2010 — General
A quick note on how to delete a tag from a remote git repository:
git push github :refs/tags/my_tag
January 13th, 2010 — General
Just a quick post to say that I will be slowly releasing the ruby gems used at SonicIQ over the next few weeks/months. The first (more to test the workflow of Jewler/Gemcutter/Github than anything), is IQ::HTML.
IQ::HTML is a super simple gem with a few helpers for creating html markup much like you’ll see in ActionPack, however this is it’s only task and is therefore useful when you don’t want the extra weight of ActionPack.
Next up will be IQ::Form which currently exists on Github but is undergoing significant changes so I would suggest waiting for the new release.
January 8th, 2010 — General, Rails, Ruby, SonicIQ
I have just had my sexy validations patch excepted 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 =~ /^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!
January 8th, 2010 — Funny, General
Was just transferring my Feedburner account for this blog and clicked the “Item Use” link under “Analyze”, got an interesting message:

I think more web apps should introduce sarcasm, it definitely brightened up my day.
December 29th, 2009 — jQuery, javascript
Wow, that’s a mouthful of a title.
Just run into an interesting bug in Webkit I believe. When using Colorbox on a link contained inside a div who’s overflow attribute was set to hidden, the div was being scrolled to the bottom when hitting the “close” link of the colorbox. This only seemed to happen in Webkit browsers.
The solution was to watch for a scroll event on the div in question and scroll back to 0:
$(document).ready(function() {
var viewer = $('#viewer');
viewer.scroll(function() { viewer.scrollTop(0); });
});
Obviously, this is only a fix when you require your content to always be scrolled to the top, however this could serve as a starting point for other scenarios.
August 16th, 2009 — Rails, Ruby
Update: This patch has been applied but this information is out of date, please see: this new post.
Having had a patch accepted in Rails, I now have the bug (not a bug… the bug for writing patches… anyway).
I have just added this patch to allow for “sexy-migration-esque” validations using the new ActiveModel. Below explains the gist of it:
class EmailValidator < ActiveRecord::Validator
def validate
field = options[:attr]
record.errors[field] << "is not valid"
unless record.send(field) =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
end
end
# Register the custom validator in an initializer file.
ActiveModel::Validations.register_validator :email, EmailValidator
class Person < ActiveModel::Base
attr_accessor :name, :email
validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 }
validates :email, :presence => true, :email => true
end
As with the previous patch I believe this adds even more flexibility which is what Rails 3 is all about. It also allows validators to be shared more easily.
Rails defaults can now also be overridden e.g.
class RequiredValidator < ActiveRecord::Validator
def validate
field = options[:attr]
record.errors[field] << "Required" if record.send(field).blank?
end
end
ActiveModel::Validations.register_validator :email, RequiredValidator
I would appreciate a +1 on the ticket from anyone who feels this should be in Rails 3.0.
February 7th, 2009 — General
I’m pleased to give you LucidGrid, a super useful “bookmarklet”:http://en.wikipedia.org/wiki/Bookmarklet for working with grid based layouts…
I have been working on a few grid based layouts of late and consistently find myself sitting with a calculator tapping out the various dimensions. I was surprised to find that there weren’t any simple bookmarklets for this so I wrote one.
LucidGrid allows you to supply a width, the number of columns you need and the gutter size and then overlays a grid on the current page. You may disable the grid by clicking the bookmarklet again or by clicking exit. If you choose to apply the grid, the preference dialog will minimise to the top right of the window.
Grid changes happen in realtime so it is easy to play with different settings.

Simply drag the link below to your bookmarks bar in your browser or click it to try it out first:
LucidGrid
Enjoy… (currently tested with Safari 3.2.1 and Firefox 3.0.5).
p.s. I will be adding cookie support soon so that the grid remains with the same settings between page views.