<commit_before>require 'speedrunslive'

class Game < ApplicationRecord
  include PgSearch
  include SRLGame

  validates :name, presence: true

  has_many :categories, dependent: :destroy
  has_many :runs, through: :categories
  has_many :users, -> { distinct }, through: :runs
  has_many :runners, -> { distinct }, through: :runs, class_name: 'User'
  has_many :aliases, class_name: 'GameAlias'

  after_create :create_initial_alias

  scope :named, -> { where.not(name: nil) }
  scope :shortnamed, -> { where.not(shortname: nil) }
  pg_search_scope :search_both_names,
                  against: %i[name shortname],
                  using: {
                    tsearch: {},
                    trigram: {only: :name}
                  }

  def self.search(term)
    term.strip!
    return Game.none if term.blank?

    Game.joins(:aliases).merge(GameAlias.search_for_name(term))
  end

  def self.from_name(name)
    name = name.strip
    return nil if name.blank?
    joins(:aliases).where(game_aliases: {name: name}).first
  end

  def self.from_name!(name)
    name = name.strip
    return nil if name.blank?
    joins(:aliases).where(game_aliases: {name: name}).first_or_create(name: name)<commit_msg>Dedupe search results<commit_after>require 'speedrunslive'

class Game < ApplicationRecord
  include PgSearch
  include SRLGame

  validates :name, presence: true

  has_many :categories, dependent: :destroy
  has_many :runs, through: :categories
  has_many :users, -> { distinct }, through: :runs
  has_many :runners, -> { distinct }, through: :runs, class_name: 'User'
  has_many :aliases, class_name: 'GameAlias'

  after_create :create_initial_alias

  scope :named, -> { where.not(name: nil) }
  scope :shortnamed, -> { where.not(shortname: nil) }
  pg_search_scope :search_both_names,
                  against: %i[name shortname],
                  using: {
                    tsearch: {},
                    trigram: {only: :name}
                  }

  def self.search(term)
    term.strip!
    return Game.none if term.blank?

    Game.joins(:aliases).merge(GameAlias.search_for_name(term)).distinct
  end

  def self.from_name(name)
    name = name.strip
    return nil if name.blank?
    joins(:aliases).where(game_aliases: {name: name}).first
  end

  def self.from_name!(name)
    name = name.strip
    return nil if name.blank?
    joins(:aliases).where(game_aliases: {name: name}).first_or_create(name: name)
{'fraction_non_alphanumeric': 0.11704312114989733, 'fraction_numerical': 0.0, 'mean_word_length': 2.9934426229508198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4580856', 'n_tokens_mistral': 807, 'n_tokens_neox': 777, 'n_words': 181}