# frozen_string_literal: true

class UxToolkitFavoriteProject < ApplicationRecord
  belongs_to :user
  belongs_to :project, optional: true

  validates :user_id, presence: true
  validates :project_id, presence: true, uniqueness: {scope: :user_id}

  def self.visible_for(user)
    joins(:project)
      .where(user_id: user.id)
      .merge(Project.visible(user))
      .includes(:project)
      .order("#{Project.table_name}.name")
  end
end
