Commit a8aed5d9befec360868155eba1be426cee015a18

Authored by Victor Costa
1 parent f400109e

Define badge author in rule settings

lib/ext/article.rb
@@ -3,6 +3,6 @@ require_dependency 'article' @@ -3,6 +3,6 @@ require_dependency 'article'
3 class Article 3 class Article
4 4
5 has_merit 5 has_merit
6 - has_merit_actions :user_method => :author 6 + has_merit_actions
7 7
8 end 8 end
lib/ext/comment.rb
@@ -2,6 +2,6 @@ require_dependency 'comment' @@ -2,6 +2,6 @@ require_dependency 'comment'
2 2
3 class Comment 3 class Comment
4 4
5 - has_merit_actions :user_method => :author 5 + has_merit_actions
6 6
7 end 7 end
lib/merit/badge_rules.rb
1 -# Be sure to restart your server when you modify this file.  
2 -#  
3 # +grant_on+ accepts: 1 # +grant_on+ accepts:
4 # * Nothing (always grants) 2 # * Nothing (always grants)
5 # * A block which evaluates to boolean (recieves the object as parameter) 3 # * A block which evaluates to boolean (recieves the object as parameter)
6 # * A block with a hash composed of methods to run on the target object with 4 # * A block with a hash composed of methods to run on the target object with
7 # expected values (+votes: 5+ for instance). 5 # expected values (+votes: 5+ for instance).
8 # 6 #
9 -# +grant_on+ can have a +:to+ method name, which called over the target object  
10 -# should retrieve the object to badge (could be +:user+, +:self+, +:follower+,  
11 -# etc). If it's not defined merit will apply the badge to the user who  
12 -# triggered the action (:action_user by default). If it's :itself, it badges  
13 -# the created object (new user for instance).  
14 -#  
15 -# The :temporary option indicates that if the condition doesn't hold but the  
16 -# badge is granted, then it's removed. It's false by default (badges are kept  
17 -# forever).  
18 -  
19 module Merit 7 module Merit
20 class BadgeRules 8 class BadgeRules
21 include Merit::BadgeRulesMethods 9 include Merit::BadgeRulesMethods
@@ -24,11 +12,13 @@ module Merit @@ -24,11 +12,13 @@ module Merit
24 :comment_author => { 12 :comment_author => {
25 :action => 'comment#create', 13 :action => 'comment#create',
26 :default_threshold => 5, 14 :default_threshold => 5,
  15 + :to => :author,
27 :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 } 16 :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 }
28 }, 17 },
29 :article_author => { 18 :article_author => {
30 :action => 'article#create', 19 :action => 'article#create',
31 :default_threshold => 5, 20 :default_threshold => 5,
  21 + :to => :author,
32 :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } 22 :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 }
33 }, 23 },
34 :relevant_commenter => { 24 :relevant_commenter => {
@@ -44,7 +34,7 @@ module Merit @@ -44,7 +34,7 @@ module Merit
44 34
45 environment.gamification_plugin_badges.all.each do |badge| 35 environment.gamification_plugin_badges.all.each do |badge|
46 setting = AVAILABLE_RULES[badge.name.to_sym] 36 setting = AVAILABLE_RULES[badge.name.to_sym]
47 - grant_on setting[:action], :badge => badge.name, :level => badge.level do |source| 37 + grant_on setting[:action], :badge => badge.name, :level => badge.level, :to => setting[:to] do |source|
48 setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold]) 38 setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold])
49 end 39 end
50 end 40 end