Topic: Refactor Field Checks
This is becoming a familiar design pattern for me:
validates_length_of :login, :within => 3..40, :if => :login_is_filled?
validates_length_of :email, :within => 3..100, :if => :email_is_filled?
validates_presence_of :password, :if=>:password_required?
validates_presence_of :password_confirmation, :if => :password_required? && :password_is_filled?
Sometimes those is_filled? methods can get really long and repetitive, so I try to be clever.
validates_length_of :login, :within => 3..40, :if => is_filled?('login')
Of course that fails because
undefined method `is_filled?' for User:Class
Another problem I notice is that the blank? method wont work on something that is not a model attribute (such as password confirmation). Is there any way to get around this?
BTW: This wold be a perfect first post for the new refactoring forum (hint, hint)
Last edited by pimpmaster (2007-08-11 07:06:57)