From 132da353694c4e4a1b3be16be5014d1693c8a863 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 28 May 2015 17:53:38 -0300 Subject: [PATCH] Add new badge types --- lib/ext/friendship.rb | 7 +++++++ lib/merit/badge_rules.rb | 22 ++++++++++++++++++++-- test/unit/article_test.rb | 24 ++++++++++++++++++++++++ test/unit/comment_test.rb | 18 +++++++++++++++--- test/unit/person_test.rb | 30 ++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 lib/ext/friendship.rb create mode 100644 test/unit/person_test.rb diff --git a/lib/ext/friendship.rb b/lib/ext/friendship.rb new file mode 100644 index 0000000..98a73a3 --- /dev/null +++ b/lib/ext/friendship.rb @@ -0,0 +1,7 @@ +require_dependency 'friendship' + +class Friendship + + has_merit_actions + +end diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index 07ec370..434a945 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -21,11 +21,29 @@ module Merit :to => :author, :value => lambda { |article| article.author.present? ? article.author.articles.count : 0 } }, - :relevant_commenter => { + :positive_votes_received => { :action => 'vote#create', :default_threshold => 5, :to => lambda {|vote| vote.voteable.author}, - :value => lambda { |vote| vote.voteable.kind_of?(Comment) ? Vote.for_voteable(vote.voteable).where('vote > 0').count : 0 } + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote > 0').count } + }, + :negative_votes_received => { + :action => 'vote#create', + :default_threshold => 5, + :to => lambda {|vote| vote.voteable.author}, + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote < 0').count } + }, + :votes_performed => { + :action => 'vote#create', + :default_threshold => 5, + :to => lambda {|vote| vote.voter}, + :value => lambda { |vote| Vote.for_voter(vote.voter).count } + }, + :friendly => { + :action => 'friendship#create', + :default_threshold => 5, + :to => lambda {|friendship| friendship.person}, + :value => lambda { |friendship| friendship.person.friends.count } } } diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 9c654a0..5612c6f 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -82,4 +82,28 @@ class ArticleTest < ActiveSupport::TestCase end end + should 'add badge to author when users like his article' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received') + GamificationPlugin.gamification_set_rules(environment) + + article = create(Article, :name => 'Test', :profile => person, :author => person) + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) } + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) + assert_equal [], person.badges + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) + assert_equal 'positive_votes_received', person.reload.badges.first.name + end + + should 'add badge to author when users dislike his article' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received') + GamificationPlugin.gamification_set_rules(environment) + + article = create(Article, :name => 'Test', :profile => person, :author => person) + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) } + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) + assert_equal [], person.badges + Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) + assert_equal 'negative_votes_received', person.reload.badges.first.name + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index a9f0895..e69e7fa 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -32,8 +32,8 @@ class CommentTest < ActiveSupport::TestCase assert_equal 'comment_author', person.badges.first.name end - should 'add badge to author when users vote in his comment' do - GamificationPlugin::Badge.create!(:owner => environment, :name => 'relevant_commenter') + should 'add badge to author when users like his comment' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received') GamificationPlugin.gamification_set_rules(environment) comment = create(Comment, :source => article, :author_id => person.id) @@ -41,7 +41,19 @@ class CommentTest < ActiveSupport::TestCase Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) assert_equal [], person.badges Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) - assert_equal 'relevant_commenter', person.reload.badges.first.name + assert_equal 'positive_votes_received', person.reload.badges.first.name + end + + should 'add badge to author when users dislike his comment' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received') + GamificationPlugin.gamification_set_rules(environment) + + comment = create(Comment, :source => article, :author_id => person.id) + 4.times { Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) } + Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) + assert_equal [], person.badges + Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) + assert_equal 'negative_votes_received', person.reload.badges.first.name end should 'add merit points to comment owner when an user like his comment' do diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb new file mode 100644 index 0000000..4d1050b --- /dev/null +++ b/test/unit/person_test.rb @@ -0,0 +1,30 @@ +require_relative "../test_helper" + +class PersonTest < ActiveSupport::TestCase + + def setup + @environment = Environment.default + GamificationPlugin.gamification_set_rules(@environment) + @person = create_user('testuser').person + end + attr_accessor :environment, :person + + should 'add badge to a voter' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'votes_performed') + GamificationPlugin.gamification_set_rules(environment) + + 4.times { Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1) } + assert_equal [], person.badges + Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1) + assert_equal 'votes_performed', person.reload.badges.first.name + end + + should 'add badge to a friendly person' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'friendly') + GamificationPlugin.gamification_set_rules(environment) + + 5.times { |i| person.add_friend(create_user("testuser#{i}").person) } + assert_equal 'friendly', person.reload.badges.first.name + end + +end -- libgit2 0.21.2