Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bid < ActiveRecord::Base
- belongs_to :project, :counter_cache => true
- belongs_to :user
- validates_presence_of :project_id
- validates_presence_of :user_id
- validates_value_of :amount, :within => 1..10000
- end
- class Project < ActiveRecord::Base
- has_many :bids
- belongs_to :user, :counter_cache => true
- validates_presence_of :name
- validates_length_of :name, :within => 5..100
- validates_length_of :description, :within => 10..3000
- cattr_reader :per_page
- @@per_page = 50
- end
- class User < ActiveRecord::Base
- attr_accessor :password
- has_many :projects
- has_many :bids
- has_many :ratings
- # associations
- validates_length_of :email, :within => 3..100
- validates_uniqueness_of :email, :case_sensitive => false
- validates_presence_of :name
- validates_length_of :password, :within => 4..40
- validates_confirmation_of :password
- before_save :encrypt_password
- def self.encrypt(pass, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{pass}--")
- end
- def self.authenticate(email, pass)
- user = find_by_email(email)
- user && user.authenticated?(pass) ? user : nil
- end
- def authenticated?(pass)
- encrypted_password == User.encrypt(pass, salt)
- end
- protected
- def encrypt_password
- return if password.blank?
- if new_record?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{email}--")
- end
- self.encrypted_password = User.encrypt(password, salt)
- end
- public
- def password_required
- encrypted_password.blank? || !password.blank?
- end
- end
Add Comment
Please, Sign In to add comment