# frozen_string_literal: true

class UxToolkitTextSnippet < ApplicationRecord
  validates :title,
            presence: true,
            length: {maximum: 255},
            uniqueness: {case_sensitive: false}
  validates :snippet_text, presence: true
  validates :position,
            numericality: {only_integer: true, greater_than_or_equal_to: 0}

  scope :enabled, -> { where(enabled: true) }
  scope :ordered, -> { order(:position, Arel.sql('LOWER(title)'), :id) }

  def self.table_available?
    table_exists?
  rescue ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
    false
  end
end
