Monday, February 18, 2008

ActiveRecord Double Validation Errors in RSpec

I had a strange error occur in one of my rspec model unit tests today, and I wanted to document it here because my solution (which is a bit of a hack) is the opposite of what worked for other people.

I have a bunch of tests that check to make sure I'm validating various properties of a model. All of a sudden, I started having tests fail because the validations seemed to be adding the same error twice.
'User creation should require domain names to be unique' FAILED
expected 1 error on :domain, got 2
This error only occured when my full suite of tests ran. If I ran the unit test by itself (or even if I just ran only the model tests by themselves), it didn't happen.  Unfortunately I didn't notice whatever it was I did to introduce this error, so I couldn't reverse it.

Several other people have encountered this problem, so I know it's because my tests are leaking state in some way.  Somehow I am doing something during my tests that rspec isn't able to clean up.  Usually it's because the tests are doing something weird with extra require or load statements, which causes multiple copies of the class to be loaded.  Removing these statements usually works.  

I had no such statements and so spent a while trying to sort this out.  On a whim, I added a require statement to the top of the failing User model spec, and that fixed it:
require 'user'
I wish I had more time to investigate, because it makes me thing I don't know enough about Rails autoloading behavior, or Ruby's loading behavior. 

If you're running into this same problem, I found these mailing lists threads to be useful:

Tuesday, February 12, 2008

Unobtrusive Firefox Plugin Click-to-Install

I've been working on a really cool new project, to be announced soon, where I've built a Rails-based web app that has two interfaces: one for humans to interact with inside of a browser, and a RESTful API for browser plugins to interact with via GETs and POSTs.  We want people to be able to interact with our site while visiting other sites.


I had seen other Firefox plugins that were click-to-install, but I had a hard time figuring out how to make it work for our plugin. Firefox users were having to "Save Link As.." and open the downloaded .xpi file manually.  Very old-fashioned.  So here's a quick note to help future Mozilla or Firefox developers who need to create a click-to-install plugin:


1) It's all done through Javascript, so anyone without Javascript will have to install your plugin the old-fashioned way.  The Mozilla site documents the API call you need to make.


2) I'm a huge proponent of unobtrusive javascript (UJS), which I learned by using Dan Webb's excellent LowPro framework. Thus I wanted to make sure that the click-to-install javascript was offered as a progressive enhancement to the normal HTML links we provided.  That way, everyone could have a link to the plugin file itself for manual installation, but people with Javascript could enjoy click-to-install.


In this part of the site, we weren't using any other Javascript libraries, so it seemed like overkill to include Prototype and LowPro just for this one effect.  So it was a great chance to learn how to roll my own UJS without library support.  I whipped up a quick UJS click-to-install technique following inspiration from this presentation.  Here's what I came up with:

<script defer="defer" type="text/javascript">
//<![CDATA[
function doXPITrigger() {
if (!document.getElementsByTagName) return false;
var links = document.getElementsByTagName("a");
for (var i=0; i < links.length; i++) {
if (links[i].className.match("firefox")) {
links[i].onclick = function() {
xpi={'Awesome New Project Toolbar':'/downloads/awesome_project.xpi'};
InstallTrigger.install(xpi);
return false;
}
}
}
}

window.onload = doXPITrigger
//]]>

3) I've seen other advice recommending you configure your web server to recognize the .xpi mimetype appropriately.  I did this but it didn't make much difference in my case.  Still, it's probably worth doing.  I added this line to our Apache config:


AddType application/x-xpinstall .xpi

Wednesday, January 23, 2008

Shout out to Rails Envy & More Autotest Love

Thanks to the Rails Envy Podcast for mentioning my note on autotest configuration. Another aspect I enjoy about using the verbose flag is that autotest tells you exactly what has triggered the re-test. Kind of a fun way to look under the hood of your testing system:
[["app/controllers/admin/users_controller.rb", Wed Jan 23 14:06:10 -0600 2008]]
/usr/local/bin/ruby -S script/spec -O spec/spec.opts spec/controllers/other_inboxes_controller_spec.rb spec/controllers/admin/users_controller_spec.rb
If you like that, you might also be interested in the autotest timestamp plugin, which stamps autotest runs like so:
# Waiting at 2008-01-23 14:06:17
All you have to do is uncomment this line in your ~/.autotest file:
require 'autotest/timestamp'

Wednesday, January 16, 2008

Autotest with verbose flag on

I followed a tip from David Chelimsky's blog and began running autotest with the -v flag for verbosity.  When you first run it, you get a bunch of lines like this:
Dunno! spec/other_inbox_spec_helpers.rb
Dunno! app/views/layouts/main/_footer.erb
Which show all the files for which autotest doesn't have a mapping.  With ZenTest 3.8.0 out, it's easy to add mappings (which tell autotest which tests to run when a matching file changes) and exceptions (which tell autotest which files to ignore).  You can also set up .autotest files for particular projects, or for your whole development machine (~/.autotest).

Here's an example of a per-project .autotest I use, sitting in the Rails root directory.  I've got a custom spec helper and I want to rerun all my tests if this file ever changes:
Autotest.add_hook :initialize do |at|
%w{ domain_regexp perfdata coverage reports }.each { |exception| at.add_exception(exception) }

at.add_mapping(/spec\/app_spec_helper.rb/) do |_, m|
at.files_matching %r%^spec/(controllers|helpers|lib|models|views)/.*\.rb$%
end

end
And here's part of my site-wide .autotest file, mostly cribbed from David's blog, where I'm ignoring other kinds of cruft that pile up in projects.  Also note the mapping for spec/defaults.rb, a file I commonly setup in my specs containing default parameters for different models.
Autotest.add_hook :initialize do |at|
%w{.hg .git .svn stories tmtags Rakefile Capfile README spec/spec.opts spec/rcov.opts vendor/gems autotest svn-commit .DS_Store }.each {|exception|at.add_exception(exception)}

at.add_mapping(/spec\/defaults.rb/) do |f, _|
at.files_matching %r%^spec/(controllers|helpers|lib|models|views)/.*\.rb$%
end

end

Tuesday, January 8, 2008

Presenter classes help with Rails complexity

A few weeks back I gave a talk at a Bmore on Rails meeting all about Presenter classes, which I learned about from Jay Fields.  I can't claim any authorship of this idea, but it's been very helpful to me, so I thought I'd share some of my examples here plus a bit of extra code I wrote to make Presenter integration with ActiveRecord a bit more fun.

Presenters allow you to extract the logic needed for complex views (especially views that require the use of more than one model) into a separate, easily testable class.  This helps you write clean code and skinny controllers, among other benefits. 

1) The key background material is here:


2) I extended Jay Fields' code by adding methods to combine error messages from different models:


3) An example Presenter, combining a User object and an Account object into a Preference presenter, is here:


4) An example controller, using the Preference presenter, is here:


Also, Jay wrote an excellent recipe for Advanced Rails Recipes that covers this technique.

Thursday, January 3, 2008

Dynamic Content Caching Recipe

hello everyone,

Pragmatic Programmers just released an updated beta version of Advanced Rails Recipes which contains another recipe that I contributed, this one about dynamic content caching. I've built a few sites that had a lot of static content, with only bit of dynamic content on each page (usually a signout button, or an admin link). In these cases, I was able to use page caching with a little bit of Javascript that looks for a cookie in the client browser and alters the page accordingly. The book is shaping up great and I'm learning a lot by reading other recipes, so definitely check it out!

Wednesday, January 2, 2008

Improv and personal transformation

In summer 2007 Towson Unitarian Universalist Church asked me to speak at one of their services about my experiences as an improv theater director, teacher, and performer.  Rather than tell them how great improv is, I decided to share what I think improv reveals about the human condition.  They were nice enough to tape the talk and give me a copy, which I present here for all eternity, in MP3 format (it's about 19 minutes long).

UPDATE: I had this speech transcribed by escriptionist.com. The transcript wants some editing and translation, but if you just feel like skimming instead of listening to the audio, it's pretty good (PDF file).