Class: Honor::Scorecard

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/honor/scorecard.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#positionObject

attr_accessible :daily, :lifetime, :monthly, :user_id, :weekly, :yearly, :position


5
6
7
# File 'lib/honor/scorecard.rb', line 5

def position
  @position
end

Class Method Details

.leaderboard(scorecard_user_ids, opt = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/honor/scorecard.rb', line 8

def leaderboard(scorecard_user_ids, opt={})
  opt.reverse_merge!  :rank_by => 'daily',
                      :sort_direction => 'desc'
  scorecards = team_scorecards(scorecard_user_ids, rank_by: opt[:rank_by], sort_direction: opt[:sort_direction])
  rankings = []
  scorecards.each_with_index do |scorecard, i|
    if i == 0
      scorecard.position = 1
    elsif scorecard[opt[:rank_by]] == scorecards[i-1][opt[:rank_by]]
      scorecard.position = rankings[i-1]
    else
      scorecard.position = i + 1
    end
    rankings[i] = scorecard.position
  end
  return scorecards
end

.reset_daily_scoresObject


26
27
28
# File 'lib/honor/scorecard.rb', line 26

def reset_daily_scores
  where("updated_at < ?", Time.zone.now.beginning_of_day).update_all(:daily => 0)
end

.reset_monthly_scoresObject


34
35
36
# File 'lib/honor/scorecard.rb', line 34

def reset_monthly_scores
  where("updated_at < ?", Time.zone.now.beginning_of_month).update_all(:monthly => 0)
end

.reset_weekly_scoresObject


30
31
32
# File 'lib/honor/scorecard.rb', line 30

def reset_weekly_scores
  where("updated_at < ?", Time.zone.now.beginning_of_week).update_all(:weekly => 0)
end

.reset_yearly_scoresObject


38
39
40
# File 'lib/honor/scorecard.rb', line 38

def reset_yearly_scores
  where("updated_at < ?", Time.zone.now.beginning_of_year).update_all(:yearly => 0)
end

.team_scorecards(scorecard_user_ids, opt = {}) ⇒ Object


42
43
44
45
46
# File 'lib/honor/scorecard.rb', line 42

def team_scorecards(scorecard_user_ids, opt={})
  opt.reverse_merge!  :rank_by => 'daily',
                      :sort_direction => 'desc'
  includes(:user).where('user_id IN (?)', scorecard_user_ids).order("#{opt[:rank_by]} #{opt[:sort_direction]}")
end

.update_scorecardsObject


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/honor/scorecard.rb', line 48

def update_scorecards
  Honor::Scorecard.where("updated_at < ?", 1.day.ago).find_each do |scorecard|
    scorecard.update_attributes!({
      daily: Honor::Point.user_points_today(scorecard.user_id),
      weekly: Honor::Point.user_points_this_week(scorecard.user_id),
      monthly: Honor::Point.user_points_this_month(scorecard.user_id),
      yearly:  Honor::Point.user_points_this_year(scorecard.user_id),
      lifetime: Honor::Point.user_points_total(scorecard.user_id)
    })
  end
end