January 2007

Updated Validateable Recipe

Recipe 64[1] show how to use Rails’ ActiveRecord Validations functionality on objects that don’t have corresponding database records. The canonical example of this situation is a Password (model) class. It helps support the view but there is no actual passwords table in the database.

When I went to use the recipe, I ran into an issue that others have seen. Turns out that Rails has changed since the recipe was written and now the validation functionality is expecting a validateable class to define a class method called human_attribute_name. Looking back at my old projects I notice that this has been the case since at least Spring 2006. Whatever. I tweaked the recipe and now it works w/ my fairly edgy Rails version (I’m on revision 5662 just now). Here’s the code:

    1 module Validateable
    2   [:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
    3   def method_missing(symbol, *params)
    4     if(symbol.to_s =~ /(.*)_before_type_cast$/)
    5       send($1)
    6     end
    7   end
    8   module ClassMethods
    9     def human_attribute_name(attribute_key_name)
   10       attribute_key_name.humanize
   11     end
   12   end
   13   def self.included(base)
   14     base.send(:include, ActiveRecord::Validations)
   15     base.extend(ClassMethods)
   16   end
   17 end

Just put that code into validateable.rb, drop it into your lib directory and put an include Validateable line at the top (inside) of your class definition.
Notice that instead of implementing the append_features class method (as the original recipe did), I’m implementing included as recommended in the Module#append_features documentation[2]. It’s just a little cleaner in that it eliminates the need to invoke super.

One last interesting bit is that I ran into difficulties convincing Ruby to add a class method to the target class. At first I tried simply using this definition:

    1 def self.human_attribute_name(attribute_key_name)

In place of the sub-module ClassMethods and the base.extend... call. I still don’t fully get why that approach wouldn’t work, but after reading Jay Fields’ post on Ruby: instance and class methods from a module I thought I’d give this approach a try — and voilà.
[1] Recipe 64 Validating Non-Active Record Objects in Chad Fowler’s Rails Recipes.

[2] p. 554 of Dave Thomas’ Programming Ruby a.k.a. The Pickaxe Book.

Ruby on Rails

Comments (0)

Permalink

Twelve Days

I’ve been enjoying Crossfit for a few months now through Crossfit Plano. Many of the workouts have names — like “Fran” or “Cindy”. It’s often kind of like circuit training where you’re trying to work your way up to some prescribed weight with good form while minimizing the elapsed time. The workouts are basically as extreme as you want them to be. There is definitely something for everyone.

So I walk into class last Saturday and on the whiteboard is “The Twelve Days of Crossfit“. It’s like the song but you get to give yourself gifts — like handstands and clean-and-jerks. And you don’t wait a day between sets — you try to do the whole thing as fast as you can. Anyway I didn’t do the workout myself but I was intrigued by the sheer number of repetitions involved. Many of us have sung the song — but I wondered just how many “gifts” (repetitions) there were. A trip to Wolfram MathWorld (see Power Sums) and a little noodling yielded the following…

Twelve Days

I’m sure my coach would be more impressed if I could do a proper squat. On the other hand, one must keep oneself entertained.

Crossfit
diversion

Comments (0)

Permalink