<commit_before>module SearchCache
  extend ActiveSupport::Concern

  included do
    after_save :calculate_tsvector
  end

  def calculate_tsvector
    ActiveRecord::Base.connection.execute("
      UPDATE proposals SET tsv = (#{searchable_values_sql}) WHERE id = #{self.id}")
  end

  private

  def searchable_values_sql
    searchable_values.collect { |value, weight| set_tsvector(value, weight) }.join(" || ")
  end

  def set_tsvector(value, weight)
    "setweight(to_tsvector('spanish', coalesce(#{quote(value)}, '')), #{quote(weight)})"
  end

  def quote(value)
    ActiveRecord::Base.connection.quote(value)
  end

end<commit_msg>Use model table name instead of hardcoding it<commit_after>module SearchCache
  extend ActiveSupport::Concern

  included do
    after_save :calculate_tsvector
  end

  def calculate_tsvector
    ActiveRecord::Base.connection.execute("
      UPDATE #{self.class.table_name} SET tsv = (#{searchable_values_sql}) WHERE id = #{self.id}")
  end

  private

  def searchable_values_sql
    searchable_values.collect { |value, weight| set_tsvector(value, weight) }.join(" || ")
  end

  def set_tsvector(value, weight)
    "setweight(to_tsvector('spanish', coalesce(#{quote(value)}, '')), #{quote(weight)})"
  end

  def quote(value)
    ActiveRecord::Base.connection.quote(value)
  end

end
{'fraction_non_alphanumeric': 0.12018140589569161, 'fraction_numerical': 0.0, 'mean_word_length': 4.6581196581196584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2473070', 'n_tokens_mistral': 436, 'n_tokens_neox': 414, 'n_words': 92}