/
rules.rb
41 lines (38 loc) · 1.32 KB
/
rules.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Talent
module Rules
# Grant badge to user if applies
def grant_on(action, *args, &block)
options = args.extract_options!
talent_rules[action] ||= []
talent_rules[action] << {
:badge => options[:badge],
:level => options[:level],
:block => block
}
Rails.logger.warn "TALENT: Added rule for #{action}."
end
def check_new_actions
TalentAction.where(:processed => false).each do |action|
check_rules(action)
action.processed!
end
end
def check_rules(action)
action_name = "#{action.target_model}\##{action.action_method}"
talent_rules[action_name].each do |rule|
Rails.logger.warn "TALENT: Checking #{talent_rules[action_name].count} rules for #{action_name}..."
user = User.find(action.user_id)
badge = Badge.where(:name => rule[:badge], :level => rule[:level]).first
# Grant if no block given, or it evaluates to true
if (rule[:block].nil? || rule[:block].call) && !user.badges.include?(badge)
user.badges << badge
user.save
Rails.logger.warn "TALENT: Granted badge #{badge.name}-#{badge.level} to #{user.name}!"
end
end unless talent_rules[action_name].nil?
end
def talent_rules
@talent_rules ||= {}
end
end
end