Change is coming to how we get the radio we listen too. Radio listeners will go from having 10 choices on their radios to thousands. Good times, good times.

Thursday, January 19, 2006

validator mixins

There was a proposal on the Rails mailing list this morning about divorcing the validation code from ActiveRecord, and making it its own mixin.

------------------------------------------------------------

It seems to me that Validations are quite useful for many objects, even
ones having nothing to do with ActiveRecord.

Common examples are Contact Us forms, multi-step forms, web service
paramater validations, etc.

I took a look at the source for Validations, and it seems that it is
very independent from ActiveRecord. I caught only three depenedencies:

1. validates_numericy --> depends on ActiveRecords' before_type_case
2. The exceptions were from the ActiveRecord namespace
3. Including Validations hooked on methods like save

It would seem to me that Validations would be best off as an independent
mixin. All ActiveRecord would need to do is mix it, add
validates_numericay (1), catch the exceptions and rethrow them as
ActiveRecord exceptions (2), and hook save type methods (3).

What does everyone say? Would this be a good change? Is there an
important reason for *not* doing this?


------------------------------------------------------------

I've been developing a data storage system based on Ferrett that clones the ActiveRecord interface, and having the ability to mixin validation instead of rolling my own makes my brow twitch like Elvis. "Better than a penut butter, bannana sandwich baby"

I've been calling my search based data storage IndexRecord, and the goal for its interface is simple. Have models subclass IndexRecord instead of ActiveRecord, and have them be interchangable. Currently, a model could be used like this. Imagine the power of mixing in validators.

class Page < IndexedRecord
#setting up searchable fields in Page documents
fields :page_number, :text
end

Then you can do stuff like this -

page = Page.new
page.page_number = 23
page.text = "One flew over the cookoo's nest."
page.save

pages = Page.search("text:cookoo") #gives back a hash of unique control_id and summary

Now, imagine with validators mixed in, this model would be one step closer to AcriveRecord functionality.

No comments: