Commit 132da353694c4e4a1b3be16be5014d1693c8a863

Authored by Victor Costa
1 parent 233d63e9

Add new badge types

lib/ext/friendship.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +require_dependency 'friendship'
  2 +
  3 +class Friendship
  4 +
  5 + has_merit_actions
  6 +
  7 +end
lib/merit/badge_rules.rb
@@ -21,11 +21,29 @@ module Merit @@ -21,11 +21,29 @@ module Merit
21 :to => :author, 21 :to => :author,
22 :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } 22 :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 }
23 }, 23 },
24 - :relevant_commenter => { 24 + :positive_votes_received => {
25 :action => 'vote#create', 25 :action => 'vote#create',
26 :default_threshold => 5, 26 :default_threshold => 5,
27 :to => lambda {|vote| vote.voteable.author}, 27 :to => lambda {|vote| vote.voteable.author},
28 - :value => lambda { |vote| vote.voteable.kind_of?(Comment) ? Vote.for_voteable(vote.voteable).where('vote > 0').count : 0 } 28 + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote > 0').count }
  29 + },
  30 + :negative_votes_received => {
  31 + :action => 'vote#create',
  32 + :default_threshold => 5,
  33 + :to => lambda {|vote| vote.voteable.author},
  34 + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote < 0').count }
  35 + },
  36 + :votes_performed => {
  37 + :action => 'vote#create',
  38 + :default_threshold => 5,
  39 + :to => lambda {|vote| vote.voter},
  40 + :value => lambda { |vote| Vote.for_voter(vote.voter).count }
  41 + },
  42 + :friendly => {
  43 + :action => 'friendship#create',
  44 + :default_threshold => 5,
  45 + :to => lambda {|friendship| friendship.person},
  46 + :value => lambda { |friendship| friendship.person.friends.count }
29 } 47 }
30 } 48 }
31 49
test/unit/article_test.rb
@@ -82,4 +82,28 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -82,4 +82,28 @@ class ArticleTest &lt; ActiveSupport::TestCase
82 end 82 end
83 end 83 end
84 84
  85 + should 'add badge to author when users like his article' do
  86 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')
  87 + GamificationPlugin.gamification_set_rules(environment)
  88 +
  89 + article = create(Article, :name => 'Test', :profile => person, :author => person)
  90 + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) }
  91 + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
  92 + assert_equal [], person.badges
  93 + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
  94 + assert_equal 'positive_votes_received', person.reload.badges.first.name
  95 + end
  96 +
  97 + should 'add badge to author when users dislike his article' do
  98 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')
  99 + GamificationPlugin.gamification_set_rules(environment)
  100 +
  101 + article = create(Article, :name => 'Test', :profile => person, :author => person)
  102 + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) }
  103 + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
  104 + assert_equal [], person.badges
  105 + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
  106 + assert_equal 'negative_votes_received', person.reload.badges.first.name
  107 + end
  108 +
85 end 109 end
test/unit/comment_test.rb
@@ -32,8 +32,8 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -32,8 +32,8 @@ class CommentTest &lt; ActiveSupport::TestCase
32 assert_equal 'comment_author', person.badges.first.name 32 assert_equal 'comment_author', person.badges.first.name
33 end 33 end
34 34
35 - should 'add badge to author when users vote in his comment' do  
36 - GamificationPlugin::Badge.create!(:owner => environment, :name => 'relevant_commenter') 35 + should 'add badge to author when users like his comment' do
  36 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')
37 GamificationPlugin.gamification_set_rules(environment) 37 GamificationPlugin.gamification_set_rules(environment)
38 38
39 comment = create(Comment, :source => article, :author_id => person.id) 39 comment = create(Comment, :source => article, :author_id => person.id)
@@ -41,7 +41,19 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -41,7 +41,19 @@ class CommentTest &lt; ActiveSupport::TestCase
41 Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) 41 Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1)
42 assert_equal [], person.badges 42 assert_equal [], person.badges
43 Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) 43 Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1)
44 - assert_equal 'relevant_commenter', person.reload.badges.first.name 44 + assert_equal 'positive_votes_received', person.reload.badges.first.name
  45 + end
  46 +
  47 + should 'add badge to author when users dislike his comment' do
  48 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')
  49 + GamificationPlugin.gamification_set_rules(environment)
  50 +
  51 + comment = create(Comment, :source => article, :author_id => person.id)
  52 + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) }
  53 + Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1)
  54 + assert_equal [], person.badges
  55 + Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1)
  56 + assert_equal 'negative_votes_received', person.reload.badges.first.name
45 end 57 end
46 58
47 should 'add merit points to comment owner when an user like his comment' do 59 should 'add merit points to comment owner when an user like his comment' do
test/unit/person_test.rb 0 → 100644
@@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class PersonTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @environment = Environment.default
  7 + GamificationPlugin.gamification_set_rules(@environment)
  8 + @person = create_user('testuser').person
  9 + end
  10 + attr_accessor :environment, :person
  11 +
  12 + should 'add badge to a voter' do
  13 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'votes_performed')
  14 + GamificationPlugin.gamification_set_rules(environment)
  15 +
  16 + 4.times { Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1) }
  17 + assert_equal [], person.badges
  18 + Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1)
  19 + assert_equal 'votes_performed', person.reload.badges.first.name
  20 + end
  21 +
  22 + should 'add badge to a friendly person' do
  23 + GamificationPlugin::Badge.create!(:owner => environment, :name => 'friendly')
  24 + GamificationPlugin.gamification_set_rules(environment)
  25 +
  26 + 5.times { |i| person.add_friend(create_user("testuser#{i}").person) }
  27 + assert_equal 'friendly', person.reload.badges.first.name
  28 + end
  29 +
  30 +end