diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index 26c3fc1..ca08b43 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -17,7 +17,9 @@ class GamificationPlugin < Noosfero::Plugin # Override initial rules with environment specific rules def self.gamification_set_rules(environment) Merit::AppPointRules.clear + Merit::AppBadgeRules.clear Merit::AppPointRules.merge!(Merit::PointRules.new(environment).defined_rules) + Merit::AppBadgeRules.merge!(Merit::BadgeRules.new(environment).defined_rules) end def application_controller_filters @@ -37,23 +39,23 @@ class GamificationPlugin < Noosfero::Plugin Merit::Badge.create!( id: 1, - name: "commenter", + name: "comment_author", description: "Commenter" ) Merit::Badge.create!( id: 2, - name: "relevant-commenter", + name: "relevant_commenter", description: "Relevant Commenter" ) Merit::Badge.create!( id: 3, - name: "article-creator", + name: "article_author", description: "Article Creator", level: 1 ) Merit::Badge.create!( id: 4, - name: "article-creator", + name: "article_author", description: "Article Creator", level: 2 ) diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index 677a93f..ff3b9c4 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -20,17 +20,40 @@ module Merit class BadgeRules include Merit::BadgeRulesMethods - def initialize + AVAILABLE_RULES = { + :comment_author => { + :action => 'comment#create', + :default_threshold => 5, + :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 } + }, + :article_author => { + :action => 'article#create', + :default_threshold => 5, + :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } + }, + :relevant_commenter => { + :action => 'vote_plugin_profile#vote', + :default_threshold => 5, + :value => lambda { |voteable| voteable.kind_of?(Comment) ? voteable.votes.count : 0 } + } + } - grant_on 'comment#create', badge: 'commenter' do |comment| - comment.author.present? && comment.author.comments.count >= 5 + def initialize(environment=nil) + return if environment.nil? + @environment = environment + + Merit::Badge.all.each do |badge| + setting = AVAILABLE_RULES[badge.name.to_sym] + grant_on setting[:action], :badge => badge.name do |source| + setting[:value].call(source) >= setting[:default_threshold] + end end - grant_on 'article#create', badge: 'article-creator', level: 1 do |article| + grant_on 'article#create', badge: 'article_author', level: 1 do |article| article.author.present? && article.author.articles.count >= 5 end - grant_on 'article#create', badge: 'article-creator', level: 2 do |article| + grant_on 'article#create', badge: 'article_author', level: 2 do |article| article.author.present? && article.author.articles.count >= 10 end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 7b9b6a1..cb82ba9 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -33,9 +33,9 @@ module Merit :value => lambda {|vote| vote.vote}, :default_weight => 5 }, + # TODO comment_voter and article_voter } - # FIXME get value from environment def weight(category) settings = Noosfero::Plugin::Settings.new(@environment, GamificationPlugin) settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', AVAILABLE_RULES[category][:default_weight]).to_i diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 3a8e019..7d50014 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -24,13 +24,13 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit badge to author when create 5 new articles' do 5.times { create(Article, :profile_id => person.id, :author => person) } - assert_equal 'article-creator', person.badges.first.name + assert_equal 'article_author', person.badges.first.name assert_equal 1, person.badges.first.level end should 'add merit badge level 2 to author when create 10 new articles' do 10.times { create(Article, :profile_id => person.id, :author => person) } - assert_equal ['article-creator'], person.badges.map(&:name).uniq + assert_equal ['article_author'], person.badges.map(&:name).uniq assert_equal [1, 2], person.badges.map(&:level) end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 6d57062..a47e5dc 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -24,7 +24,7 @@ class CommentTest < ActiveSupport::TestCase should 'add merit badge to author when create 5 new comments' do 5.times { create(Comment, :source => article, :author_id => person.id) } - assert_equal 'commenter', person.badges.first.name + assert_equal 'comment_author', person.badges.first.name end should 'add merit points to comment owner when an user like his comment' do -- libgit2 0.21.2