class Student < ActiveRecord::Base
  self.per_page = 3
  attr_accessor :remember_token
  has_secure_password
  before_save { email.downcase! }

  validates :first_name,
            presence: true,
            length: { maximum: 50 }

  validates :last_name,
            presence: true,
            length: { maximum: 50 }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i

  validates :email,
            presence: true,
            length: { maximum: 255 },
            format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }

  validates :password,
            presence: true,
            length: { minimum: 6 },
            allow_nil: true

  has_many :ratings
  has_many :courses, through: :ratings


# class << self
#   # Returns the hash digest of the given string.
#   def digest(string)
#     cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
#                                                   BCrypt::Engine.cost
#     BCrypt::Password.create(string, cost: cost)
#   end
#
#   # Returns a random token.
#   def new_token
#     SecureRandom.urlsafe_base64
#   end
#
#   # Remembers a student in the database for use in persistent sessions.
#   def remember
#     self.remember_token = Student.new_token
#     update_attribute(:remember_digest, Student.digest(remember_token))
#   end
#
#   # Returns true if the given token matches the digest.
#   def authenticated?(remember_token)
#     return false if remember_digest.nil?
#     BCrypt::Password.new(remember_digest).is_password?(remember_token)
#   end
#
#   # Forgets a student.
#   def forget
#     update_attribute(:remember_digest, nil)
#   end
# end
end
