<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
  <title>EdgeCase - Home</title>
  <id>tag:blog.edgecase.com,2009:mephisto/</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  
  <link href="http://blog.edgecase.com/" rel="alternate" type="text/html" />
  <updated>2009-06-25T02:45:09Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/edgecase" type="application/atom+xml" /><entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-06-25:780</id>
    <published>2009-06-25T00:40:00Z</published>
    <updated>2009-06-25T02:45:09Z</updated>
    <link href="http://blog.edgecase.com/2009/6/25/make-ajax-file-uploads-sexy" rel="alternate" type="text/html" />
    <title>Make Ajax File Uploads Sexy (screencast)</title>
<content type="html">
            &lt;p&gt;&lt;a href="http://blog.edgecase.com/2009/6/15/ajax-file-uploads-made-easy-screencast"&gt;Last week&lt;/a&gt; I showed you how to perform file uploads via Ajax, but the result was too ugly for my taste.  Today we're going to make it sexy.  Check it out.&lt;/p&gt;

&lt;p&gt;&amp;lt;embed src="http://blip.tv/play/g9dWgYyMBQA" height="430" width="535"&gt;&amp;lt;/embed&gt;&lt;/p&gt;

&lt;p&gt;The source code for the example app is &lt;a href="http://github.com/adamlogic/ajax_file_upload_example/tree/master"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next week I plan to dive into using Flash to enable selecting and uploading mutiple files with progress indication.  Stay tuned.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/wICNA2a0aQw" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-06-15:779</id>
    <published>2009-06-15T14:18:00Z</published>
    <updated>2009-06-25T00:45:57Z</updated>
    <link href="http://blog.edgecase.com/2009/6/15/ajax-file-uploads-made-easy-screencast" rel="alternate" type="text/html" />
    <title>Ajax File Uploads Made Easy (screencast)</title>
<content type="html">
            &lt;p&gt;Here is my second screencast for the EC blog, this time talking about Ajax file uploads in Rails using jQuery.  Let me know what you think in the comments.&lt;/p&gt;

&lt;p&gt;&amp;lt;embed src="http://blip.tv/play/g9dWgYnOCwA" height="430" width="535"&gt;&amp;lt;/embed&gt;&lt;/p&gt;

&lt;p&gt;A Non-Flash version is available &lt;a href="http://blip.tv/file/2243026/"&gt;here&lt;/a&gt;.
The plugin discussed is on &lt;a href="http://github.com/adamlogic/jaxy_file_uploads"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;update:&lt;/strong&gt; I decided to turn this into a series of screencasts on how to handle file uploads on the client side.  &lt;a href="http://blog.edgecase.com/2009/6/25/make-ajax-file-uploads-sexy"&gt;Part two&lt;/a&gt; is now available, which builds on this example.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/QUOYS7GCIVU" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-06-02:777</id>
    <published>2009-06-02T13:51:00Z</published>
    <updated>2009-06-02T14:17:31Z</updated>
    <link href="http://blog.edgecase.com/2009/6/2/a-rant-about-testing" rel="alternate" type="text/html" />
    <title>A Rant About Testing</title>
<content type="html">
            &lt;p&gt;Time to stir up some controversy.  It’s been a while since I’ve posted, so why not come back with a bang.  Disclaimer, though: these are only the opinions of me, &lt;a href="http://twitter.com/adamlogic"&gt;Adam McCrea&lt;/a&gt;, and not of EdgeCase.&lt;/p&gt;


	&lt;h2&gt;&lt;span class="caps"&gt;TATFT&lt;/span&gt;&lt;/h2&gt;


	&lt;p&gt;There’s a meme going around for a while that’s gained a lot of popularity, and I for one would like to say that I do not, nor do I have any desire to &lt;a href="http://smartic.us/2008/8/15/tatft-i-feel-a-revolution-coming-on/"&gt;Test All The Time&lt;/a&gt;.  Being religious about testing is a recipe for brittle tests, wasted effort, and a false sense of security.  I’d rather shift from religion to pragmatism.  This means thinking hard about what and when we test.&lt;/p&gt;


	&lt;p&gt;In a few recent projects, I’ve begun heavy integration testing.  No, I’m not breaking any new ground, but the fact is prior to these recent projects, I had no integration tests.  I was a bad, bad little programmer.  That aside, I have to say that it’s been incredibly eye-opening.&lt;/p&gt;


	&lt;p&gt;I like to test as far up the stack as I can, getting as close as possible to the end user.  Right now that means using &lt;a href="http://github.com/brynary/webrat/tree/master"&gt;Webrat&lt;/a&gt; to mimic actual clicks and form interaction to navigate the app.  Sticking to this approach, I can change the internals of the app however I want as long as the generated &lt;span class="caps"&gt;HTML&lt;/span&gt; for links and form fields remains untouched.  If you had told me a few months back that I could substantially refactor my code and &lt;strong&gt;not change one test&lt;/strong&gt;, I would have thought you were crazy.  But integration tests make that possible.&lt;/p&gt;


	&lt;h2&gt;Less testing&lt;/h2&gt;


	&lt;p&gt;But how does that reduce waste?  For starters, I no longer write controller (or view) tests.  And neither should you.  Seriously, just stop.  You’re not only wasting your time, but also every future developer who touches that code and has to maintain your useless tests.  This assumes two things: that you’re thoroughly integration testing your app, and that your controllers are as skinny as possible.  If you still think you need controller tests, you’ve probably got crap in your controllers that shouldn’t be there.  Get it out, and stop writing those tests.&lt;/p&gt;


	&lt;p&gt;I’ll proudly admit that I’m also testing my models a lot less.  Seriously, what’s the point of testing things like attribute readers/writers and associations if I’ve got a full suite of integration tests?  If my comments don’t belong to posts, I’ll get yelled at, and it’s not going to take long to pinpoint the problem.  So, I’m really only unit testing substantial model and helper methods.  Everything else gets hit at the integration level only.&lt;/p&gt;


	&lt;h2&gt;What about coverage?&lt;/h2&gt;


	&lt;p&gt;I still think coverage is important, but only at the integration level.  Whether or not a piece of code is unit tested is purely subjective, but every line of code must be tested from the point of view that matters most – the user’s.  100% integration test coverage is no guarantee that you’ll catch every possible bug, of course, but anything less would indicate that you wrote some code without thinking about how it will be used.&lt;/p&gt;


	&lt;h2&gt;So is Cucumber the answer?&lt;/h2&gt;


	&lt;p&gt;Honestly, it just doesn’t matter.  Just test your code from as far up the stack as possible using whatever tool you prefer.  Cucumber, Rails integration tests, Selenium, well-trained monkeys, whatever.  Go for it.  I do like &lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt;, though, so it’s certainly worth a try if you’re new to full-stack testing.&lt;/p&gt;


	&lt;p&gt;Ideally, JavaScript would be included in these tests, I just haven’t found a solution yet that makes it practical.  It would require a masochist to get 100% coverage with Selenium, but &lt;a href="http://github.com/langalex/culerity/tree/master"&gt;Culerity&lt;/a&gt; is one promising option that has popped up.  I haven’t had a chance to check it out, though.&lt;/p&gt;


	&lt;p&gt;This approach certainly isn’t for everyone, but the current wave of &lt;span class="caps"&gt;TATFT&lt;/span&gt; is in desperate need of a counterpoint.  If you like to use unit tests as a way to design your code, you probably think I’m way off base.  That’s fine.  We don’t all have to play by the same rules.  Hell, even I still &lt;span class="caps"&gt;TDD&lt;/span&gt;, just not at the unit level.  The point here is to make sure you’re very conscious of what, when, and why you test, and that you’re getting enough value from them to justify the time spent.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/Vo6aI1LDubU" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-05-11:746</id>
    <published>2009-05-11T12:37:00Z</published>
    <updated>2009-05-11T12:38:51Z</updated>
    <link href="http://blog.edgecase.com/2009/5/11/bookmarklets-101" rel="alternate" type="text/html" />
    <title>Bookmarklets 101</title>
<content type="html">
            &lt;p&gt;Last week I showed you a bookmarklet that lets you edit your CSS live in any browser.  Today, I want to dive into the details of how to create a bookmarklet.  But first, the basics:&lt;/p&gt;

&lt;h2&gt;What is a bookmarklet?&lt;/h2&gt;

&lt;p&gt;A bookmarklet is a small snippet of JavaScript crammed into a URL.  Nothing else.  That's not very helpful, though, is it?  All it means is this: instead of using the http: or ftp: protocol for a URL, we use the javascript: protocol.  And when this URL is invoked (perhaps by clicking a bookmark in your browser's bookmark toolbar) the remainder of the URL is executed as JavaScript.  What this means is that we can execute any arbitrary JavaScript within the context of any site we please.  And we can package it so that it can be shared and reused easily.  But...&lt;/p&gt;

&lt;h2&gt;What is the point?&lt;/h2&gt;

&lt;p&gt;In general a bookmarklet will either alter the current page in some way, or make use of the context of the current page in concert with some other service.  Some examples of the former are &lt;a href="http://blog.edgecase.com/2009/5/4/edit-css-in-any-browser-with-the-click-of-a-button"&gt;edit CSS&lt;/a&gt;, &lt;a href="http://www.learningjquery.com/2008/06/updated-jquery-bookmarklet"&gt;jQuerify&lt;/a&gt;, &lt;a href="http://getfirebug.com/lite.html"&gt;Firebug Lite&lt;/a&gt;, and &lt;a href="http://www.badlydrawntoy.com/2009/04/23/grid960-a-grid-overlay-bookmarklet-for-960gs/"&gt;960gs grid overlay&lt;/a&gt;.  These are usually only useful to developers.  Some examples of the latter are &lt;a href="http://is.gd/instructions.php"&gt;URL shorteners&lt;/a&gt;, &lt;a href="http://delicious.com/help/bookmarklets"&gt;post to delicious&lt;/a&gt;, &lt;a href="http://www.amazon.com/wishlist/get-button"&gt;Amazon wishlist&lt;/a&gt;, and &lt;a href="http://www.instapaper.com/i"&gt;Instapaper&lt;/a&gt;.  These are much more useful to consumers.  Both kinds of bookmarks use the same technology, though, so the approach to developing them is basically the same.&lt;/p&gt;

&lt;h2&gt;The "Hello World" bookmarklet&lt;/h2&gt;

&lt;p&gt;Create a bookmark, and give it the following URL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;javascript:alert('hello%20world');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now click the bookmark.  Yep, that's all there is to it.  Notice that we replaced the space with %20.  That's important, and there are a few more rules that we need to keep in mind when creating bookmarklets.&lt;/p&gt;

&lt;h2&gt;Gotchas &amp;amp; Tips&lt;/h2&gt;

&lt;h3&gt;URL length limit&lt;/h3&gt;

&lt;p&gt;I'm not sure what the definitive answer is on this.  IE is (unsurprisingly) the limiting factor here, but I've heard &lt;a href="http://www.garyharan.com/index.php/2008/01/04/cross-browser-bookmarklets-stuff-you-should-know/"&gt;conflicting&lt;/a&gt; &lt;a href="http://www.javascriptkata.com/2007/04/19/how-to-do-a-bookmarklet-with-javascript/"&gt;information&lt;/a&gt; on what the actual limit is.  I haven't bothered to test it since that would mean firing up Windows and opening IE, which I avoid at all costs.  So, just keep it really really short, m'kay?&lt;/p&gt;

&lt;h3&gt;Cheat the URL limit&lt;/h3&gt;

&lt;p&gt;Most bookmarklets try to do way more than is possible within the URL limit I just described, so of course there is a way to cheat the rule.  Just host an external JS file that gets dynamically required by your bookmarklet.  &lt;a href="http://github.com/adamlogic/auto_update_stylesheets/blob/166c8c2e62d6b93eddd5c59fe2d645907a204cfd/bookmarklet.js"&gt;Here&lt;/a&gt; is an example.  The bookmarklet just appends a SCRIPT tag into the HEAD of the document, which requests and executes the external JS file.&lt;/p&gt;

&lt;h3&gt;Don't return a value&lt;/h3&gt;

&lt;p&gt;If your bookmarklet code returns a value, that value will find it's way to the browser's address bar.  This is most likely not what you want, so you have two weapons to use against this.  You can append a void() call at the end of your JS code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;someCode();void(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you can wrap your JS in an anonymous function.  The added benefit here is that any variables you define are not polluting the global scope:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function(){someCode()}();
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Don't depend on a JS library&lt;/h3&gt;

&lt;p&gt;Probably an obvious one, but you can't assume that jQuery, Prototype, or any other library is included in the page where your bookmarklet will eventually be run.  I got around this in my Edit CSS bookmarklet by dynamically including jQuery and running it in noConflict mode.  See the code for that &lt;a href="http://github.com/adamlogic/auto_update_stylesheets/blob/166c8c2e62d6b93eddd5c59fe2d645907a204cfd/load_it.js"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;Next steps&lt;/h2&gt;

&lt;p&gt;If you want to write a bookmarklet that integrates with a Rails application, definitely check out John Nunemaker's &lt;a href="http://video.railstips.org/creating-and-integrating-bookmarklets-with-rails/"&gt;screencast&lt;/a&gt;.  Also dig into the bookmarklets that I linked to earlier in the article.  If you have a favorite bookmarklet or want to know more about bookmarklets, please join the discussion below.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/hV8KIKYmwrw" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-05-04:722</id>
    <published>2009-05-04T12:22:00Z</published>
    <updated>2009-05-04T12:22:54Z</updated>
    <link href="http://blog.edgecase.com/2009/5/4/edit-css-in-any-browser-with-the-click-of-a-button" rel="alternate" type="text/html" />
    <title>Edit CSS in Any Browser With the Click of a Button </title>
<content type="html">
            &lt;p&gt;So, a few weeks back I showed you how you could &lt;a href="http://blog.edgecase.com/2009/4/6/see-your-css-changes-in-real-time-in-any-browser"&gt;see your &lt;span class="caps"&gt;CSS&lt;/span&gt; changes in real-time in any browser&lt;/a&gt;.  That was cool and all, but I haven’t found myself using it simply because of the convenience factor.  The script (along with jQuery) had to be included in the page, whereas I could just fire up Web Developer Toolbar in an instant.&lt;/p&gt;


	&lt;p&gt;Today I’m introducing the bookmarklet version of that same script.  It no longer requires jQuery, so just drag the link below up to your bookmark toolbar, and you’ll have this functionality one click away.&lt;/p&gt;


	&lt;p&gt;&lt;a&gt;Edit &lt;span class="caps"&gt;CSS&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I’ve also put together a short (3-minute) demo below.  Enjoy.&lt;/p&gt;


&amp;lt;embed src="http://blip.tv/play/Af74DQA" height="452" width="575"&gt;&amp;lt;/embed&gt;

	&lt;p&gt;Non-Flash version of screencast is available &lt;a href="http://blip.tv/file/2069219/"&gt;here&lt;/a&gt; (the Files and Links section in the sidebar).&lt;/p&gt;


	&lt;p&gt;The source for this is on &lt;a href="http://github.com/adamlogic/auto_update_stylesheets"&gt;Github&lt;/a&gt;, so please add issues there and fork away.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/yqo_iRnzT1s" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-04-27:702</id>
    <published>2009-04-27T12:36:00Z</published>
    <updated>2009-04-27T12:41:07Z</updated>
    <link href="http://blog.edgecase.com/2009/4/27/best-cms-for-developers-rails" rel="alternate" type="text/html" />
    <title>Best CMS For Developers?  Rails.</title>
<content type="html">
            &lt;p&gt;The site you’re looking at right now (unless you’re reading from &lt;span class="caps"&gt;RSS&lt;/span&gt;) is old, ugly, and busted.  Yes, the EdgeCase site – and blog – are in desperate need of an overhaul.  While &lt;a href="http://twitter.com/nummi"&gt;@nummi&lt;/a&gt; kicks around some design ideas, I’ve started looking into how to power our future home on the web.&lt;/p&gt;


	&lt;p&gt;Our needs are pretty simple.  We need a collection of pseudo-static pages (occasional updates, but with some dynamic content), and a blog.  This sounds like a simple &lt;a href="http://en.wikipedia.org/wiki/Content_management_system"&gt;&lt;span class="caps"&gt;CMS&lt;/span&gt;&lt;/a&gt; to me, and &lt;span class="caps"&gt;CMS&lt;/span&gt; solutions are supposed to make sites like this completely painless, right?&lt;/p&gt;


	&lt;p&gt;Yes, I am that delusional.&lt;/p&gt;


	&lt;p&gt;You see, I’ve never actually used a &lt;span class="caps"&gt;CMS&lt;/span&gt;.  Everything we’ve built at EdgeCase has been far too custom to be considered a &lt;span class="caps"&gt;CMS&lt;/span&gt;, so until last Friday, I’d really only heard about them.  Unfortunately, what I discovered after looking at some popular Ruby &lt;span class="caps"&gt;CMS&lt;/span&gt; solutions (&lt;a href="http://radiantcms.org/"&gt;Radiant&lt;/a&gt; and &lt;a href="http://www.browsercms.org/"&gt;BrowserCMS&lt;/a&gt;) is that I &lt;strong&gt;hate&lt;/strong&gt; CMS systems.&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;A CMS&lt;/span&gt; might make a lot of sense for a business of non-techies – regular folks who need to manage their content without calling up the “web guy”.  But that’s certainly not us.  We’re an entire company of “web guys”.  At this point you might be thinking I’m ignorant for not realizing we’re outside the target audience for a &lt;span class="caps"&gt;CMS&lt;/span&gt;, and you’re right.  But I didn’t realize this, so I’m going to rant for a bit on why &lt;span class="caps"&gt;CMS&lt;/span&gt; systems suck for developers.&lt;/p&gt;


	&lt;p&gt;The problem is that the benefits regular users get from a &lt;span class="caps"&gt;CMS&lt;/span&gt; just don’t apply to developers, and in fact they often cripple us.  Take the most obvious &lt;span class="caps"&gt;CMS&lt;/span&gt; feature of editing your site content within a browser.  Is there a developer out there who is as comfortable in an &lt;span class="caps"&gt;HTML&lt;/span&gt; textarea or a Word-like &lt;span class="caps"&gt;WYSIWYG&lt;/span&gt; as they are in their preferred text editor?  But really this is the tip of the iceberg, a minor issue that I can get over, and I also can’t deny the convenience of committing a content change with the click of a single submit button.&lt;/p&gt;


	&lt;p&gt;But going a step further, most &lt;span class="caps"&gt;CMS&lt;/span&gt; systems also make the templates available for editing within a browser.  This is where real pain sets in for me, and for two reasons: &lt;a href="http://wiki.github.com/radiant/radiant/radius-tags"&gt;bastardized&lt;/a&gt; &lt;a href="http://www.liquidmarkup.org/"&gt;templating&lt;/a&gt; &lt;a href="http://codex.wordpress.org/Template_Tags"&gt;languages&lt;/a&gt;, and lack of a real editor.  As a Rails developer, I’m very comfortable with &lt;span class="caps"&gt;ERB&lt;/span&gt;, and even more so with &lt;span class="caps"&gt;HAML&lt;/span&gt;.  I should &lt;strong&gt;not&lt;/strong&gt; have to learn a new templating language just to build my simple website.  And regardless of which language I use, I simply don’t have the patience to code &lt;span class="caps"&gt;HTML&lt;/span&gt; in a textarea.  I may not be a fan of editor wars, but I at least need an actual text editor if I’m building a website.&lt;/p&gt;


	&lt;p&gt;Other &lt;span class="caps"&gt;CMS&lt;/span&gt; niceties… Content versioning?  If the content is part of the code base, I get that for free with Git.  Permissions?  Our needs are simple – if you have access to edit the code, you have access to edit the content.  Plugins?  Rails certainly has plenty of plugins, and with Rails 2.3 supporting Rails Engines and Rack middleware, the possibilities are endless.&lt;/p&gt;


	&lt;p&gt;So where does this rant leave me for the EdgeCase site?  A Rails app, of course – custom built, because I’m not convinced that anything pre-built is actually going to make my life easier.&lt;/p&gt;


	&lt;p&gt;For the blog component of the site, I’ve got a lot of options to explore.  I could, of course, build something from scratch.  This might not be a bad idea, since our needs are pretty simple.  But there are also some blog engines out there that could be worth checking out: &lt;a href="http://github.com/karmi/marley/tree/master"&gt;Marley&lt;/a&gt; and &lt;a href="http://github.com/adamwiggins/scanty/tree/master"&gt;Scanty&lt;/a&gt; are a couple that look very interesting.  I’d love to see an engine-style Rails plugin that provides a blog, but I haven’t come across one yet.  Perhaps we’ll just have to build one ourselves.  After all, a blog can be &lt;a href="http://rubyonrails.org/screencasts"&gt;built in 15 minutes&lt;/a&gt;, right?&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://twitter.com/adamlogic"&gt;Follow me&lt;/a&gt; on Twitter if you want updates on what direction we take.  Or you could just wait for the redesigned EdgeCase site.  It should arrive on or before 2011.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/eplMfwCPpTU" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-04-22:698</id>
    <published>2009-04-22T12:49:00Z</published>
    <updated>2009-04-22T12:55:42Z</updated>
    <link href="http://blog.edgecase.com/2009/4/22/debugging-jquery-events" rel="alternate" type="text/html" />
    <title>Debugging jQuery Events</title>
<content type="html">
            &lt;p&gt;When an app reaches a certain level of complexity, particularly with regards to JavaScript, you’re probably going to have events bound and triggered all over the place.  Events are a powerful thing, but they can be a bitch to debug.&lt;/p&gt;


	&lt;p&gt;This didn’t use to be the case.  Back when we were all setting events inline, all you had to do was look at the &lt;span class="caps"&gt;HTML&lt;/span&gt; to know what events are bound to to an element:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
&amp;lt;form onsubmit="if (!validate(this)) return false;"&amp;gt;...&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I’m going to give you the benefit of the doubt that you’re not doing this anymore. I’ve been writing (and evangelizing) unobtrusive JavaScript for years now, but I have to admit this is one aspect of inline event handling that I really miss.&lt;/p&gt;


	&lt;p&gt;Take this scenario:&lt;/p&gt;


	&lt;p&gt;You’ve given a form a class of “ajax”, and you’ve got the following jQuery in place to make sure that all forms with a class of “ajax” are submitted via Ajax.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
$('form.ajax').ajaxForm();
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Unfortunately, when you try to submit the form, nothing happens.  Nothing at all.  No Ajax.  No form post.  No errors.&lt;/p&gt;


	&lt;p&gt;What do you do?&lt;/p&gt;


	&lt;p&gt;In the past, I’d start sticking console.log statements in my JS, or maybe debugging directly in Firebug. Recently, though, I found a better way.  I was fumbling my way through the event code in jQuery, and I discovered that event data is easily accessible through the element itself. Here is how a Firebug session might look as I dig into these events:&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://blog.edgecase.com/assets/2009/4/22/Prosper_-_Timesheet.jpg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;The first thing I do is grab the form element, and see what it has in the “events” data (if you’ve never messed with the data() method before, &lt;a href="http://docs.jquery.com/Core/data"&gt;you should&lt;/a&gt; ). I see that there is at least one handler assigned to the “submit” event, so I iterate through each of them and call toString() on each function so I can see the actual code.&lt;/p&gt;


	&lt;p&gt;The second function listed is the actual code for the ajaxForm() method, so it’s cool.  The first one, though, is the culprit I’m looking for.  This handler is being bound to my form’s submit event somewhere, and it’s calling stopImmediatePropagation() which prevents any other handlers from being called. Then it’s just a matter of searching for that exact code in the project, removing or fixing it, and beating the developer who put it there (which is usually me).&lt;/p&gt;


	&lt;p&gt;I admit this is not as easy as glancing at the &lt;span class="caps"&gt;HTML&lt;/span&gt; and seeing the event handler code inline, but it at least gets the job done. Any other tips for debugging events, please share in the comments!&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/h-PlT1Eq1-s" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-04-14:691</id>
    <published>2009-04-14T19:44:00Z</published>
    <updated>2009-04-14T19:44:42Z</updated>
    <link href="http://blog.edgecase.com/2009/4/14/show-off-your-mockups" rel="alternate" type="text/html" />
    <title>Show Off Your Mockups</title>
<content type="html">
            &lt;p&gt;Whether you’re a do-it-all designer/developer or if those roles are separate, odds are your typical workflow involves taking a static &lt;span class="caps"&gt;HTML&lt;/span&gt; mockup and wiring it up with all your crazy codes.&lt;/p&gt;


	&lt;p&gt;So, maybe you’ve got an &lt;span class="caps"&gt;HTML&lt;/span&gt; file with a bunch of dummy data in it that you need to demo for a client.  You probably stuck it somewhere in /public because it’s easy.  It’s probably got a list of copy-and-pasted garbage in there, too, so it looks like a “real” page with lots of data.  And of course there’s the header, footer, and other layout cruft you had to add so it fits the look of the rest of the application.&lt;/p&gt;


	&lt;p&gt;You’ve gotten some feedback from your client, and made some tweaks to the design.  It’s looking pretty good.  You’re feeling pretty good.  Now it’s time to wire that page into the application.&lt;/p&gt;


	&lt;p&gt;Still feeling good?&lt;/p&gt;


	&lt;p&gt;This is the part that sounds so easy until you have to dive in and do it.  It’s painful and tedious, but the good news is, it doesn’t have to be.  It’s time to let &lt;a href="http://github.com/adamlogic/showoff"&gt;Showoff&lt;/a&gt; come to your rescue.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  script/plugin install git://github.com/adamlogic/showoff.git
  script/generate showoff
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This plugin is an extraction from a recent project where I got tired of dealing with this same old scenario.  I wanted  a place where I could stick mockups without thinking about it.  I wanted my mockups to use the same layout, &lt;span class="caps"&gt;CSS&lt;/span&gt;, and JavaScript that the rest of my application does.  I wanted to be able to use Ruby in my mockups to have access to looping, variables, and data generation (&lt;a href="http://faker.rubyforge.org/"&gt;Faker&lt;/a&gt;).  I wanted partials in my mockups.  And I wanted a single place to go where the client and development team could see all of the mockups.&lt;/p&gt;


	&lt;p&gt;After installing and generating Showoff, navigate to /mockups in your app to see some examples.  To make your own, just create a file in app/views/mockups.  You can use &lt;span class="caps"&gt;HTML&lt;/span&gt;, ERB, or any other templating language (such as &lt;span class="caps"&gt;HAML&lt;/span&gt;) that your application supports.  Yes, it really is that simple.&lt;/p&gt;


	&lt;p&gt;Here is an example in a fresh Rails app with Ryan Bates’ &lt;a href="http://github.com/ryanb/nifty-generators"&gt;nifty_layout&lt;/a&gt; applied:&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://blog.edgecase.com/assets/2009/4/14/Mockups-1.jpg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;I’m sure Showoff isn’t the first plugin to attempt to simplify this process, but I can tell you that it’s working really well for us. Check it out and let us know what you think.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; I should also add that this plugin requires Rails 2.3, since it makes use of an engine-style plugin.  This basically lets you embed one “mini app” inside of your main app.  It’s pretty powerful, and I’ll write more about my experience of extracting this plugin in a a future post.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/qc-7l-gm0Ts" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-04-06:676</id>
    <published>2009-04-06T18:53:00Z</published>
    <updated>2009-04-06T19:00:52Z</updated>
    <link href="http://blog.edgecase.com/2009/4/6/see-your-css-changes-in-real-time-in-any-browser" rel="alternate" type="text/html" />
    <title>See Your CSS Changes in Real-Time in Any Browser</title>
<content type="html">
            &lt;p&gt;I’ve wanted to scratch this itch for a while, and this morning I decided to steal a few hours to give it a shot. Most of my &lt;span class="caps"&gt;CSS&lt;/span&gt; authoring and editing takes place in Firefox, using Web Developer Toolbar. For me, seeing &lt;span class="caps"&gt;CSS&lt;/span&gt; rendered in real-time is the only way to write it. (This is one reason I had to pass on Sass, even though I love Haml, but that’s a topic for another time.)&lt;/p&gt;


	&lt;p&gt;This has come major shortcomings, though, since Web Developer Toolbar is not intended to be a text editor (nor should it be). The biggest pain is saving your changes. A save button is provided, but you have to navigate through your filesystem to find the right location. If you’re lucky this location will be remembered next time you save. Support for find and undo is shaky at best, and as a Vim user, there about a thousand other features that I “need” in my text editor.&lt;/p&gt;


	&lt;p&gt;The other problem with Web Developer Toolbar is that it is Firefox-specific. On days that I have to debug my &lt;span class="caps"&gt;CSS&lt;/span&gt; in IE (my “hating life” days), I’m back to the refresh button.&lt;/p&gt;


	&lt;p&gt;Today I may have found a solution.&lt;/p&gt;


	&lt;p&gt;I say “may” because I literally just wrote this. I’ve tested it a little bit, but I can’t claim to have used it for real project work. Nonetheless, I’m pretty damn excited. Twenty minutes ago I had three browsers open (IE, Safari, and Firefox), and saw all three of them update as I saved my &lt;span class="caps"&gt;CSS&lt;/span&gt; file in Vim.&lt;/p&gt;


	&lt;p&gt;Check it out on &lt;a href="http://github.com/adamlogic/auto_update_stylesheets/tree/master"&gt;Github&lt;/a&gt; and let me know what you think. As long as you’re using jQuery, using it is as simple as dropping in the .js file and calling $.autoUpdateStylesheets.&lt;/p&gt;


	&lt;p&gt;My next steps are to pull this into a bookmarklet and remove the jQuery dependency. Then it’s just a matter of pushing a button and editing away in your favorite text editor. Enjoy.&lt;/p&gt;


	&lt;p&gt;(oh, and this should work with Sass, too, so I guess it’s time for me to take another look.)&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/1TcEJ2wAAE4" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-03-30:656</id>
    <published>2009-03-30T17:41:00Z</published>
    <updated>2009-03-31T13:34:05Z</updated>
    <link href="http://blog.edgecase.com/2009/3/30/seeding-your-development-database-with-faker-and-factory-girl" rel="alternate" type="text/html" />
    <title>Seeding Your Development Database with Faker and Factory Girl</title>
<content type="html">
            &lt;p&gt;For the longest time, I’ve used fixtures as a means to seed some basic data in my development environment.  The goal is to put the application in a useable state without having to manually create data (such as users or anything else required to run the app).  In my most recent project, however, I’ve completely abandoned fixtures, and I doubt I’ll ever look back.&lt;/p&gt;


	&lt;p&gt;I felt two kinds of pain with fixture data – the pain of maintaining it and the lack of quality data.&lt;/p&gt;


	&lt;p&gt;The pain of maintaining fixtures was usually bad enough (and I was lazy enough) that I typically just ignored it altogether.  With every change to the schema, more data had to be added or changed.  And not just one place, since a fixture usually contains several records.  I just don’t have the patience for it.&lt;/p&gt;


	&lt;p&gt;Due to my lack of patience, fixture data in any of my projects has been completely unreliable.  I’ll leave out fields that end up being required.  Or I’ll only put one record in a fixture file because I’m too lazy bother with more.&lt;/p&gt;


	&lt;p&gt;I put up with this pain for too long.  The ability to quickly seed a development database with valid, useable data is a big deal, and I was done ignoring the problem.  Enter Faker and Factory Girl.&lt;/p&gt;


	&lt;p&gt;You probably already know what &lt;a href="http://github.com/thoughtbot/factory_girl"&gt;Factory Girl&lt;/a&gt; is, but in case you don’t, it’s just another factory implementation, and it happens to be my preferred implementation at the moment.  If you prefer another, I’m sure it will work just as well with what I’m describing here.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://faker.rubyforge.org/"&gt;Faker&lt;/a&gt; is a Ruby library for generating pseudo-random data.  Typically, this is exactly the kind of data that I want in my development database.  Repeatability isn’t that important.  Faker will generate names for people and business, addresses, phone numbers, Lorem text, &lt;a href="http://faker.rubyforge.org/rdoc/classes/Faker/Company.html#M000024"&gt;BS&lt;/a&gt;, and more.  I use Faker in my factories as a template for pseudo-random models:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
Factory.define :user do |u|
  u.email { Factory.next :email }
  u.email_confirmation { |u| u.email }
  u.password 'test'
  u.password_confirmation { |u| u.password }
  u.first_name 'Test'
  u.last_name 'User'
end

Factory.define :user_sample, :parent =&amp;gt; :user do |u|
  u.first_name { Faker::Name.first_name }
  u.last_name { Faker::Name.last_name }
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here, the ‘user’ factory is used in tests.  It doesn’t use Faker, because I need my tests to be consistent and repeatable.  The ‘user_sample’ factory, however, inherits the ‘user’ factory and uses Faker to generate the name.&lt;/p&gt;


	&lt;p&gt;Once I have my factories ready to go, I create a custom Rake task to seed my development database:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
namespace :db do
  desc 'Provide a base load of randomly generated (but valid) data for development'
  task :seed =&amp;gt; [:reset, 'fixtures:load'] do

    # generate users
    users = []
    6.times { users &amp;lt;&amp;lt; Factory(:user_sample) }

    users &amp;lt;&amp;lt; Factory(:user, :email =&amp;gt; 'user@edgecase.com', :role =&amp;gt; 'user')
    users &amp;lt;&amp;lt; Factory(:user, :email =&amp;gt; 'admin@edgecase.com', :role =&amp;gt; 'admin')

    # generate orders
    20.times { Factory(:order_sample, :user =&amp;gt; users.rand) }

    # login instructions
    puts "\n**************\n\nThe following accounts are available for use:\n\n" 
    puts '  user@edgecase.com (password: test)'
    puts '  admin@edgecase.com (password: test)'
    puts "\n**************\n\n" 

  end
end
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;With this in place, I can run &lt;code&gt;rake db:seed&lt;/code&gt;, and I’ll get a nicely bootstrapped development database to poke around in.  Each order will belong to a random user with a random name.    It’ll be different every time you run it.  It’s like a box of chocolates.  Or something.&lt;/p&gt;


	&lt;p&gt;Want more users?  Change 6 to 60.  Lovely.&lt;/p&gt;


	&lt;p&gt;Notice that db:seed still has db:fixtures:load as a prerequisite.  This means that fixtures are still an option if I want to combine them with my custom seed task.  Nice as that sounds, I haven’t used it.  Fixtures are, for me, but a memory.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/Lt0ccZvzoak" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>Adam</name>
    </author>
    <id>tag:blog.edgecase.com,2009-03-23:621</id>
    <published>2009-03-23T16:15:00Z</published>
    <updated>2009-03-23T16:25:27Z</updated>
    <link href="http://blog.edgecase.com/2009/3/23/better-flash-and-validation-messages" rel="alternate" type="text/html" />
    <title>Better Flash and Validation Messages</title>
<content type="html">
            &lt;p&gt;In Rails, there are two common types of messages that we show on a page.  Flash messages, which are page-level messages telling the user that something has happened.  And validation messages, which are specific to a form that the user filled out (actually, they are specific to a model, but we’ll focus on the user’s point of view).&lt;/p&gt;


	&lt;p&gt;I rarely see these messages presented in a way that satisfies me.  My two big complaints are separation and language.&lt;/p&gt;


	&lt;h2&gt;Separation&lt;/h2&gt;


	&lt;p&gt;I’ve always been bothered at typical Rails apps (including many of ours) that separate flash messages from validation messages, often with completely different styling.  In my mind they are slight variations of the same thing: informational messages for the user.  They should be shown together, in a consistent location, and with the same styling.&lt;/p&gt;


	&lt;p&gt;The reason they are typically separate is purely implementation.  Flash messages are set in controllers in usually spit out in an application layout file.  Validation messages come from model objects and are usually spit out within forms.  Rails makes it very easy to do this, and many Rails tutorials promote this separation.&lt;/p&gt;


	&lt;p&gt;Come together, right now…&lt;/p&gt;


	&lt;p&gt;Just because it’s easy doesn’t make it right, but I’d much rather have both.  Recently I’ve found the &lt;a href="http://github.com/railsgarden/message_block"&gt;message_block&lt;/a&gt; plugin that combines flash and validation messages and spits them out in a single div.  It’s easy to use and does exactly what it claims.  The &lt;span class="caps"&gt;README&lt;/span&gt; is pretty self explanatory, so go check it out.&lt;/p&gt;


	&lt;h2&gt;Language&lt;/h2&gt;


	&lt;p&gt;The validation messages that come packaged with Rails are very convenient.  Purely based on the column name and the validation method, Rails can generate an error message that is logically correct.  Unfortunately, they are rarely user-friendly.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;“User Experience is invalid.”&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;blockquote&gt;
		&lt;p&gt;“Please is not included in the list.”&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;blockquote&gt;
		&lt;p&gt;“Context can’t be blank.”&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;These are all examples of out-of-the-box validation messages.  If you don’t see a problem with them, I probably can’t convince you there is.  It’s totally a matter of personal preference.&lt;/p&gt;


	&lt;p&gt;Now, you’re probably thinking that there is a built-in way to customize validation messages, and you’re half right.  The problem is that you can’t customize the full message.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;validates_presence_of :city, :message =&amp;gt; 'Please enter the city.'&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;This code will produce the following message:&lt;/p&gt;


	&lt;p&gt;“City Please enter the city.”&lt;/p&gt;


	&lt;p&gt;Uh oh.  Rails only lets you customize the latter half of the error message.  In many cases, and for many developers, this is good enough.  But not me.  Much like a designer wants control over every pixel, I demand control over every word.  Enter &lt;a href="http://github.com/gumayunov/custom-err-msg"&gt;custom-err-msg&lt;/a&gt;.  With this plugin, I can have full control over the message by prepending ’^’.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;validates_presence_of :city, :message =&amp;gt; '^Please enter the city.'&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Problem solved.&lt;/p&gt;


	&lt;p&gt;These are two plugins I’ll be using in every project from here on out.  I think my users will appreciate it.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/qwPxVb3IClQ" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>joe</name>
    </author>
    <id>tag:blog.edgecase.com,2009-01-26:270</id>
    <published>2009-01-26T20:54:00Z</published>
    <updated>2009-01-26T20:59:17Z</updated>
    <link href="http://blog.edgecase.com/2009/1/26/learning-ruby-through-testing" rel="alternate" type="text/html" />
    <title>Learning Ruby Through Testing</title>
<content type="html">
            &lt;p&gt;We have finally published the Ruby Koans to github at &lt;a href="//github.com/edgecase/ruby_koans"&gt;edgecase/ruby_koans&lt;/a&gt; If you do not have a github account or use git, do not worry, you can click the download button on that page to receive a zip file containing the latest version of the source.&lt;/p&gt;


	&lt;p&gt;The idea was born thanks to &lt;a href="//www.clarkware.com/cgi/blosxom/2005/03/18"&gt;Mike Clark’s blog post&lt;/a&gt; about learning Ruby through unit testing.  It teaches both language and culture in a self-paced manner.&lt;/p&gt;


	&lt;p&gt;We look forward to comments and any feedback you have.  Enjoy!&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/vUomJ-J5fEg" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>joe</name>
    </author>
    <id>tag:blog.edgecase.com,2009-01-14:268</id>
    <published>2009-01-14T01:58:00Z</published>
    <updated>2009-01-14T02:01:45Z</updated>
    <link href="http://blog.edgecase.com/2009/1/14/codemash-wrap-up-coming" rel="alternate" type="text/html" />
    <title>CodeMash wrap up coming</title>
<content type="html">
            &lt;p&gt;We promised a posting on our Ruby stuff from CodeMash and it is not here yet.  We are polishing a few items for the Koans and will post them up when they are finished.&lt;/p&gt;


	&lt;p&gt;If they are not up by Friday you have permission to harass us :-).&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/iPGAA-2jUwE" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>ehren</name>
    </author>
    <id>tag:blog.edgecase.com,2008-12-12:263</id>
    <published>2008-12-12T21:53:00Z</published>
    <updated>2008-12-12T22:06:42Z</updated>
    <category term="burn out" />
    <category term="pong" />
    <link href="http://blog.edgecase.com/2008/12/12/when-burnt-out" rel="alternate" type="text/html" />
    <title>When Burnt Out</title>
<content type="html">
            Goof off. It helps.
&lt;br /&gt;
&lt;img src="http://blog.theedgecase.com/assets/2008/12/12/pong-screen.jpg" alt="pong-screen" /&gt;
&lt;br /&gt;
Written by me. Firefox 3 only. Control and shift for the left paddle, arrow keys for the right paddle.
&lt;br /&gt;
&lt;a href="http://blog.theedgecase.com/assets/2008/12/12/pong.zip"&gt;download src (html and javascript)&lt;/a&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/Zfm2-_F_PGg" height="1" width="1"/&gt;</content>  </entry>
  <entry xml:base="http://blog.edgecase.com/">
    <author>
      <name>nick</name>
    </author>
    <id>tag:blog.edgecase.com,2008-10-31:258</id>
    <published>2008-10-31T13:12:00Z</published>
    <updated>2009-04-07T15:17:30Z</updated>
    <category term="before_destroy,documentation" />
    <link href="http://blog.edgecase.com/2008/10/31/damn-the-standards-and-full-read-the-documentation" rel="alternate" type="text/html" />
    <title>Damn The Standards And Full Read The Documentation!</title>
<content type="html">
            &lt;p&gt;Do not let conventions prevent correct behaviour.&lt;/p&gt;


	&lt;p&gt;Often within an ActiveRecord::Base model, statements of a certain ilk are grouped together.  For example the single line validations are often in the same place.  It is tempting to place additional statements such that the model file is consistent.  One might put all the &lt;tt&gt;before_destroy&lt;/tt&gt; callback macros near the &lt;tt&gt;before_save&lt;/tt&gt; or &lt;tt&gt;after_save&lt;/tt&gt; statements.&lt;/p&gt;


	&lt;p&gt;Without loss of generality the following example models illustrate a behavioural anomaly that was encountered.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
ActiveRecord::Schema.define(:version =&amp;gt; 1) do
  create_table "accounts", :force =&amp;gt; true do |t|
  end

  create_table "invoices", :force =&amp;gt; true do |t|
    t.integer "account_id" 
  end

  create_table "receipts", :force =&amp;gt; true do |t|
    t.integer "account_id" 
  end
end

class Account &amp;lt; ActiveRecord::Base
  has_many :receipts, :dependent =&amp;gt; :destroy
  has_many :invoices, :dependent =&amp;gt; :destroy
  before_destroy :ensure_no_invoices
  def ensure_no_invoices
    return true if invoices.empty?
    false
  end
end

class Invoice &amp;lt; ActiveRecord::Base
  belongs_to :account
end

class Receipt &amp;lt; ActiveRecord::Base
  belongs_to :account
end
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;The intent is to prevent the destruction of an account and related models when the account still has at least one invoice.  The above code is how &lt;strong&gt;not&lt;/strong&gt; to do it.  The following sandboxed example demonstrates that the account will not be destroyed but that associated models will be.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
test_before_destroy&amp;gt; ./script/console --sandbox
Loading development environment in sandbox (Rails 2.1.1)
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 1&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 1, account_id: 1&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 1, account_id: 1&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
=&amp;gt; false
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 0, 0]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;The Rails documentation on &lt;tt&gt;before_destroy&lt;/tt&gt; can be found on the &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html"&gt;callbacks&lt;/a&gt; entry.  There is a vital clue stated there.&lt;/p&gt;


&lt;pre&gt;
*IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the callbacks before specifying the associations.  Otherwise, you might trigger the loading of a child before the parent has registered the callbacks and they wonâ€˜t be inherited.
&lt;/pre&gt;

	&lt;p&gt;The documentation mentions only “inheritance” but further experimentation reveals that the desired behaviour of not destroying a model and related models is obtained when the documentation is heeded.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
class Account &amp;lt; ActiveRecord::Base
  before_destroy :ensure_no_invoices
  has_many :receipts, :dependent =&amp;gt; :destroy
  has_many :invoices, :dependent =&amp;gt; :destroy
  def ensure_no_invoices
    return true if invoices.empty?
    false
  end
end
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Here the &lt;tt&gt;before_destroy&lt;/tt&gt; macro is written before the associations.  This &lt;strong&gt;is&lt;/strong&gt; how to do it.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
test_before_destroy&amp;gt; ./script/console --sandbox
Loading development environment in sandbox (Rails 2.1.1)
Any modifications you make will be rolled back on exit
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 2&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 2, account_id: 2&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 2, account_id: 2&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
=&amp;gt; false
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Interleaving the &lt;tt&gt;before_destroy&lt;/tt&gt; macro between the associations allows the destruction of one associated model but not the other.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
class Account &amp;lt; ActiveRecord::Base
  has_many :receipts, :dependent =&amp;gt; :destroy
  before_destroy :ensure_no_invoices
  has_many :invoices, :dependent =&amp;gt; :destroy
  def ensure_no_invoices
    return true if invoices.empty?
    false
  end
end

test_before_destroy&amp;gt; ./script/console --sandbox
Loading development environment in sandbox (Rails 2.1.1)
Any modifications you make will be rolled back on exit
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 3&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 3, account_id: 3&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 3, account_id: 3&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
=&amp;gt; false
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 0]

class Account &amp;lt; ActiveRecord::Base
  has_many :invoices, :dependent =&amp;gt; :destroy
  before_destroy :ensure_no_invoices
  has_many :receipts, :dependent =&amp;gt; :destroy
  def ensure_no_invoices
    return true if invoices.empty?
    false
  end
end
test_before_destroy&amp;gt; ./script/console --sandbox
Loading development environment in sandbox (Rails 2.1.1)
Any modifications you make will be rolled back on exit
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 4&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 4, account_id: 4&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 4, account_id: 4&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
=&amp;gt; false
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 0, 1]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Note that when the &lt;tt&gt;before_destroy&lt;/tt&gt; macro is declared after the associations and the specified check raises an exception, the &lt;tt&gt;script/console&lt;/tt&gt; sandboxed environment does not rollback the destruction of the associated models because of the transactional nature of the entire &lt;tt&gt;script/console&lt;/tt&gt; session.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
class Account &amp;lt; ActiveRecord::Base
  has_many :receipts, :dependent =&amp;gt; :destroy
  has_many :invoices, :dependent =&amp;gt; :destroy
  before_destroy :ensure_no_invoices
  def ensure_no_invoices
    return true if invoices.empty?
    raise RuntimeError.new('Attempted to destroy account that still had invoices!')
  end
end
test_before_destroy&amp;gt; ./script/console --sandbox
Loading development environment in sandbox (Rails 2.1.1)
Any modifications you make will be rolled back on exit
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 5&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 5, account_id: 5&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 5, account_id: 5&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
RuntimeError: Attempted to destroy account that still had invoices!
        from ... (irb):6&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 0, 0]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;When &lt;tt&gt;script/console&lt;/tt&gt; is executed without the sandbox option, the effects of a rollback can be observed upon the attempted destruction of an account with invoices.&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
test_before_destroy&amp;gt; ./script/console          
Loading development environment (Rails 2.1.1)
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [0, 0, 0]
&amp;gt;&amp;gt; account = Account.create
=&amp;gt; #&amp;lt;Account id: 6&amp;gt;
&amp;gt;&amp;gt; account.invoices.create
=&amp;gt; #&amp;lt;Invoice id: 6, account_id: 6&amp;gt;
&amp;gt;&amp;gt; account.receipts.create
=&amp;gt; #&amp;lt;Receipt id: 6, account_id: 6&amp;gt;
&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&amp;gt;&amp;gt; account.destroy
RuntimeError: Attempted to destroy account that still had invoices!
        from ... (irb):6&amp;gt;&amp;gt; [Account.count, Invoice.count, Receipt.count]
=&amp;gt; [1, 1, 1]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Read the documentation.&lt;/p&gt;


	&lt;p&gt;Put the &lt;tt&gt;before_destroy&lt;/tt&gt; macro before associations.&lt;/p&gt;


	&lt;p&gt;The sandbox option in &lt;tt&gt;script/console&lt;/tt&gt; may mask rollbacks.&lt;/p&gt;
          &lt;img src="http://feeds.feedburner.com/~r/edgecase/~4/nnNdwSwUtL4" height="1" width="1"/&gt;</content>  </entry>
</feed>
