<commit_before>require 'digest/sha1'
class Group < ActiveRecord::Base
  include Authority::Abilities
  include CroppableAvatar
  self.authorizer_name = 'GroupAuthorizer'

  has_many :predictions
  has_many :memberships
  has_many :users, through: :memberships

  validates_presence_of   :name
  validates_length_of :name, maximum: 30
  validates_length_of :description, maximum: 140

  after_create :shortenUrl

  scope :id_lt, -> (i) {where('groups.id < ?', i) if i}

  def shortenUrl
    hashedId = Digest::SHA1.new << self.id.to_s
    self.share_id = hashedId.to_s
    if Rails.env.production?
      bitly = Bitly.new('adamnengland','R_098b05120c29c43ad74c6b6a0e7fcf64')
      page_url = bitly.shorten("#{Rails.application.config.knoda_web_url}/groups/join?id=#{hashedId}")
      self.share_url = page_url.short_url
    else
      self.share_url = "#{Rails.application.config.knoda_web_url}/groups/join?id=#{hashedId}"
    end
    self.save()    
  end

  def owned_by?(user)
    return self.memberships.where(:user => user, :role => 'OWNER').size > 0
  end     

  def owner
    return self.memberships.where(:role => 'OWNER').first.user
  end

  def self.rebuildLeaderboards(group)
    lb = leaderboard(group, 8.days.ago)
    Rails.cache.write("group_leaderboard_weekly_#{group.id}", lb, timeToLive: 7.days)
  end    

  def self.weeklyLeaderboard(group)
    if Rails.cache.exist?("group_leaderboard_weekly_#{group.id}")
      return Rails.cache.read("group_leaderboard_weekly_#{group.id}")
    else
      lb = leaderboard(group, 8.days.ago)
      Rails.cache.write("group_leaderboard_weekly_#{group.id}", lb, timeToLive: 7.days)
      return lb
    end    
  end

  def self.monthlyLeaderboard(group)
    leaderboard(group, 1.month.ago)
  end 

  def self.allTimeLeaderboard(group)
    leaderboard(group, 5.years.ago)
  end   

  private
    def self.leaderboard(group, max_age)
      users = group.users
      users = users.sort!{|u1,u2| u2.group_won(group.id, max_age) <=> u1.group_won(group.id, max_age)}
      leaders = []
      i = 0
      users.each do |u|
        i = i + 1
        leaders << {:rank => i, :rankText => "#{i.ordinalize} of #{users.size}", :user_id => u.id, :username => u.username, :avatar_image => u.avatar_image, :won => u.group_won(group.id, max_age), :lost => u.group_lost(group.id, max_age), :verified_account => u.verified_account}
      end
      return leaders      
    end
end
<commit_msg>Adding default order for groups<commit_after>require 'digest/sha1'
class Group < ActiveRecord::Base
  include Authority::Abilities
  include CroppableAvatar
  self.authorizer_name = 'GroupAuthorizer'

  has_many :predictions
  has_many :memberships
  has_many :users, through: :memberships

  validates_presence_of   :name
  validates_length_of :name, maximum: 30
  validates_length_of :description, maximum: 140

  after_create :shortenUrl

  default_scope order('name ASC')
  scope :id_lt, -> (i) {where('groups.id < ?', i) if i}
  
  def shortenUrl
    hashedId = Digest::SHA1.new << self.id.to_s
    self.share_id = hashedId.to_s
    if Rails.env.production?
      bitly = Bitly.new('adamnengland','R_098b05120c29c43ad74c6b6a0e7fcf64')
      page_url = bitly.shorten("#{Rails.application.config.knoda_web_url}/groups/join?id=#{hashedId}")
      self.share_url = page_url.short_url
    else
      self.share_url = "#{Rails.application.config.knoda_web_url}/groups/join?id=#{hashedId}"
    end
    self.save()    
  end

  def owned_by?(user)
    return self.memberships.where(:user => user, :role => 'OWNER').size > 0
  end     

  def owner
    return self.memberships.where(:role => 'OWNER').first.user
  end

  def self.rebuildLeaderboards(group)
    lb = leaderboard(group, 8.days.ago)
    Rails.cache.write("group_leaderboard_weekly_#{group.id}", lb, timeToLive: 7.days)
  end    

  def self.weeklyLeaderboard(group)
    if Rails.cache.exist?("group_leaderboard_weekly_#{group.id}")
      return Rails.cache.read("group_leaderboard_weekly_#{group.id}")
    else
      lb = leaderboard(group, 8.days.ago)
      Rails.cache.write("group_leaderboard_weekly_#{group.id}", lb, timeToLive: 7.days)
      return lb
    end    
  end

  def self.monthlyLeaderboard(group)
    leaderboard(group, 1.month.ago)
  end 

  def self.allTimeLeaderboard(group)
    leaderboard(group, 5.years.ago)
  end   

  private
    def self.leaderboard(group, max_age)
      users = group.users
      users = users.sort!{|u1,u2| u2.group_won(group.id, max_age) <=> u1.group_won(group.id, max_age)}
      leaders = []
      i = 0
      users.each do |u|
        i = i + 1
        leaders << {:rank => i, :rankText => "#{i.ordinalize} of #{users.size}", :user_id => u.id, :username => u.username, :avatar_image => u.avatar_image, :won => u.group_won(group.id, max_age), :lost => u.group_lost(group.id, max_age), :verified_account => u.verified_account}
      end
      return leaders      
    end
end

{'fraction_non_alphanumeric': 0.129938900203666, 'fraction_numerical': 0.016293279022403257, 'mean_word_length': 3.505504587155963, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, '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': '307939', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1772, 'n_words': 325}