Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2011 09:23
Show Gist options
  • Save anonymous/980468 to your computer and use it in GitHub Desktop.
Save anonymous/980468 to your computer and use it in GitHub Desktop.
Special password validation for devise
class AdminToolUser < ActiveRecord::Base
# Setup accessible (or protected) attributes for the model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name
validates_presence_of :first_name, :last_name
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :registerable, :lockable and :timeoutable
devise :database_authenticatable, :recoverable, :rememberable, :trackable
validate :password_must_exist, :password_must_match, :password_length, :if => :password_required?
protected
def password_required?
!persisted? || (password.present? && password_confirmation.present?)
end
def password_must_exist
errors.add(:password, "can't be blank") if password.blank?
end
def password_must_match
errors.add(:password, "doesn't match confirmation") if password != password_confirmation
end
def password_length
errors.add(:password, "must be at least 6 characters long (and less than 30)") if (password.split(//).size < 6 || password.split(//).size > 29)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment