Class: Honor::Scorecard
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Honor::Scorecard
- Defined in:
- lib/honor/scorecard.rb
Instance Attribute Summary collapse
-
#position ⇒ Object
attr_accessible :daily, :lifetime, :monthly, :user_id, :weekly, :yearly, :position.
Class Method Summary collapse
- .leaderboard(scorecard_user_ids, opt = {}) ⇒ Object
- .reset_daily_scores ⇒ Object
- .reset_monthly_scores ⇒ Object
- .reset_weekly_scores ⇒ Object
- .reset_yearly_scores ⇒ Object
- .team_scorecards(scorecard_user_ids, opt = {}) ⇒ Object
- .update_scorecards ⇒ Object
Instance Attribute Details
#position ⇒ Object
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_scores ⇒ Object
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_scores ⇒ Object
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_scores ⇒ Object
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_scores ⇒ Object
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_scorecards ⇒ Object
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 |