Commit 6334307e88f9b1df606c2793eab8743b0acf9e0b
1 parent
ce884bd9
Exists in
master
and in
1 other branch
Dynamically define badge rules
Showing
5 changed files
with
38 additions
and
13 deletions
Show diff stats
lib/gamification_plugin.rb
| @@ -17,7 +17,9 @@ class GamificationPlugin < Noosfero::Plugin | @@ -17,7 +17,9 @@ class GamificationPlugin < Noosfero::Plugin | ||
| 17 | # Override initial rules with environment specific rules | 17 | # Override initial rules with environment specific rules |
| 18 | def self.gamification_set_rules(environment) | 18 | def self.gamification_set_rules(environment) |
| 19 | Merit::AppPointRules.clear | 19 | Merit::AppPointRules.clear |
| 20 | + Merit::AppBadgeRules.clear | ||
| 20 | Merit::AppPointRules.merge!(Merit::PointRules.new(environment).defined_rules) | 21 | Merit::AppPointRules.merge!(Merit::PointRules.new(environment).defined_rules) |
| 22 | + Merit::AppBadgeRules.merge!(Merit::BadgeRules.new(environment).defined_rules) | ||
| 21 | end | 23 | end |
| 22 | 24 | ||
| 23 | def application_controller_filters | 25 | def application_controller_filters |
| @@ -37,23 +39,23 @@ class GamificationPlugin < Noosfero::Plugin | @@ -37,23 +39,23 @@ class GamificationPlugin < Noosfero::Plugin | ||
| 37 | 39 | ||
| 38 | Merit::Badge.create!( | 40 | Merit::Badge.create!( |
| 39 | id: 1, | 41 | id: 1, |
| 40 | - name: "commenter", | 42 | + name: "comment_author", |
| 41 | description: "Commenter" | 43 | description: "Commenter" |
| 42 | ) | 44 | ) |
| 43 | Merit::Badge.create!( | 45 | Merit::Badge.create!( |
| 44 | id: 2, | 46 | id: 2, |
| 45 | - name: "relevant-commenter", | 47 | + name: "relevant_commenter", |
| 46 | description: "Relevant Commenter" | 48 | description: "Relevant Commenter" |
| 47 | ) | 49 | ) |
| 48 | Merit::Badge.create!( | 50 | Merit::Badge.create!( |
| 49 | id: 3, | 51 | id: 3, |
| 50 | - name: "article-creator", | 52 | + name: "article_author", |
| 51 | description: "Article Creator", | 53 | description: "Article Creator", |
| 52 | level: 1 | 54 | level: 1 |
| 53 | ) | 55 | ) |
| 54 | Merit::Badge.create!( | 56 | Merit::Badge.create!( |
| 55 | id: 4, | 57 | id: 4, |
| 56 | - name: "article-creator", | 58 | + name: "article_author", |
| 57 | description: "Article Creator", | 59 | description: "Article Creator", |
| 58 | level: 2 | 60 | level: 2 |
| 59 | ) | 61 | ) |
lib/merit/badge_rules.rb
| @@ -20,17 +20,40 @@ module Merit | @@ -20,17 +20,40 @@ module Merit | ||
| 20 | class BadgeRules | 20 | class BadgeRules |
| 21 | include Merit::BadgeRulesMethods | 21 | include Merit::BadgeRulesMethods |
| 22 | 22 | ||
| 23 | - def initialize | 23 | + AVAILABLE_RULES = { |
| 24 | + :comment_author => { | ||
| 25 | + :action => 'comment#create', | ||
| 26 | + :default_threshold => 5, | ||
| 27 | + :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 } | ||
| 28 | + }, | ||
| 29 | + :article_author => { | ||
| 30 | + :action => 'article#create', | ||
| 31 | + :default_threshold => 5, | ||
| 32 | + :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } | ||
| 33 | + }, | ||
| 34 | + :relevant_commenter => { | ||
| 35 | + :action => 'vote_plugin_profile#vote', | ||
| 36 | + :default_threshold => 5, | ||
| 37 | + :value => lambda { |voteable| voteable.kind_of?(Comment) ? voteable.votes.count : 0 } | ||
| 38 | + } | ||
| 39 | + } | ||
| 24 | 40 | ||
| 25 | - grant_on 'comment#create', badge: 'commenter' do |comment| | ||
| 26 | - comment.author.present? && comment.author.comments.count >= 5 | 41 | + def initialize(environment=nil) |
| 42 | + return if environment.nil? | ||
| 43 | + @environment = environment | ||
| 44 | + | ||
| 45 | + Merit::Badge.all.each do |badge| | ||
| 46 | + setting = AVAILABLE_RULES[badge.name.to_sym] | ||
| 47 | + grant_on setting[:action], :badge => badge.name do |source| | ||
| 48 | + setting[:value].call(source) >= setting[:default_threshold] | ||
| 49 | + end | ||
| 27 | end | 50 | end |
| 28 | 51 | ||
| 29 | - grant_on 'article#create', badge: 'article-creator', level: 1 do |article| | 52 | + grant_on 'article#create', badge: 'article_author', level: 1 do |article| |
| 30 | article.author.present? && article.author.articles.count >= 5 | 53 | article.author.present? && article.author.articles.count >= 5 |
| 31 | end | 54 | end |
| 32 | 55 | ||
| 33 | - grant_on 'article#create', badge: 'article-creator', level: 2 do |article| | 56 | + grant_on 'article#create', badge: 'article_author', level: 2 do |article| |
| 34 | article.author.present? && article.author.articles.count >= 10 | 57 | article.author.present? && article.author.articles.count >= 10 |
| 35 | end | 58 | end |
| 36 | 59 |
lib/merit/point_rules.rb
| @@ -33,9 +33,9 @@ module Merit | @@ -33,9 +33,9 @@ module Merit | ||
| 33 | :value => lambda {|vote| vote.vote}, | 33 | :value => lambda {|vote| vote.vote}, |
| 34 | :default_weight => 5 | 34 | :default_weight => 5 |
| 35 | }, | 35 | }, |
| 36 | + # TODO comment_voter and article_voter | ||
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | - # FIXME get value from environment | ||
| 39 | def weight(category) | 39 | def weight(category) |
| 40 | settings = Noosfero::Plugin::Settings.new(@environment, GamificationPlugin) | 40 | settings = Noosfero::Plugin::Settings.new(@environment, GamificationPlugin) |
| 41 | settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', AVAILABLE_RULES[category][:default_weight]).to_i | 41 | settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', AVAILABLE_RULES[category][:default_weight]).to_i |
test/unit/article_test.rb
| @@ -24,13 +24,13 @@ class ArticleTest < ActiveSupport::TestCase | @@ -24,13 +24,13 @@ class ArticleTest < ActiveSupport::TestCase | ||
| 24 | 24 | ||
| 25 | should 'add merit badge to author when create 5 new articles' do | 25 | should 'add merit badge to author when create 5 new articles' do |
| 26 | 5.times { create(Article, :profile_id => person.id, :author => person) } | 26 | 5.times { create(Article, :profile_id => person.id, :author => person) } |
| 27 | - assert_equal 'article-creator', person.badges.first.name | 27 | + assert_equal 'article_author', person.badges.first.name |
| 28 | assert_equal 1, person.badges.first.level | 28 | assert_equal 1, person.badges.first.level |
| 29 | end | 29 | end |
| 30 | 30 | ||
| 31 | should 'add merit badge level 2 to author when create 10 new articles' do | 31 | should 'add merit badge level 2 to author when create 10 new articles' do |
| 32 | 10.times { create(Article, :profile_id => person.id, :author => person) } | 32 | 10.times { create(Article, :profile_id => person.id, :author => person) } |
| 33 | - assert_equal ['article-creator'], person.badges.map(&:name).uniq | 33 | + assert_equal ['article_author'], person.badges.map(&:name).uniq |
| 34 | assert_equal [1, 2], person.badges.map(&:level) | 34 | assert_equal [1, 2], person.badges.map(&:level) |
| 35 | end | 35 | end |
| 36 | 36 |
test/unit/comment_test.rb
| @@ -24,7 +24,7 @@ class CommentTest < ActiveSupport::TestCase | @@ -24,7 +24,7 @@ class CommentTest < ActiveSupport::TestCase | ||
| 24 | 24 | ||
| 25 | should 'add merit badge to author when create 5 new comments' do | 25 | should 'add merit badge to author when create 5 new comments' do |
| 26 | 5.times { create(Comment, :source => article, :author_id => person.id) } | 26 | 5.times { create(Comment, :source => article, :author_id => person.id) } |
| 27 | - assert_equal 'commenter', person.badges.first.name | 27 | + assert_equal 'comment_author', person.badges.first.name |
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | should 'add merit points to comment owner when an user like his comment' do | 30 | should 'add merit points to comment owner when an user like his comment' do |