class UsersBook < ApplicationRecord
  belongs_to :user
  belongs_to :book
  validates_uniqueness_of :book_id, scope: :user_id

  enum status: {
    borrowed: 0,
    returned: 1,
    overdue: 2
  }, _default:  :borrowed

  before_save :assign_identifier

  def assign_identifier
    self.identifier = "#{book.title.first(5).upcase + SecureRandom.urlsafe_base64.upcase}"
  end

  def due_date
    created_at + 7.days
  end
end
