From b3c3e70b4c3ca5e90b77d2e8bd38c72eb978aa75 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 18 Feb 2016 18:09:35 -0300 Subject: [PATCH] Do not give points to an author when he vote or comment in his content --- lib/merit/point_rules.rb | 4 +++- test/unit/article_test.rb | 15 +++++++++++++-- test/unit/comment_test.rb | 29 +++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index fca7ea2..72f702c 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -22,6 +22,7 @@ module Merit default_weight: 50, target_profile: lambda {|comment| comment.source.profile }, target_url: lambda {|comment| comment.url}, + condition: lambda {|comment, profile| comment.source.author != comment.author } }, comment_article: { action: 'comment#create', @@ -31,7 +32,7 @@ module Merit description: _('Source article of a comment'), default_weight: 50, target_profile: lambda {|comment| comment.source.profile }, - target_url: lambda {|comment| comment.url}, + target_url: lambda {|comment| comment.url} }, comment_community: { action: 'comment#create', @@ -74,6 +75,7 @@ module Merit default_weight: 20, target_profile: lambda {|vote| vote.voteable.present? ? vote.voteable.profile : nil }, target_url: lambda {|vote| vote.voteable.url}, + condition: lambda {|vote, profile| vote.voter != vote.voteable.author } }, vote_voteable: { action: 'vote#create', diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 64512bc..c21d1d9 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -51,7 +51,7 @@ class ArticleTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do - Vote.create!(:voter => person, :voteable => article, :vote => 1) + Vote.create!(:voter => create_user.person, :voteable => article, :vote => 1) end end @@ -143,7 +143,7 @@ class ArticleTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do - Vote.create!(:voter => person, :voteable => article, :vote => 1) + Vote.create!(:voter => create_user.person, :voteable => article, :vote => 1) end end @@ -216,4 +216,15 @@ class ArticleTest < ActiveSupport::TestCase assert_equal article, person.score_points.first.action.target_obj end + should 'not add merit points to article author when he likes his article' do + create_point_rule_definition('vote_voteable_author') + community = fast_create(Community) + article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first + assert_no_difference 'article.author.points(:category => c.id.to_s)' do + Vote.create!(:voter => person, :voteable => article, :vote => 1) + end + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index b86cfa1..cde76bf 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -71,6 +71,16 @@ class CommentTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1) + end + end + + should 'not add merit points to comment owner when he likes his comment' do + create_point_rule_definition('vote_voteable_author') + comment = create(Comment, :source => article, :author_id => person.id) + + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first + assert_difference 'comment.author.points(:category => c.id.to_s)', 0 do Vote.create!(:voter => person, :voteable => comment, :vote => 1) end end @@ -78,7 +88,7 @@ class CommentTest < ActiveSupport::TestCase should 'subtract merit points to comment owner when an user unlike his comment' do create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => author.id) - Vote.create!(:voter => person, :voteable => comment, :vote => 1) + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1) c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points', -1*c.weight do @@ -92,14 +102,14 @@ class CommentTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do - Vote.create!(:voter => person, :voteable => comment, :vote => -1) + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1) end end should 'add merit points from comment owner when an user remove a dislike in his comment' do create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => author.id) - Vote.create!(:voter => person, :voteable => comment, :vote => -1) + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1) c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points', c.weight do @@ -114,6 +124,13 @@ class CommentTest < ActiveSupport::TestCase end end + should 'do not add merit points when the comment author is the article author' do + create_point_rule_definition('comment_article_author') + assert_difference 'author.score_points.count' do + create(Comment, :source => article, :author_id => article.author.id) + end + end + should 'add merit points to voter when he likes a comment' do create_point_rule_definition('vote_voter') comment = create(Comment, :source => article, :author_id => person.id) @@ -180,7 +197,7 @@ class CommentTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do - Vote.create!(:voter => person, :voteable => comment, :vote => 1) + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1) end end @@ -205,7 +222,7 @@ class CommentTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do - Vote.create!(:voter => person, :voteable => comment, :vote => -1) + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1) end end @@ -227,7 +244,7 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: author.id) rule = create_point_rule_definition('comment_article_author', community) assert_difference 'author.points_by_profile(community.identifier)', rule.weight do - create(Comment, :source => article, :author_id => author.id) + create(Comment, :source => article, :author_id => person.id) end end -- libgit2 0.21.2