Alpha Blue wrote:
> Marnen Laibow-Koser wrote:
>> 
>> But we shouldn't.  If you have a good reason, I'd like to hear it.
>> 
> 
> It's a personal preference.  Some people put custom validators in libs 
> and others do it in initializers.
> 
> I decided to use initializers more because it makes sense for validators 
> and also because I had originally learned to do so right here:
> 
> http://guides.rubyonrails.org/activerecord_validations_callbacks.html
> 
> According to rubyonrails.org right in their 
> activerecord_validations-callbacks they state:
> 
> ============= QUOTE from rubyonrails.org ==================
> You can even create your own validation helpers and reuse them in 
> several different models. Here is an example where we create a custom 
> validation helper to validate the format of fields that represent email 
> addresses:
> 
> ActiveRecord::Base.class_eval do def self.validates_as_radio(attr_name, 
> n, options={}) validates_inclusion_of attr_name, {:in => 
> 1..n}.merge(options) end end
> 
> Simply reopen ActiveRecord::Base and define a class method like that. 
> You’d typically put this code somewhere in config/initializers.
> 
> ===========================================================
> 
> Some items don't need to go into initializers but again, it's more of an 
> organizational approach to rails and if you can show me 100% that both 
> rubyonrails.org and me are wrong, please do so.

Interesting.  I wasn't aware that that was in the guides.  My 
interpretation from reading the guide, however, is that while that may 
be good for the case described in the guide -- one custom validator only 
-- you really wouldn't want to do it for anything more involved.  I 
think having long initializer files is probably a code smell in most 
cases.

Frankly, I think even for one custom validator this is probably a bad 
idea.  I think whoever wrote that part of the guides really dropped the 
ball, unless he or she was simply trying for initial simplicity without 
elegance.

Also, I think you're cookbooking the guides a little too much -- that 
is, taking their solutions as gospel even for cases they didn't really 
consider.  You'll note that the guide never mentions the case of 
multiple validators.  Remember, sample code is just that -- sample code, 
not scripture.

In short: I see good reasons *not* to do it the way the guide does -- 
it's sloppy and duplicates Ruby's module system for no good reason.  I 
see not a single good reason to do it the way the guide does.

Do you have such a reason, other than "because the guide says so"?

> 
> Thanks mate. :)

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to