Module: Merit::RankRulesMethods

Included in:
RankRules
Defined in:
lib/merit/rules_rank_methods.rb

Overview

5 stars is a common ranking use case. They are not given at specified actions like badges, you should define a cron job to test if ranks are to be granted.

set_rank accepts:

  • :level ranking level (greater is better)

  • :to model or scope to check if new rankings apply

  • :level_name attribute name (default is empty and results in 'level' attribute, if set it's appended like 'level_#level_name')

Instance Method Summary collapse

Instance Method Details

#check_rank_rulesObject

Not part of merit after_filter. To be called asynchronously: Merit::RankRules.new.check_rank_rules


31
32
33
34
35
36
37
# File 'lib/merit/rules_rank_methods.rb', line 31

def check_rank_rules
  defined_rules.each do |scoped_model, level_and_rules|
    level_and_rules.sort.each do |level, rule|
      grant_when_applies(scoped_model, rule, level)
    end
  end
end

#defined_rulesObject

Currently defined rules


40
41
42
# File 'lib/merit/rules_rank_methods.rb', line 40

def defined_rules
  @defined_rules ||= {}
end

#set_rank(*args, &block) ⇒ Object

Populates defined_rules hash with following hierarchy:

defined_rules[ModelToRank] = { levels => blocks }

14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/merit/rules_rank_methods.rb', line 14

def set_rank(*args, &block)
  options = args.extract_options!

  rule = Rule.new
  rule.block = block
  if options[:level_name].present?
    rule.level_name = "level_#{options[:level_name]}"
  else
    rule.level_name = 'level'
  end

  defined_rules[options[:to]] ||= {}
  defined_rules[options[:to]].merge!(options[:level] => rule)
end