F1a42e15a7d426b8e717687794e0234b

I've never run into a situation where I wanted to use a lot of conditions on my validations. The way I'm doing it seems a little ridiculous.

in models/user.rb
...  

  validates_presence_of     :email,                      :if => :email_validations_required?
  validates_presence_of     :password,                   :if => :password_validations_required?
  validates_presence_of     :password_confirmation,      :if => :password_validations_required?
  validates_length_of       :password, :within => 4..40, :if => :password_validations_required?
  validates_confirmation_of :password,                   :if => :password_validations_required?
  validates_length_of       :nick,    :maximum => 40,    :if => :nick_validations_required?
  validates_length_of       :email,    :within => 3..100,:if => :email_validations_required?
  validates_format_of       :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, 
    :on => :create, :message => "doesn't seem to be valid.",
                                                         :if => :email_validations_required?
  validates_format_of       :nick, :with => /\A[^@]+\Z/i,:if => :nick_validations_required?,
    :message => "can't have a @ in it.  (Sorry, 1337 |-|@><0R5!)"

  validates_uniqueness_of   :login,                      :if => :nick_validations_required?
  validates_uniqueness_of   :email, :case_sensitive => false, :if => :email_validations_required?
...


def password_validations_required?
    (crypted_password.blank? && !using_openid?) || !password.blank?
  end

  def email_validations_required?
    !email.blank? || !using_openid?
  end
  
  def nick_validations_required?
    !nick.blank?
  end

Refactorings

No refactoring yet !

92772ff5353c89d9bd10f8e334161e16

Ben Hughes

May 31, 2008, May 31, 2008 21:41, permalink

1 rating. Login to rate!

You can stop repeating the :if conditions using with_options, something that restful_authentication does not do by default.

with_options :if => :password_validations_required? do
  validates_presence_of     :password
  validates_presence_of     :password_confirmation
  validates_length_of       :password, :within => 4..40
  validates_confirmation_of :password
end
F1a42e15a7d426b8e717687794e0234b

mitch

June 3, 2008, June 03, 2008 00:42, permalink

No rating. Login to rate!

Thanks so much for the help! I knew that rails must have a way to deal with this but I didn't see it in the documentation.

F1a42e15a7d426b8e717687794e0234b

mitch

June 6, 2008, June 06, 2008 01:15, permalink

2 ratings. Login to rate!

One little change. This wasn't working for me and I found a small change from the documentation. Just passing something in the block.

with_options :if => :password_validations_required? do |p|
  p.validates_presence_of     :password
  p.validates_presence_of     :password_confirmation
  p.validates_length_of       :password, :within => 4..40
  p.validates_confirmation_of :password
end

Your refactoring





Format Copy from initial code

or Cancel