# frozen_string_literal: true

class UxToolkitFavoriteQuery < ApplicationRecord
  belongs_to :user
  belongs_to :query, class_name: 'IssueQuery', optional: true

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

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