Created
May 19, 2011 09:23
-
-
Save anonymous/980468 to your computer and use it in GitHub Desktop.
Special password validation for devise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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