Guest User

Untitled

a guest
Jul 13th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. has_many :reviews
  3.  
  4. acts_as_url :handle
  5.  
  6. def to_param
  7. handle
  8. end
  9.  
  10. has_attached_file :logo, :styles => { :thumb => '100x100', :small => '250x250>'}
  11.  
  12. validates_presence_of :email
  13. validates_presence_of :name
  14.  
  15. validates_uniqueness_of :email
  16. validates_uniqueness_of :handle
  17.  
  18. attr_accessor :password_confirmation
  19.  
  20. validates_confirmation_of :password
  21. validate :password_non_blank
  22.  
  23. def self.authenticate(email, password)
  24. user = self.find_by_email(email)
  25. if user
  26. expected_password = encrypted_password(password, user.salt)
  27. if user.hashed_password != expected_password
  28. user = nil
  29. end
  30. end
  31. user
  32. end
  33.  
  34. def password
  35. @password
  36. end
  37.  
  38. def password=(pwd)
  39. @password = pwd
  40. return if pwd.blank?
  41. create_new_salt
  42. self.hashed_password = User.encrypted_password(self.password, self.salt)
  43. end
  44.  
  45. private
  46.  
  47. def password_non_blank
  48. errors.add(:password, "Missing password" ) if hashed_password.blank?
  49. end
  50.  
  51. def self.encrypted_password(password, salt)
  52. string_to_hash = password + "estate" + salt
  53. Digest::SHA1.hexdigest(string_to_hash)
  54. end
  55.  
  56. def create_new_salt
  57. self.salt = self.object_id.to_s + rand.to_s
  58. end
  59.  
  60. end
Add Comment
Please, Sign In to add comment