Reusing Rails 2.2.2 strip_tag view helper

In one of my Rails projects, I have this one class that strips HTML from some user submitted text. Instead of reinventing the wheel, I reused the Rails view helper strip_tags() by including the relevant module:

class Presenter include ActionView::Helpers::SanitizeHelper end

This worked great until I upgraded the Rails framework to v2.2.2. The following error began appearing:

undefined method `full_sanitizer' for Presenter:Class

Turned out it was easy to fix. Looking at the strip_tags method in actionpack/lib/action_view/helpers/sanitize_helper.rb:

def strip_tags(html) self.class.full_sanitizer.sanitize(html) end

It's trying to access a class method that doesn't exist in Presenter. While I included the instance methods, now I also have to extend the necessary class methods.

class Presenter include ActionView::Helpers::SanitizeHelper extend ActionView::Helpers::SanitizeHelper::ClassMethods end

Now all is good.

Ruby on Rails. March 12, 2009 - 09:38PM. 0 Comments

RSpec + Rails 2.2 = Neglected Rescue Handlers

Upgrading a Rails project to version 2.2 broke controller specs and I couldn't figure out why. The problem i was facing was that the default Rails exception rescuing would not work in the test environment.

I'm seeing alot of something like the following with 'rake spec':

ActiveRecord::RecordNotFound in 'PagesController responding to GET show should not find the record' ActiveRecord::RecordNotFound

An exception is raised during the page request as expected but it prematurely ends the spec instead of going through the rescue handlers I've setup through rescue_from(). I had no idea if this new behavior is expected or a regression. Sure I could do...

lambda{do_get}.should raise_error(ActiveRecord::RecordNotFound)

...but I wasn't looking forward to updating the spec suite to conform. Also, it wouldn't be testing the rescue handler behaviors. Initially, searching via the almighty Google yield no answers. Stepping through code execution using rdebug revealed that RSpec modifies Rails to not rescue exceptions as it normally would. However, the modification also includes a simple way to revert to the default behavior. Score! So now I'm doing:

before(:each) do controller.use_rails_error_handling! end

With that solved, I now know which search terms to use and, lo and behold, Google reveals that somebody had the same problem and already figured it out. Google, why had you forsaken me earlier?

Also, there is more context if you're interested.
Ruby on Rails. December 04, 2008 - 09:01AM. 0 Comments

Sharkey 20.0 20.0 20.0 20.0

Still got a laugh out of this classic comic years since it came out. Check out the rest of his site though unfortunately there isn't a full archive of his work.

Misc. November 26, 2008 - 04:36AM. 0 Comments

Thumbs Up: Pixel Perfect Firefox Extension

Take a look at the Firefox extension Pixel Perfect if you implement webpages based off Photoshop mockups. It overlays a transparent image of your choice over the webpage so that out of place elements are easy to spot.

It's currently at version 0.6.1 but it has plenty of potential. I could see how it could be integral to the workflow but at the moment it's great for quick checks.

Web. November 02, 2008 - 12:38PM. 0 Comments

Alias Tip

alias for teh win! Didn't get into the habit until now because I thought saving a few keystrokes wasn't that beneficial. But damn it helps with a few edge cases and now I'm addicted.

In my ~/.profile:

# For managing encrypted FileVault containing my Rails products. # '-owners on' so the owner and groups can be set on files. alias mount_rails="hdiutil attach /Users/vwoo/Documents/Knox/rails.sparseimage -owners on" alias umount_rails="hdiutil detach /Volumes/rails" # alias now takes the place of bash scripts I made for several # server software installed through MacPorts. # Arguments work like bash scripts. $1 is the first argument etc. # so I can run 'apache start' and 'apache stop' alias apache="sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper $1" alias postgresql="sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql82-server/postgresql82-server.wrapper $1"
Misc. October 11, 2008 - 12:27PM. 0 Comments