From a8aed5d9befec360868155eba1be426cee015a18 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 31 Mar 2015 08:42:12 -0300 Subject: [PATCH] Define badge author in rule settings --- lib/ext/article.rb | 2 +- lib/ext/comment.rb | 2 +- lib/merit/badge_rules.rb | 16 +++------------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/ext/article.rb b/lib/ext/article.rb index f7f8d2c..4536c61 100644 --- a/lib/ext/article.rb +++ b/lib/ext/article.rb @@ -3,6 +3,6 @@ require_dependency 'article' class Article has_merit - has_merit_actions :user_method => :author + has_merit_actions end diff --git a/lib/ext/comment.rb b/lib/ext/comment.rb index 7e59005..c252402 100644 --- a/lib/ext/comment.rb +++ b/lib/ext/comment.rb @@ -2,6 +2,6 @@ require_dependency 'comment' class Comment - has_merit_actions :user_method => :author + has_merit_actions end diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index 8d4311d..7cf0366 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -1,21 +1,9 @@ -# Be sure to restart your server when you modify this file. -# # +grant_on+ accepts: # * Nothing (always grants) # * A block which evaluates to boolean (recieves the object as parameter) # * A block with a hash composed of methods to run on the target object with # expected values (+votes: 5+ for instance). # -# +grant_on+ can have a +:to+ method name, which called over the target object -# should retrieve the object to badge (could be +:user+, +:self+, +:follower+, -# etc). If it's not defined merit will apply the badge to the user who -# triggered the action (:action_user by default). If it's :itself, it badges -# the created object (new user for instance). -# -# The :temporary option indicates that if the condition doesn't hold but the -# badge is granted, then it's removed. It's false by default (badges are kept -# forever). - module Merit class BadgeRules include Merit::BadgeRulesMethods @@ -24,11 +12,13 @@ module Merit :comment_author => { :action => 'comment#create', :default_threshold => 5, + :to => :author, :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 } }, :article_author => { :action => 'article#create', :default_threshold => 5, + :to => :author, :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } }, :relevant_commenter => { @@ -44,7 +34,7 @@ module Merit environment.gamification_plugin_badges.all.each do |badge| setting = AVAILABLE_RULES[badge.name.to_sym] - grant_on setting[:action], :badge => badge.name, :level => badge.level do |source| + grant_on setting[:action], :badge => badge.name, :level => badge.level, :to => setting[:to] do |source| setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold]) end end -- libgit2 0.21.2