Class: Merit::Score

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/merit/models/mongoid/merit/score.rb,
lib/merit/models/active_record/merit/score.rb

Defined Under Namespace

Classes: Point

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.top_scored(options = {}) ⇒ Object

Meant to display a leaderboard. Accepts options :table_name (users by default), :since_date (1.month.ago by default) and :limit (10 by default).

It lists top 10 scored objects in the last month, unless you change query parameters.


17
18
19
20
21
22
23
24
# File 'lib/merit/models/mongoid/merit/score.rb', line 17

def self.top_scored(options = {})
  options[:since_date] ||= 1.month.ago
  options[:limit]      ||= 10
  Score.where(created_at: (options[:since_date]..Time.now))
        .desc(:points)
        .limit(options[:limit])
        .flatten.map { |score| score.sash.user }
end

Instance Method Details

#pointsObject


26
27
28
# File 'lib/merit/models/mongoid/merit/score.rb', line 26

def points
  score_points.sum(:num_points) || 0
end