<commit_before>require 'stringex'

class Topic
  include Mongoid::Document
  include Sunspot::Mongoid
  include Redis::Aid::Ns(:new_topic)
  
  field :title
  index :title

  field :slug_title
  index :slug_title

  validates_uniqueness_of :title
  validates_uniqueness_of :slug_title

  searchable :auto_index => true do
    text    :title, :slug_title
  end


  alias :old_set_title :title= unless method_defined?(:old_set_title)
  def title=(new_title)
    old_set_title new_title
    self.slug_title = new_title.to_url
  end

  def self.by_title(title)
    where(slug_title: title.to_url||'').first || new(title: title)
  end

  def self.by_slug(slug)
    where(slug_title: slug||'').first
  end

  def self.ensure_for_channel(ch)
    unless Topic.where(slug_title: ch.slug_title||'').first
      t = Topic.by_title(ch.title)
      t.save and t
    end
  end

  def self.for_channel(ch)
    by_slug(ch.slug_title) or ensure_for_channel(ch)
  end


  def top_users(nr=5)
    redis[:top_users].zrevrange(0, (nr-1)).map {|id| User.find(id)}
  end
  
  def top_users_add(user, val)
    redis[:top_users].zadd val ,user
  end
<commit_msg>actually save top users per topic<commit_after>require 'stringex'

class Topic
  include Mongoid::Document
  include Sunspot::Mongoid
  include Redis::Aid::Ns(:new_topic)
  
  field :title
  index :title

  field :slug_title
  index :slug_title

  validates_uniqueness_of :title
  validates_uniqueness_of :slug_title

  searchable :auto_index => true do
    text    :title, :slug_title
  end


  alias :old_set_title :title= unless method_defined?(:old_set_title)
  def title=(new_title)
    old_set_title new_title
    self.slug_title = new_title.to_url
  end

  def self.by_title(title)
    where(slug_title: title.to_url||'').first || new(title: title)
  end

  def self.by_slug(slug)
    where(slug_title: slug||'').first
  end

  def self.ensure_for_channel(ch)
    unless Topic.where(slug_title: ch.slug_title||'').first
      t = Topic.by_title(ch.title)
      t.save and t
    end
  end

  def self.for_channel(ch)
    by_slug(ch.slug_title) or ensure_for_channel(ch)
  end


  def top_users(nr=5)
    redis[id][:top_users].zrevrange(0, (nr-1)).map {|id| User.find(id)}
  end
  
  def top_users_add(user, val)
    redis[id][:top_users].zadd val ,user
  end

{'fraction_non_alphanumeric': 0.1103388357949609, 'fraction_numerical': 0.0026064291920069507, 'mean_word_length': 3.8179916317991633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1517293', 'n_tokens_mistral': 891, 'n_tokens_neox': 891, 'n_words': 177}