Commit b3c3e70b4c3ca5e90b77d2e8bd38c72eb978aa75

Authored by Victor Costa
1 parent 287929d6
Exists in master

Do not give points to an author when he vote or comment in his content

lib/merit/point_rules.rb
@@ -22,6 +22,7 @@ module Merit @@ -22,6 +22,7 @@ module Merit
22 default_weight: 50, 22 default_weight: 50,
23 target_profile: lambda {|comment| comment.source.profile }, 23 target_profile: lambda {|comment| comment.source.profile },
24 target_url: lambda {|comment| comment.url}, 24 target_url: lambda {|comment| comment.url},
  25 + condition: lambda {|comment, profile| comment.source.author != comment.author }
25 }, 26 },
26 comment_article: { 27 comment_article: {
27 action: 'comment#create', 28 action: 'comment#create',
@@ -31,7 +32,7 @@ module Merit @@ -31,7 +32,7 @@ module Merit
31 description: _('Source article of a comment'), 32 description: _('Source article of a comment'),
32 default_weight: 50, 33 default_weight: 50,
33 target_profile: lambda {|comment| comment.source.profile }, 34 target_profile: lambda {|comment| comment.source.profile },
34 - target_url: lambda {|comment| comment.url}, 35 + target_url: lambda {|comment| comment.url}
35 }, 36 },
36 comment_community: { 37 comment_community: {
37 action: 'comment#create', 38 action: 'comment#create',
@@ -74,6 +75,7 @@ module Merit @@ -74,6 +75,7 @@ module Merit
74 default_weight: 20, 75 default_weight: 20,
75 target_profile: lambda {|vote| vote.voteable.present? ? vote.voteable.profile : nil }, 76 target_profile: lambda {|vote| vote.voteable.present? ? vote.voteable.profile : nil },
76 target_url: lambda {|vote| vote.voteable.url}, 77 target_url: lambda {|vote| vote.voteable.url},
  78 + condition: lambda {|vote, profile| vote.voter != vote.voteable.author }
77 }, 79 },
78 vote_voteable: { 80 vote_voteable: {
79 action: 'vote#create', 81 action: 'vote#create',
test/unit/article_test.rb
@@ -51,7 +51,7 @@ class ArticleTest < ActiveSupport::TestCase @@ -51,7 +51,7 @@ class ArticleTest < ActiveSupport::TestCase
51 51
52 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 52 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
53 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do 53 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
54 - Vote.create!(:voter => person, :voteable => article, :vote => 1) 54 + Vote.create!(:voter => create_user.person, :voteable => article, :vote => 1)
55 end 55 end
56 end 56 end
57 57
@@ -143,7 +143,7 @@ class ArticleTest < ActiveSupport::TestCase @@ -143,7 +143,7 @@ class ArticleTest < ActiveSupport::TestCase
143 143
144 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 144 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
145 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do 145 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
146 - Vote.create!(:voter => person, :voteable => article, :vote => 1) 146 + Vote.create!(:voter => create_user.person, :voteable => article, :vote => 1)
147 end 147 end
148 end 148 end
149 149
@@ -216,4 +216,15 @@ class ArticleTest < ActiveSupport::TestCase @@ -216,4 +216,15 @@ class ArticleTest < ActiveSupport::TestCase
216 assert_equal article, person.score_points.first.action.target_obj 216 assert_equal article, person.score_points.first.action.target_obj
217 end 217 end
218 218
  219 + should 'not add merit points to article author when he likes his article' do
  220 + create_point_rule_definition('vote_voteable_author')
  221 + community = fast_create(Community)
  222 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  223 +
  224 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
  225 + assert_no_difference 'article.author.points(:category => c.id.to_s)' do
  226 + Vote.create!(:voter => person, :voteable => article, :vote => 1)
  227 + end
  228 + end
  229 +
219 end 230 end
test/unit/comment_test.rb
@@ -71,6 +71,16 @@ class CommentTest < ActiveSupport::TestCase @@ -71,6 +71,16 @@ class CommentTest < ActiveSupport::TestCase
71 71
72 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 72 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
73 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do 73 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do
  74 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1)
  75 + end
  76 + end
  77 +
  78 + should 'not add merit points to comment owner when he likes his comment' do
  79 + create_point_rule_definition('vote_voteable_author')
  80 + comment = create(Comment, :source => article, :author_id => person.id)
  81 +
  82 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
  83 + assert_difference 'comment.author.points(:category => c.id.to_s)', 0 do
74 Vote.create!(:voter => person, :voteable => comment, :vote => 1) 84 Vote.create!(:voter => person, :voteable => comment, :vote => 1)
75 end 85 end
76 end 86 end
@@ -78,7 +88,7 @@ class CommentTest < ActiveSupport::TestCase @@ -78,7 +88,7 @@ class CommentTest < ActiveSupport::TestCase
78 should 'subtract merit points to comment owner when an user unlike his comment' do 88 should 'subtract merit points to comment owner when an user unlike his comment' do
79 create_point_rule_definition('vote_voteable_author') 89 create_point_rule_definition('vote_voteable_author')
80 comment = create(Comment, :source => article, :author_id => author.id) 90 comment = create(Comment, :source => article, :author_id => author.id)
81 - Vote.create!(:voter => person, :voteable => comment, :vote => 1) 91 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1)
82 92
83 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 93 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
84 assert_difference 'comment.author.points', -1*c.weight do 94 assert_difference 'comment.author.points', -1*c.weight do
@@ -92,14 +102,14 @@ class CommentTest < ActiveSupport::TestCase @@ -92,14 +102,14 @@ class CommentTest < ActiveSupport::TestCase
92 102
93 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 103 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
94 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do 104 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do
95 - Vote.create!(:voter => person, :voteable => comment, :vote => -1) 105 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1)
96 end 106 end
97 end 107 end
98 108
99 should 'add merit points from comment owner when an user remove a dislike in his comment' do 109 should 'add merit points from comment owner when an user remove a dislike in his comment' do
100 create_point_rule_definition('vote_voteable_author') 110 create_point_rule_definition('vote_voteable_author')
101 comment = create(Comment, :source => article, :author_id => author.id) 111 comment = create(Comment, :source => article, :author_id => author.id)
102 - Vote.create!(:voter => person, :voteable => comment, :vote => -1) 112 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1)
103 113
104 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first 114 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
105 assert_difference 'comment.author.points', c.weight do 115 assert_difference 'comment.author.points', c.weight do
@@ -114,6 +124,13 @@ class CommentTest < ActiveSupport::TestCase @@ -114,6 +124,13 @@ class CommentTest < ActiveSupport::TestCase
114 end 124 end
115 end 125 end
116 126
  127 + should 'do not add merit points when the comment author is the article author' do
  128 + create_point_rule_definition('comment_article_author')
  129 + assert_difference 'author.score_points.count' do
  130 + create(Comment, :source => article, :author_id => article.author.id)
  131 + end
  132 + end
  133 +
117 should 'add merit points to voter when he likes a comment' do 134 should 'add merit points to voter when he likes a comment' do
118 create_point_rule_definition('vote_voter') 135 create_point_rule_definition('vote_voter')
119 comment = create(Comment, :source => article, :author_id => person.id) 136 comment = create(Comment, :source => article, :author_id => person.id)
@@ -180,7 +197,7 @@ class CommentTest < ActiveSupport::TestCase @@ -180,7 +197,7 @@ class CommentTest < ActiveSupport::TestCase
180 197
181 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first 198 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first
182 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do 199 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do
183 - Vote.create!(:voter => person, :voteable => comment, :vote => 1) 200 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => 1)
184 end 201 end
185 end 202 end
186 203
@@ -205,7 +222,7 @@ class CommentTest < ActiveSupport::TestCase @@ -205,7 +222,7 @@ class CommentTest < ActiveSupport::TestCase
205 222
206 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first 223 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first
207 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do 224 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do
208 - Vote.create!(:voter => person, :voteable => comment, :vote => -1) 225 + Vote.create!(:voter => create_user.person, :voteable => comment, :vote => -1)
209 end 226 end
210 end 227 end
211 228
@@ -227,7 +244,7 @@ class CommentTest < ActiveSupport::TestCase @@ -227,7 +244,7 @@ class CommentTest < ActiveSupport::TestCase
227 article = create(TextArticle, profile_id: community.id, author_id: author.id) 244 article = create(TextArticle, profile_id: community.id, author_id: author.id)
228 rule = create_point_rule_definition('comment_article_author', community) 245 rule = create_point_rule_definition('comment_article_author', community)
229 assert_difference 'author.points_by_profile(community.identifier)', rule.weight do 246 assert_difference 'author.points_by_profile(community.identifier)', rule.weight do
230 - create(Comment, :source => article, :author_id => author.id) 247 + create(Comment, :source => article, :author_id => person.id)
231 end 248 end
232 end 249 end
233 250