Class: Cheer::RankEvaluator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cheer/rank_evaluator.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ RankEvaluator

Returns a new instance of RankEvaluator.



10
11
12
13
# File 'lib/cheer/rank_evaluator.rb', line 10

def initialize(attributes = {})
  @model_object = attributes[:model_object]
  @config       = attributes[:config]
end

Instance Method Details

#current_rankObject



15
16
17
18
# File 'lib/cheer/rank_evaluator.rb', line 15

def current_rank
  return high_rankers unless equal_rankers?
  high_rankers + position_amongst_equal_rankers
end

#rank_aroundObject



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

def rank_around
  offset = current_rank - (around_limit + 1)
  limit_setting = around_limit * 2 + 1

  if offset < 0
    limit_setting += offset
    offset = 0
  end

  top_rankers(limit_setting).offset(offset)
end

#top_rankers(user_limit) ⇒ Object



32
33
34
35
# File 'lib/cheer/rank_evaluator.rb', line 32

def top_rankers(user_limit)
  model_klass.order("#{column_name} DESC, #{sort_order}")
             .limit(user_limit)
end