PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/identity/email.rb

https://github.com/bountyhill/bountyhill
Ruby | 50 lines | 27 code | 17 blank | 6 comment | 1 complexity | e01cd3285204e88803e959040482cc5d MD5 | raw file
  1. # encoding: UTF-8
  2. class Identity::Email < Identity
  3. include Identity::PolymorphicRouting
  4. attr_accessor :password_new, :password_new_confirmation
  5. with_metrics! "accounts.email"
  6. after_create :send_confirmation_email
  7. attr_accessible :user, :name, :email, :password, :password_confirmation, :newsletter_subscription
  8. has_secure_password
  9. # -- validation -----------------------------------------------------
  10. # constant to use with password validation
  11. MIN_PASSWORD_LENGTH = 6
  12. # constant to use with email validation
  13. EMAIL_ADDRESS_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i
  14. validates :email, :presence => true, :format => { :with => EMAIL_ADDRESS_REGEX }, :uniqueness => { :case_sensitive => false }
  15. validates :password, :length => { :minimum => MIN_PASSWORD_LENGTH }
  16. # -- authenticate ---------------------------------------------------
  17. def self.authenticate(email, password)
  18. identity = find_by_email(email)
  19. identity.authenticate(password) if identity
  20. end
  21. # -- methods --------------------------------------------------------
  22. def confirmed?
  23. confirmed_at.present?
  24. end
  25. def confirm!(flag)
  26. Identity::Email.update_all({:confirmed_at => flag ? Time.now : nil}, :id => id)
  27. reload
  28. end
  29. private
  30. def send_confirmation_email
  31. Deferred.mail UserMailer.confirm_email(user)
  32. end
  33. end