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 3 class Article
4 4  
5 5 has_merit
6   - has_merit_actions :user_method => :author
  6 + has_merit_actions
7 7  
8 8 end
... ...
lib/ext/comment.rb
... ... @@ -2,6 +2,6 @@ require_dependency 'comment'
2 2  
3 3 class Comment
4 4  
5   - has_merit_actions :user_method => :author
  5 + has_merit_actions
6 6  
7 7 end
... ...
lib/merit/badge_rules.rb
1   -# Be sure to restart your server when you modify this file.
2   -#
3 1 # +grant_on+ accepts:
4 2 # * Nothing (always grants)
5 3 # * A block which evaluates to boolean (recieves the object as parameter)
6 4 # * A block with a hash composed of methods to run on the target object with
7 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 7 module Merit
20 8 class BadgeRules
21 9 include Merit::BadgeRulesMethods
... ... @@ -24,11 +12,13 @@ module Merit
24 12 :comment_author => {
25 13 :action => 'comment#create',
26 14 :default_threshold => 5,
  15 + :to => :author,
27 16 :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 }
28 17 },
29 18 :article_author => {
30 19 :action => 'article#create',
31 20 :default_threshold => 5,
  21 + :to => :author,
32 22 :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 }
33 23 },
34 24 :relevant_commenter => {
... ... @@ -44,7 +34,7 @@ module Merit
44 34  
45 35 environment.gamification_plugin_badges.all.each do |badge|
46 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 38 setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold])
49 39 end
50 40 end
... ...