Sha256: 2417693efcd52a6207f8912938e4d37901a3cd1d42586704f5331f3143af0ca0

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module Merit
  class Score
    include Mongoid::Document
    include Mongoid::Timestamps

    field :category, type: String, default: 'default'

    belongs_to :sash
    has_many :score_points, class_name: 'Merit::Score::Point', dependent: :destroy

    # 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.
    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

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

    class Point
      include Mongoid::Document
      include Mongoid::Timestamps

      field :num_points,    type: Integer, default: 0
      field :log,           type: String

      belongs_to :score, class_name: 'Merit::Score'
      has_many :activity_logs, class_name: 'Merit::ActivityLog', as: :related_change
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
merit-2.0.0 lib/merit/models/mongoid/merit/score.rb
merit-1.9.0 lib/merit/models/mongoid/merit/score.rb
merit-1.8.0 lib/merit/models/mongoid/merit/score.rb