Commit 2195363864f69e172952dcc3ac1ac254e7f65937

Authored by Leandro Santos
2 parents 8f6e8ef9 7c473e44

Merge branch 'working' into 'master'

Generate Merit actions for article_followers and friendship



See merge request !7
lib/merit/badge_rules.rb
@@ -161,7 +161,6 @@ module Merit @@ -161,7 +161,6 @@ module Merit
161 end 161 end
162 # pass source and to for different situations 162 # pass source and to for different situations
163 action = (badge.custom_fields || {}).fetch(s[:action], {}) 163 action = (badge.custom_fields || {}).fetch(s[:action], {})
164 - debugger if source.is_a? Vote  
165 can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i 164 can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i
166 end 165 end
167 can_be_granted 166 can_be_granted
script/process_merit_rules.rb
@@ -24,11 +24,11 @@ end @@ -24,11 +24,11 @@ end
24 24
25 #puts "Destroy all merit actions" 25 #puts "Destroy all merit actions"
26 #Merit::Action.destroy_all 26 #Merit::Action.destroy_all
27 -# 27 +
28 #count = Person.count 28 #count = Person.count
29 #Person.all.each.with_index(1) do |person, i| 29 #Person.all.each.with_index(1) do |person, i|
30 -# puts "#{i}/#{count} Remove sash from #{person.identifier}"  
31 -# person.sash.destroy unless person.sash.nil? 30 + #puts "#{i}/#{count} Remove sash from #{person.identifier}"
  31 + #person.sash.destroy unless person.sash.nil?
32 #end 32 #end
33 33
34 Merit.observers << 'ProcessObserver' 34 Merit.observers << 'ProcessObserver'
@@ -50,13 +50,26 @@ Environment.all.each do |environment| @@ -50,13 +50,26 @@ Environment.all.each do |environment|
50 article.comments.each.with_index(1) do |comment, i| 50 article.comments.each.with_index(1) do |comment, i|
51 create_action(comment, i, comment_count) 51 create_action(comment, i, comment_count)
52 end 52 end
  53 +
  54 + followed_articles_count = article.article_followers.count
  55 + article.article_followers.each_with_index do |af, i|
  56 + create_action(af, i, followed_articles_count)
  57 + end
53 end 58 end
54 59
  60 + people_count = environment.people.count
55 environment.people.each.with_index(1) do |person, person_index| 61 environment.people.each.with_index(1) do |person, person_index|
  62 + create_action(person, person_index, people_count)
  63 +
56 vote_count = person.votes.count 64 vote_count = person.votes.count
57 person.votes.each.with_index(1) do |vote, vote_index| 65 person.votes.each.with_index(1) do |vote, vote_index|
58 create_action(vote, vote_index, vote_count) 66 create_action(vote, vote_index, vote_count)
59 end 67 end
  68 +
  69 + friendship_count = person.friends.count
  70 + person.friends.each_with_index do |friend, index|
  71 + create_action(friend, index, friendship_count)
  72 + end
60 end 73 end
61 74
62 environment.people.each.with_index(1) do |person, person_index| 75 environment.people.each.with_index(1) do |person, person_index|
test/unit/article_follower.rb
@@ -1,126 +0,0 @@ @@ -1,126 +0,0 @@
1 -require_relative "../test_helper"  
2 -  
3 -class ArticleTest < ActiveSupport::TestCase  
4 -  
5 - def setup  
6 - @person = create_user('testuser').person  
7 - @environment = Environment.default  
8 - @community = fast_create(Community)  
9 - end  
10 -  
11 - attr_accessor :person, :environment, :community  
12 -  
13 - should 'add merit points to an article follower by user' do  
14 - create_point_rule_definition('followed_article')  
15 - article = create(TextArticle, :name => 'Test', :profile => community, :author => person)  
16 -  
17 - c = GamificationPlugin::PointsCategorization.for_type(:followed_article).first  
18 - assert_difference 'article.points(:category => c.id.to_s)', c.weight do  
19 - article.person_followers << person  
20 - end  
21 - end  
22 -  
23 - should 'add merit points to follower when it follows an article' do  
24 - create_point_rule_definition('follower')  
25 -  
26 - article = create(TextArticle, :name => 'Test', :profile => community, :author => person)  
27 -  
28 - c = GamificationPlugin::PointsCategorization.for_type(:follower).first  
29 - assert_difference 'person.points(:category => c.id.to_s)', c.weight do  
30 - article.person_followers << person  
31 - end  
32 - end  
33 -  
34 - should "add merit points for article's author followed by an user" do  
35 - create_point_rule_definition('followed_article_author')  
36 -  
37 - article = create(TextArticle, :name => 'Test', :profile => community, :author => person)  
38 -  
39 - c = GamificationPlugin::PointsCategorization.for_type(:followed_article_author).first  
40 - assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do  
41 - article.person_followers << person  
42 - end  
43 - end  
44 -  
45 - should 'subtract merit points to follower when it unfollow an article' do  
46 - create_point_rule_definition('follower')  
47 - follower = create_user('someuser').person  
48 - article = create(TextArticle, :profile_id => community.id, :author => person)  
49 - score_points = follower.score_points.count  
50 - points = follower.points  
51 - article.person_followers << follower  
52 - assert_equal score_points + 1, follower.score_points.count  
53 - ArticleFollower.last.destroy  
54 - assert_equal score_points + 2, follower.score_points.count  
55 - assert_equal points, follower.points  
56 - end  
57 -  
58 - should 'subtract merit points to article author when a user unfollow an article' do  
59 - create_point_rule_definition('follower')  
60 - article = create(TextArticle, :profile_id => community.id, :author => person)  
61 - score_points = person.score_points.count  
62 - points = person.points  
63 - article.person_followers << person  
64 - assert_equal score_points + 1, person.score_points.count  
65 - ArticleFollower.last.destroy  
66 - assert_equal score_points + 2, person.score_points.count  
67 - assert_equal points, person.points  
68 - end  
69 -  
70 - should 'subtract merit points to article when a user unfollow an article' do  
71 - create_point_rule_definition('followed_article')  
72 - article = create(TextArticle, :profile_id => community.id, :author => person)  
73 - score_points = article.score_points.count  
74 - points = article.points  
75 - article.person_followers << person  
76 - assert_equal score_points + 1, article.score_points.count  
77 - ArticleFollower.last.destroy  
78 - assert_equal score_points + 2, article.score_points.count  
79 - assert_equal points, article.points  
80 - end  
81 -  
82 -#FIXME make tests for badges generete with article follower actions  
83 -#  
84 -# should 'add badge to author when users like his article' do  
85 -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')  
86 -# GamificationPlugin.gamification_set_rules(environment)  
87 -#  
88 -# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
89 -# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) }  
90 -# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)  
91 -# assert_equal [], person.badges  
92 -# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)  
93 -# assert_equal 'positive_votes_received', person.reload.badges.first.name  
94 -# end  
95 -#  
96 -# should 'add badge to author when users dislike his article' do  
97 -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')  
98 -# GamificationPlugin.gamification_set_rules(environment)  
99 -#  
100 -# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
101 -# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) }  
102 -# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)  
103 -# assert_equal [], person.badges  
104 -# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)  
105 -# assert_equal 'negative_votes_received', person.reload.badges.first.name  
106 -# end  
107 -#  
108 -# should 'add merit badge to author when create 5 new articles' do  
109 -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)  
110 -# GamificationPlugin.gamification_set_rules(environment)  
111 -#  
112 -# 5.times { create(TextArticle, :profile_id => person.id, :author => person) }  
113 -# assert_equal 'article_author', person.badges.first.name  
114 -# assert_equal 1, person.badges.first.level  
115 -# end  
116 -#  
117 -# should 'add merit badge level 2 to author when create 10 new articles' do  
118 -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)  
119 -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})  
120 -# GamificationPlugin.gamification_set_rules(environment)  
121 -#  
122 -# 10.times { create(TextArticle, :profile_id => person.id, :author => person) }  
123 -# assert_equal ['article_author'], person.badges.map(&:name).uniq  
124 -# assert_equal [1, 2], person.badges.map(&:level)  
125 -# end  
126 -end  
test/unit/article_follower_test.rb 0 → 100644
@@ -0,0 +1,126 @@ @@ -0,0 +1,126 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class ArticleTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @person = create_user('testuser').person
  7 + @environment = Environment.default
  8 + @community = fast_create(Community)
  9 + end
  10 +
  11 + attr_accessor :person, :environment, :community
  12 +
  13 + should 'add merit points to an article follower by user' do
  14 + create_point_rule_definition('followed_article')
  15 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  16 +
  17 + c = GamificationPlugin::PointsCategorization.for_type(:followed_article).first
  18 + assert_difference 'article.points(:category => c.id.to_s)', c.weight do
  19 + article.person_followers << person
  20 + end
  21 + end
  22 +
  23 + should 'add merit points to follower when it follows an article' do
  24 + create_point_rule_definition('follower')
  25 +
  26 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  27 +
  28 + c = GamificationPlugin::PointsCategorization.for_type(:follower).first
  29 + assert_difference 'person.points(:category => c.id.to_s)', c.weight do
  30 + article.person_followers << person
  31 + end
  32 + end
  33 +
  34 + should "add merit points for article's author followed by an user" do
  35 + create_point_rule_definition('followed_article_author')
  36 +
  37 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  38 +
  39 + c = GamificationPlugin::PointsCategorization.for_type(:followed_article_author).first
  40 + assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
  41 + article.person_followers << person
  42 + end
  43 + end
  44 +
  45 + should 'subtract merit points to follower when it unfollow an article' do
  46 + create_point_rule_definition('follower')
  47 + follower = create_user('someuser').person
  48 + article = create(TextArticle, :profile_id => community.id, :author => person)
  49 + score_points = follower.score_points.count
  50 + points = follower.points
  51 + article.person_followers << follower
  52 + assert_equal score_points + 1, follower.score_points.count
  53 + ArticleFollower.last.destroy
  54 + assert_equal score_points + 2, follower.score_points.count
  55 + assert_equal points, follower.points
  56 + end
  57 +
  58 + should 'subtract merit points to article author when a user unfollow an article' do
  59 + create_point_rule_definition('follower')
  60 + article = create(TextArticle, :profile_id => community.id, :author => person)
  61 + score_points = person.score_points.count
  62 + points = person.points
  63 + article.person_followers << person
  64 + assert_equal score_points + 1, person.score_points.count
  65 + ArticleFollower.last.destroy
  66 + assert_equal score_points + 2, person.score_points.count
  67 + assert_equal points, person.points
  68 + end
  69 +
  70 + should 'subtract merit points to article when a user unfollow an article' do
  71 + create_point_rule_definition('followed_article')
  72 + article = create(TextArticle, :profile_id => community.id, :author => person)
  73 + score_points = article.score_points.count
  74 + points = article.points
  75 + article.person_followers << person
  76 + assert_equal score_points + 1, article.score_points.count
  77 + ArticleFollower.last.destroy
  78 + assert_equal score_points + 2, article.score_points.count
  79 + assert_equal points, article.points
  80 + end
  81 +
  82 +#FIXME make tests for badges generete with article follower actions
  83 +#
  84 +# should 'add badge to author when users like his article' do
  85 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')
  86 +# GamificationPlugin.gamification_set_rules(environment)
  87 +#
  88 +# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)
  89 +# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) }
  90 +# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
  91 +# assert_equal [], person.badges
  92 +# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
  93 +# assert_equal 'positive_votes_received', person.reload.badges.first.name
  94 +# end
  95 +#
  96 +# should 'add badge to author when users dislike his article' do
  97 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')
  98 +# GamificationPlugin.gamification_set_rules(environment)
  99 +#
  100 +# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)
  101 +# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) }
  102 +# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
  103 +# assert_equal [], person.badges
  104 +# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
  105 +# assert_equal 'negative_votes_received', person.reload.badges.first.name
  106 +# end
  107 +#
  108 +# should 'add merit badge to author when create 5 new articles' do
  109 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
  110 +# GamificationPlugin.gamification_set_rules(environment)
  111 +#
  112 +# 5.times { create(TextArticle, :profile_id => person.id, :author => person) }
  113 +# assert_equal 'article_author', person.badges.first.name
  114 +# assert_equal 1, person.badges.first.level
  115 +# end
  116 +#
  117 +# should 'add merit badge level 2 to author when create 10 new articles' do
  118 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
  119 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})
  120 +# GamificationPlugin.gamification_set_rules(environment)
  121 +#
  122 +# 10.times { create(TextArticle, :profile_id => person.id, :author => person) }
  123 +# assert_equal ['article_author'], person.badges.map(&:name).uniq
  124 +# assert_equal [1, 2], person.badges.map(&:level)
  125 +# end
  126 +end
test/unit/article_test.rb
@@ -137,97 +137,57 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -137,97 +137,57 @@ class ArticleTest &lt; ActiveSupport::TestCase
137 assert_equal 0, person.points 137 assert_equal 0, person.points
138 end 138 end
139 139
140 - #should 'add merit badge to author when create 5 new articles on community' do  
141 - #GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)  
142 - #GamificationPlugin.gamification_set_rules(environment)  
143 -  
144 - #5.times { create(TextArticle, :profile_id => person.id, :author => person) }  
145 - #assert_equal 'article_author', person.badges.first.name  
146 - #assert_equal 1, person.badges.first.level  
147 - #end  
148 -  
149 - #should 'add merit badge level 2 to author when create 10 new articles on community' do  
150 - #GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)  
151 - #GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})  
152 - #GamificationPlugin.gamification_set_rules(environment)  
153 -  
154 - #10.times { create(TextArticle, :profile_id => person.id, :author => person) }  
155 - #assert_equal ['article_author'], person.badges.map(&:name).uniq  
156 - #assert_equal [1, 2], person.badges.map(&:level)  
157 - #end  
158 -  
159 - #should 'add merit points to community article owner when an user like it on community' do  
160 - #create_point_rule_definition('vote_voteable_author')  
161 - #community = fast_create(Community)  
162 - #article = create(TextArticle, :name => 'Test', :profile => community, :author => person)  
163 -  
164 - #c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first  
165 - #assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do  
166 - #Vote.create!(:voter => person, :voteable => article, :vote => 1)  
167 - #end  
168 - #end  
169 -  
170 - #should 'add merit points to article when an user like it on community' do  
171 - #create_point_rule_definition('vote_voteable')  
172 - #article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
173 - #article = article.reload  
174 -  
175 - #c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).first  
176 - #assert_difference 'article.points(:category => c.id.to_s)', c.weight do  
177 - #Vote.create!(:voter => person, :voteable => article, :vote => 1)  
178 - #end  
179 - #end  
180 -  
181 - #should 'add merit points to community when create a new article on community' do  
182 - #create_point_rule_definition('article_community')  
183 - #community = fast_create(Community)  
184 - #assert_difference 'community.score_points.count' do  
185 - #create(TextArticle, :profile_id => community.id, :author => person)  
186 - #end  
187 - #end  
188 -  
189 - #should 'add merit points to voter when he likes an article on community' do  
190 - #create_point_rule_definition('vote_voter')  
191 - #article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
192 -  
193 - #c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first  
194 - #assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do  
195 - #Vote.create!(:voter => person, :voteable => article, :vote => 1)  
196 - #end  
197 - #end  
198 -  
199 - #should 'add merit points to voter when he dislikes an article on community' do  
200 - #create_point_rule_definition('vote_voter')  
201 - #article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
202 -  
203 - #c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first  
204 - #assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do  
205 - #Vote.create!(:voter => person, :voteable => article, :vote => -1)  
206 - #end  
207 - #end  
208 -  
209 - #should 'add badge to author when users like his article on community' do  
210 - #GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')  
211 - #GamificationPlugin.gamification_set_rules(environment)  
212 -  
213 - #article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
214 - #4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) }  
215 - #Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)  
216 - #assert_equal [], person.badges  
217 - #Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)  
218 - #assert_equal 'positive_votes_received', person.reload.badges.first.name  
219 - #end  
220 -  
221 - #should 'add badge to author when users dislike his article on community' do  
222 - #GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')  
223 - #GamificationPlugin.gamification_set_rules(environment)  
224 -  
225 - #article = create(TextArticle, :name => 'Test', :profile => person, :author => person)  
226 - #4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) }  
227 - #Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)  
228 - #assert_equal [], person.badges  
229 - #Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)  
230 - #assert_equal 'negative_votes_received', person.reload.badges.first.name  
231 - #end 140 + should 'add merit points to community article owner when an user like it on community' do
  141 + community = fast_create(Community)
  142 + create_point_rule_definition('vote_voteable_author', community)
  143 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  144 +
  145 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first
  146 + assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
  147 + Vote.create!(:voter => person, :voteable => article, :vote => 1)
  148 + end
  149 + end
  150 +
  151 + should 'add merit points to article when an user like it on community' do
  152 + community = fast_create(Community)
  153 + create_point_rule_definition('vote_voteable', community)
  154 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  155 + article = article.reload
  156 +
  157 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).first
  158 + assert_difference 'article.points(:category => c.id.to_s)', c.weight do
  159 + Vote.create!(:voter => person, :voteable => article, :vote => 1)
  160 + end
  161 + end
  162 +
  163 + should 'add merit points to community when create a new article on community' do
  164 + community = fast_create(Community)
  165 + create_point_rule_definition('article_community', community)
  166 + assert_difference 'community.score_points.count' do
  167 + create(TextArticle, :profile_id => community.id, :author => person)
  168 + end
  169 + end
  170 +
  171 + should 'add merit points to voter when he likes an article on community' do
  172 + community = fast_create(Community)
  173 + create_point_rule_definition('vote_voter', community)
  174 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  175 +
  176 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first
  177 + assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
  178 + Vote.create!(:voter => person, :voteable => article, :vote => 1)
  179 + end
  180 + end
  181 +
  182 + should 'add merit points to voter when he dislikes an article on community' do
  183 + community = fast_create(Community)
  184 + create_point_rule_definition('vote_voter', community)
  185 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  186 +
  187 + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first
  188 + assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
  189 + Vote.create!(:voter => person, :voteable => article, :vote => -1)
  190 + end
  191 + end
232 192
233 end 193 end