Module: Cheer::ModelAdditions

Defined in:
lib/cheer/model_additions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Hookup event for ModelAdditions Module



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cheer/model_additions.rb', line 5

def self.extended(base)
  base.class_eval do
    model_name = self.name.underscore

    define_singleton_method :standing_methods do
      {
        current_rank: "current_#{model_name}_rank".to_sym,
        rank_around: "#{model_name.pluralize}_around".to_sym,
        top_rankers: "top_#{model_name.pluralize}".to_sym
      }
    end
  end
end

Instance Method Details

#leaderboard(name, args = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cheer/model_additions.rb', line 19

def leaderboard(name, args = {})
  define_method name.to_sym do |user_limit = 3|
    rank_evaluator = RankEvaluator.new(
      model_object: self,
      config: Argument.new(args.merge(model_klass: self.class))
    )

    Leaderboard.new(
      model_klass: self.class,
      rank_evaluator: rank_evaluator
    )
  end
end