Guest User

Untitled

a guest
Jun 1st, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. class Bid < ActiveRecord::Base
  2. belongs_to :project, :counter_cache => true
  3. belongs_to :user
  4. validates_presence_of :project_id
  5. validates_presence_of :user_id
  6. validates_value_of :amount, :within => 1..10000
  7. end
  8.  
  9. class Project < ActiveRecord::Base
  10. has_many :bids
  11. belongs_to :user, :counter_cache => true
  12. validates_presence_of :name
  13. validates_length_of :name, :within => 5..100
  14. validates_length_of :description, :within => 10..3000
  15. cattr_reader :per_page
  16. @@per_page = 50
  17. end
  18.  
  19. class User < ActiveRecord::Base
  20. attr_accessor :password
  21. has_many :projects
  22. has_many :bids
  23. has_many :ratings
  24.  
  25. # associations
  26. validates_length_of :email, :within => 3..100
  27. validates_uniqueness_of :email, :case_sensitive => false
  28. validates_presence_of :name
  29. validates_length_of :password, :within => 4..40
  30. validates_confirmation_of :password
  31.  
  32. before_save :encrypt_password
  33.  
  34. def self.encrypt(pass, salt)
  35. Digest::SHA1.hexdigest("--#{salt}--#{pass}--")
  36. end
  37.  
  38. def self.authenticate(email, pass)
  39. user = find_by_email(email)
  40. user && user.authenticated?(pass) ? user : nil
  41. end
  42.  
  43. def authenticated?(pass)
  44. encrypted_password == User.encrypt(pass, salt)
  45. end
  46.  
  47. protected
  48.  
  49. def encrypt_password
  50. return if password.blank?
  51. if new_record?
  52. self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{email}--")
  53. end
  54. self.encrypted_password = User.encrypt(password, salt)
  55. end
  56.  
  57. public
  58. def password_required
  59. encrypted_password.blank? || !password.blank?
  60. end
  61. end
Add Comment
Please, Sign In to add comment