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 22 default_weight: 50,
23 23 target_profile: lambda {|comment| comment.source.profile },
24 24 target_url: lambda {|comment| comment.url},
  25 + condition: lambda {|comment, profile| comment.source.author != comment.author }
25 26 },
26 27 comment_article: {
27 28 action: 'comment#create',
... ... @@ -31,7 +32,7 @@ module Merit
31 32 description: _('Source article of a comment'),
32 33 default_weight: 50,
33 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 37 comment_community: {
37 38 action: 'comment#create',
... ... @@ -74,6 +75,7 @@ module Merit
74 75 default_weight: 20,
75 76 target_profile: lambda {|vote| vote.voteable.present? ? vote.voteable.profile : nil },
76 77 target_url: lambda {|vote| vote.voteable.url},
  78 + condition: lambda {|vote, profile| vote.voter != vote.voteable.author }
77 79 },
78 80 vote_voteable: {
79 81 action: 'vote#create',
... ...
test/unit/article_test.rb
... ... @@ -51,7 +51,7 @@ class ArticleTest < ActiveSupport::TestCase
51 51  
52 52 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
53 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 55 end
56 56 end
57 57  
... ... @@ -143,7 +143,7 @@ class ArticleTest < ActiveSupport::TestCase
143 143  
144 144 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
145 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 147 end
148 148 end
149 149  
... ... @@ -216,4 +216,15 @@ class ArticleTest < ActiveSupport::TestCase
216 216 assert_equal article, person.score_points.first.action.target_obj
217 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 230 end
... ...
test/unit/comment_test.rb
... ... @@ -71,6 +71,16 @@ class CommentTest < ActiveSupport::TestCase
71 71  
72 72 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
73 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 84 Vote.create!(:voter => person, :voteable => comment, :vote => 1)
75 85 end
76 86 end
... ... @@ -78,7 +88,7 @@ class CommentTest < ActiveSupport::TestCase
78 88 should 'subtract merit points to comment owner when an user unlike his comment' do
79 89 create_point_rule_definition('vote_voteable_author')
80 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 93 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
84 94 assert_difference 'comment.author.points', -1*c.weight do
... ... @@ -92,14 +102,14 @@ class CommentTest < ActiveSupport::TestCase
92 102  
93 103 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
94 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 106 end
97 107 end
98 108  
99 109 should 'add merit points from comment owner when an user remove a dislike in his comment' do
100 110 create_point_rule_definition('vote_voteable_author')
101 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 114 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
105 115 assert_difference 'comment.author.points', c.weight do
... ... @@ -114,6 +124,13 @@ class CommentTest < ActiveSupport::TestCase
114 124 end
115 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 134 should 'add merit points to voter when he likes a comment' do
118 135 create_point_rule_definition('vote_voter')
119 136 comment = create(Comment, :source => article, :author_id => person.id)
... ... @@ -180,7 +197,7 @@ class CommentTest < ActiveSupport::TestCase
180 197  
181 198 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first
182 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 201 end
185 202 end
186 203  
... ... @@ -205,7 +222,7 @@ class CommentTest < ActiveSupport::TestCase
205 222  
206 223 c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first
207 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 226 end
210 227 end
211 228  
... ... @@ -227,7 +244,7 @@ class CommentTest < ActiveSupport::TestCase
227 244 article = create(TextArticle, profile_id: community.id, author_id: author.id)
228 245 rule = create_point_rule_definition('comment_article_author', community)
229 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 248 end
232 249 end
233 250  
... ...