yeshoua_crm 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +11 -0
  3. data/lib/yeshoua_crm.rb +87 -0
  4. data/lib/yeshoua_crm/acts_as_draftable/draft.rb +40 -0
  5. data/lib/yeshoua_crm/acts_as_draftable/rcrm_acts_as_draftable.rb +154 -0
  6. data/lib/yeshoua_crm/acts_as_list/list.rb +282 -0
  7. data/lib/yeshoua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb +350 -0
  8. data/lib/yeshoua_crm/acts_as_taggable/tag.rb +81 -0
  9. data/lib/yeshoua_crm/acts_as_taggable/tag_list.rb +111 -0
  10. data/lib/yeshoua_crm/acts_as_taggable/tagging.rb +16 -0
  11. data/lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb +274 -0
  12. data/lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb +80 -0
  13. data/lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_voter.rb +20 -0
  14. data/lib/yeshoua_crm/acts_as_votable/votable.rb +323 -0
  15. data/lib/yeshoua_crm/acts_as_votable/vote.rb +28 -0
  16. data/lib/yeshoua_crm/acts_as_votable/voter.rb +131 -0
  17. data/lib/yeshoua_crm/assets_manager.rb +43 -0
  18. data/lib/yeshoua_crm/currency.rb +439 -0
  19. data/lib/yeshoua_crm/currency/formatting.rb +224 -0
  20. data/lib/yeshoua_crm/currency/heuristics.rb +151 -0
  21. data/lib/yeshoua_crm/currency/loader.rb +24 -0
  22. data/lib/yeshoua_crm/helpers/external_assets_helper.rb +17 -0
  23. data/lib/yeshoua_crm/helpers/form_tag_helper.rb +123 -0
  24. data/lib/yeshoua_crm/helpers/tags_helper.rb +13 -0
  25. data/lib/yeshoua_crm/helpers/vote_helper.rb +35 -0
  26. data/lib/yeshoua_crm/liquid/drops/cells_drop.rb +86 -0
  27. data/lib/yeshoua_crm/liquid/drops/issues_drop.rb +66 -0
  28. data/lib/yeshoua_crm/liquid/drops/news_drop.rb +54 -0
  29. data/lib/yeshoua_crm/liquid/drops/users_drop.rb +72 -0
  30. data/lib/yeshoua_crm/liquid/filters/arrays.rb +177 -0
  31. data/lib/yeshoua_crm/liquid/filters/base.rb +208 -0
  32. data/lib/yeshoua_crm/money_helper.rb +65 -0
  33. data/lib/yeshoua_crm/version.rb +3 -0
  34. data/test/acts_as_draftable/draft_test.rb +29 -0
  35. data/test/acts_as_draftable/rcrm_acts_as_draftable_test.rb +185 -0
  36. data/test/acts_as_taggable/rcrm_acts_as_taggable_test.rb +345 -0
  37. data/test/acts_as_taggable/tag_list_test.rb +34 -0
  38. data/test/acts_as_taggable/tag_test.rb +72 -0
  39. data/test/acts_as_taggable/tagging_test.rb +15 -0
  40. data/test/acts_as_viewed/rcrm_acts_as_viewed_test.rb +47 -0
  41. data/test/acts_as_votable/rcrm_acts_as_votable_test.rb +19 -0
  42. data/test/acts_as_votable/rcrm_acts_as_voter_test.rb +14 -0
  43. data/test/acts_as_votable/votable_test.rb +507 -0
  44. data/test/acts_as_votable/voter_test.rb +296 -0
  45. data/test/currency_test.rb +292 -0
  46. data/test/liquid/drops/issues_drop_test.rb +34 -0
  47. data/test/liquid/drops/news_drop_test.rb +38 -0
  48. data/test/liquid/drops/projects_drop_test.rb +44 -0
  49. data/test/liquid/drops/uses_drop_test.rb +36 -0
  50. data/test/liquid/filters/arrays_filter_test.rb +24 -0
  51. data/test/liquid/filters/base_filter_test.rb +63 -0
  52. data/test/liquid/liquid_helper.rb +32 -0
  53. data/test/models/issue.rb +14 -0
  54. data/test/models/news.rb +3 -0
  55. data/test/models/project.rb +8 -0
  56. data/test/models/user.rb +11 -0
  57. data/test/models/vote_classes.rb +33 -0
  58. data/test/money_helper_test.rb +12 -0
  59. data/test/schema.rb +121 -0
  60. data/test/tags_helper_test.rb +29 -0
  61. data/test/test_helper.rb +66 -0
  62. data/test/vote_helper_test.rb +28 -0
  63. data/yeshoua_crm.gemspec +28 -0
  64. metadata +206 -0
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module YeshouaCrm
4
+ module ActsAsTaggable
5
+ class TaggingTest < ActiveSupport::TestCase
6
+ def test_tag
7
+ assert_equal tags(:error), taggings(:tag_for_error).tag
8
+ end
9
+
10
+ def test_taggable
11
+ assert_equal issues(:first_issue), taggings(:tag_for_error).taggable
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RcrmActsAsViewedTest < ActiveSupport::TestCase
4
+ def user
5
+ users(:jonathan)
6
+ end
7
+
8
+ def issue
9
+ issues(:first_issue)
10
+ end
11
+
12
+ def test_zero_of_view_count
13
+ assert_equal issue.view_count, '0(0)'
14
+ end
15
+
16
+ def test_can_view
17
+ issue.view '127.0.0.1', user
18
+ assert_equal issue.view_count, '1(1)'
19
+ # second view change only total count
20
+ issue.view '127.0.0.1', user
21
+ assert_equal issue.view_count, '2(1)'
22
+ end
23
+
24
+ def test_viewed_by
25
+ assert !issue.viewed_by?('127.0.0.1', user)
26
+ issue.view '127.0.0.1', user
27
+ assert issue.viewed_by?('127.0.0.1', user)
28
+ end
29
+
30
+ def test_twice_view
31
+ issue.view '127.0.0.1', user
32
+ issue.view '127.0.0.1', user
33
+ assert_equal '2(1)', issue.view_count
34
+ end
35
+
36
+ def test_viewed?
37
+ assert !issue.viewed?
38
+ issue.view '127.0.0.1', user
39
+ assert issue.viewed?
40
+ end
41
+
42
+ def test_find_viewed_by
43
+ assert_equal [], Issue.find_viewed_by(user)
44
+ issue.view '127.0.0.1', user
45
+ assert_equal [issue], Issue.find_viewed_by(user)
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RcrmActsAsVotableTest < ActiveSupport::TestCase
4
+
5
+ class NotVotable < ActiveRecord::Base; end
6
+ def test_that_votable_returns_false_unless_included
7
+ assert_equal NotVotable.votable?, false
8
+ end
9
+
10
+ def test_that_votable_returns_true_if_included
11
+ assert_equal Votable.votable?, true
12
+ end
13
+
14
+ def test_behaves_like_votable_model
15
+ assert Voter.create(:name => 'i can vote!')
16
+ assert VotableCache.create(:name => 'voting model with cache')
17
+ assert VotableVoter.create(:name => 'i can vote too!')
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RcrmActsAsVoterTest < ActiveSupport::TestCase
4
+
5
+ class NotVoter < ActiveRecord::Base; end
6
+ def test_that_voter_returns_false_unless_included
7
+ assert_equal NotVoter.voter?, false
8
+ end
9
+
10
+ def test_that_voter_returns_true_if_included
11
+ assert_equal Voter.voter?, true
12
+ assert_equal VotableVoter.voter?, true
13
+ end
14
+ end
@@ -0,0 +1,507 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class VotableTest < ActiveSupport::TestCase
4
+ def votable
5
+ votables(:votable)
6
+ end
7
+
8
+ def test_vote_without_voter
9
+ assert !votables(:votable).vote_by
10
+ end
11
+
12
+ def test_have_vote_when_saved
13
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
14
+ assert_equal votable.votes_for.size, 1
15
+ end
16
+
17
+ def test_have_vote_when_saved_by_id
18
+ votable.vote_by(:voter => voters(:voter), :vote => "yes", :vote_ip => '127.0.0.1')
19
+ assert_equal votable.votes_for.size, 1
20
+ assert_equal votable.votes_for.last.vote_ip, '127.0.0.1'
21
+ end
22
+
23
+ def test_voted_twice_by_one_ip
24
+ votable.vote_by(:voter => voters(:voter), :vote => "yes", :vote_ip => '127.0.0.1', :vote_by_ip => true)
25
+ votable.vote_by(:voter => voters(:voter), :vote => "no", :vote_ip => '127.0.0.1', :vote_by_ip => true)
26
+ assert_equal votable.votes_for.size, 1
27
+ end
28
+
29
+ def test_voted_twice_by_one_ip_with_duplicate_params
30
+ votable.vote_by(:voter => voters(:voter), :vote => "yes", :vote_ip => '127.0.0.1', :vote_by_ip => true)
31
+ votable.vote_by(:voter => voters(:voter), :vote => "no", :vote_ip => '127.0.0.1', :vote_by_ip => true, :duplicate => true)
32
+ assert_equal votable.votes_for.size, 2
33
+ end
34
+
35
+ def test_one_scoped_vote_by_ip_when_using_scope_by_same_person
36
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank', :vote_ip => '127.0.0.1', :vote_by_ip => true)
37
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank', :vote_ip => '127.0.0.1', :vote_by_ip => true)
38
+ assert_equal votable.find_votes_for(:vote_scope => 'rank').size, 1
39
+ end
40
+
41
+ def test_two_votes_for_when_voting_on_two_diff_scopes
42
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'weekly_rank', :vote_ip => '127.0.0.1', :vote_by_ip => true)
43
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'monthly_rank', :vote_ip => '127.0.0.1', :vote_by_ip => true)
44
+ assert_equal votable.votes_for.size, 2
45
+ end
46
+
47
+ def test_voted_twice_by_same_person
48
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
49
+ votable.vote_by(:voter => voters(:voter), :vote => "no")
50
+ assert_equal votable.votes_for.size, 1
51
+ end
52
+
53
+ def test_voted_twice_by_same_person_with_duplicate_params
54
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
55
+ votable.vote_by(:voter => voters(:voter), :vote => "no", :duplicate => true)
56
+ assert_equal votable.votes_for.size, 2
57
+ end
58
+
59
+ def test_one_scoped_vote
60
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
61
+ assert_equal votable.find_votes_for(:vote_scope => 'rank').size, 1
62
+ end
63
+
64
+ def test_one_scoped_vote_when_using_scope_by_same_person
65
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
66
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
67
+ assert_equal votable.find_votes_for(:vote_scope => 'rank').size, 1
68
+ end
69
+
70
+ def test_two_votes_for_when_voting_on_two_diff_scopes
71
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'weekly_rank')
72
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'monthly_rank')
73
+ assert_equal votable.votes_for.size, 2
74
+ end
75
+
76
+ def test_called_with_vote_up
77
+ votables(:votable).vote_up voters(:voter)
78
+ assert_equal votables(:votable).get_up_votes.first.voter, voters(:voter)
79
+ end
80
+
81
+ def test_called_with_vote_down
82
+ votables(:votable).vote_down voters(:voter)
83
+ assert_equal votables(:votable).get_down_votes.first.voter, voters(:voter)
84
+ end
85
+
86
+ def test_have_two_votes_when_voted_two_different_people
87
+ votable.vote_by(:voter => voters(:voter))
88
+ votable.vote_by(:voter => voters(:voter2))
89
+ assert_equal votable.votes_for.size, 2
90
+ end
91
+
92
+ def test_one_true_vote
93
+ votable.vote_by(:voter => voters(:voter))
94
+ votable.vote_by(:voter => voters(:voter2), :vote => "dislike")
95
+ assert_equal votable.get_up_votes.size, 1
96
+ end
97
+
98
+ def test_two_false_votes
99
+ votable.vote_by(:voter => voters(:voter), :vote => 'no')
100
+ votable.vote_by(:voter => voters(:voter2), :vote => "dislike")
101
+ assert_equal votable.get_down_votes.size, 2
102
+ end
103
+
104
+ def test_have_been_voted_on_by_voter2
105
+ votable.vote_by(:voter => voters(:voter2), :vote => true)
106
+ assert_equal votable.find_votes_for.first.voter.id, voters(:voter2).id
107
+ end
108
+
109
+ def test_count_the_vote_as_registered_if_this_the_voters_first_vote
110
+ votable.vote_by(:voter => voters(:voter))
111
+ assert votable.vote_registered?
112
+ end
113
+
114
+ def test_not_count_the_vote_as_being_registered_if_that_voter_has_already_voted_and_voted_has_not_changed
115
+ votable.vote_by(:voter => voters(:voter), :vote => true)
116
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes')
117
+ assert !votable.vote_registered?
118
+ end
119
+
120
+ def test_count_the_vote_as_registered_if_the_voter_has_voted_and_the_flag_has_changed
121
+ votable.vote_by(:voter => voters(:voter), :vote => true)
122
+ votable.vote_by(:voter => voters(:voter), :vote => 'dislike')
123
+ assert votable.vote_registered?
124
+ end
125
+
126
+ def test_count_the_vote_as_registered_if_the_voter_has_voted_and_vote_weight_has_changed
127
+ votable.vote_by(:voter => voters(:voter), :vote => true, :vote_weight => 1)
128
+ votable.vote_by(:voter => voters(:voter), :vote => true, :vote_weight => 2)
129
+ assert votable.vote_registered?
130
+ end
131
+
132
+ def test_voted_on_by_voter
133
+ votable.vote_by(:voter => voters(:voter))
134
+ assert votable.voted_on_by?(voters(:voter))
135
+ end
136
+
137
+ def test_unvoted
138
+ votable.liked_by(voters(:voter))
139
+ votable.unliked_by(voters(:voter))
140
+ assert !votable.voted_on_by?(voters(:voter))
141
+ end
142
+
143
+ def test_unvoted_positive_vote
144
+ votable.vote_by(:voter => voters(:voter))
145
+ votable.unvote(:voter => voters(:voter))
146
+ assert_equal votable.find_votes_for.count, 0
147
+ end
148
+
149
+ def test_set_the_votable_to_unregistered_after_unvoting
150
+ votable.vote_by(:voter => voters(:voter))
151
+ votable.unvote(:voter => voters(:voter))
152
+ assert !votable.vote_registered?
153
+ end
154
+
155
+ def test_unvote_a_negative_vote
156
+ votable.vote_by(:voter => voters(:voter), :vote => "no")
157
+ votable.unvote(:voter => voters(:voter))
158
+ assert_equal votable.find_votes_for.count, 0
159
+ end
160
+
161
+ def test_unvote_only_the_from_a_single_voter
162
+ votable.vote_by(:voter => voters(:voter))
163
+ votable.vote_by(:voter => voters(:voter2))
164
+ votable.unvote(:voter => voters(:voter))
165
+ assert_equal votable.find_votes_for.count, 1
166
+ end
167
+
168
+ def test_contained_to_instance
169
+ votable2 = Votable.new(:name => "2nd votable")
170
+ votable2.save
171
+
172
+ votable.vote_by(:voter => voters(:voter), :vote => false)
173
+ votable2.vote_by(:voter => voters(:voter), :vote => true)
174
+ votable2.vote_by(:voter => voters(:voter), :vote => true)
175
+
176
+ assert votable.vote_registered?
177
+ assert !votable2.vote_registered?
178
+ end
179
+
180
+ def test_default_weight_if_not_specified
181
+ votable.upvote_by(voters(:voter))
182
+ assert_equal votable.find_votes_for.first.vote_weight, 1
183
+ end
184
+
185
+ # with cached votes_for
186
+
187
+ def voter
188
+ voters(:voter)
189
+ end
190
+
191
+ def votable_cache
192
+ votable_caches(:votable_cache)
193
+ end
194
+
195
+ def test_not_update_cached_votes_for_if_there_are_no_colums
196
+ votable.vote_by(:voter => voter)
197
+ end
198
+
199
+ def test_update_chaced_votes_for_if_there_is_a_total_column
200
+ votable_cache.cached_votes_total = 50
201
+ votable_cache.vote_by(voter: voter)
202
+ assert_equal votable_cache.cached_votes_total, 1
203
+ end
204
+
205
+ def test_update_cached_total_votes_for_when_a_vote_up_is_removed
206
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
207
+ votable_cache.unvote(:voter => voter)
208
+ assert_equal votable_cache.cached_votes_total, 0
209
+ end
210
+
211
+ def test_update_cachded_score_votes_for_if_there_is_a_score_column
212
+ votable_cache.cached_votes_score = 50
213
+ votable_cache.vote_by(:voter => voter)
214
+ assert_equal votable_cache.cached_votes_score, 1
215
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false')
216
+ assert_equal votable_cache.cached_votes_score, 0
217
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
218
+ assert_equal votable_cache.cached_votes_score, -2
219
+ end
220
+
221
+ def test_update_cached_score_votef_for_when_a_vote_up_is_removed
222
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
223
+ assert_equal votable_cache.cached_votes_score, 1
224
+ votable_cache.unvote(:voter => voter)
225
+ assert_equal votable_cache.cached_votes_score, 0
226
+ end
227
+
228
+ def test_update_cached_score_votef_for_when_a_vote_down_is_removed
229
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
230
+ assert_equal votable_cache.cached_votes_score, -1
231
+ votable_cache.unvote(:voter => voter)
232
+ assert_equal votable_cache.cached_votes_score, 0
233
+ end
234
+
235
+ def test_updata_cached_weighted_total_if_there_is_a_weighted_total_column
236
+ votable_cache.cached_weighted_total = 50
237
+ votable_cache.vote_by(:voter => voter)
238
+ assert_equal votable_cache.cached_weighted_total, 1
239
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false')
240
+ assert_equal votable_cache.cached_weighted_total, 2
241
+ end
242
+
243
+ def test_update_cached_weighted_total_votes_for_when_a_vote_up_is_removed
244
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 3)
245
+ assert_equal votable_cache.cached_weighted_total, 3
246
+ votable_cache.unvote(:voter => voter)
247
+ assert_equal votable_cache.cached_weighted_total, 0
248
+ end
249
+
250
+ def test_update_cached_weighted_total_votes_for_when_a_vote_down_is_removed
251
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_weight => 4)
252
+ assert_equal votable_cache.cached_weighted_total, 4
253
+ votable_cache.unvote(:voter => voter)
254
+ assert_equal votable_cache.cached_weighted_total, 0
255
+ end
256
+
257
+ def test_update_cached_weighted_score_if_there_is_a_weighted_score_column
258
+ votable_cache.cached_weighted_score = 50
259
+ votable_cache.vote_by(:voter => voter, :vote_weight => 3)
260
+ assert_equal votable_cache.cached_weighted_score, 3
261
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false', :vote_weight => 5)
262
+ assert_equal votable_cache.cached_weighted_score, -2
263
+ # voter changes her vote from 3 to 5
264
+ votable_cache.vote_by(:voter => voter, :vote_weight => 5)
265
+ assert_equal votable_cache.cached_weighted_score, 0
266
+ votable_cache.vote_by :voter => voters(:voter3), :vote_weight => 4
267
+ assert_equal votable_cache.cached_weighted_score, 4
268
+ end
269
+
270
+ def test_update_cached_weighted_score_votes_for_when_a_vote_up_is_removed
271
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 3)
272
+ assert_equal votable_cache.cached_weighted_score, 3
273
+ votable_cache.unvote :voter => voter
274
+ assert_equal votable_cache.cached_weighted_score, 0
275
+ end
276
+
277
+ def test_update_cached_weighted_score_votes_for_when_a_vote_down_is_removed
278
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_weight => 4)
279
+ assert_equal votable_cache.cached_weighted_score, -4
280
+ votable_cache.unvote :voter => voter
281
+ assert_equal votable_cache.cached_weighted_score, 0
282
+ end
283
+
284
+ def test_update_cached_weighted_average_if_there_is_a_weighted_average_column
285
+ votable_cache.cached_weighted_average = 50.0
286
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 5)
287
+ assert_equal votable_cache.cached_weighted_average, 5.0
288
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'true', :vote_weight => 3)
289
+ assert_equal votable_cache.cached_weighted_average, 4.0
290
+ # voter changes her vote from 5 to 4
291
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 4)
292
+ assert_equal votable_cache.cached_weighted_average, 3.5
293
+ votable_cache.vote_by(:voter => voters(:voter3), :vote => 'true', :vote_weight => 5)
294
+ assert_equal votable_cache.cached_weighted_average, 4.0
295
+ end
296
+
297
+ def test_update_cached_weighted_average_votes_for_when_a_vote_up_is_removed
298
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 5)
299
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'true', :vote_weight => 3)
300
+ assert_equal votable_cache.cached_weighted_average, 4
301
+ votable_cache.unvote :voter => voter
302
+ assert_equal votable_cache.cached_weighted_average, 3
303
+ end
304
+
305
+ def test_update_cached_up_votes_for_if_there_is_an_up_vote_column
306
+ votable_cache.cached_votes_up = 50
307
+ votable_cache.vote_by(:voter => voter)
308
+ votable_cache.vote_by(:voter => voter)
309
+ assert_equal votable_cache.cached_votes_up, 1
310
+ end
311
+
312
+ def test_update_cached_down_votes_for_if_there_is_a_down_vote_column
313
+ votable_cache.cached_votes_down = 50
314
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
315
+ assert_equal votable_cache.cached_votes_down, 1
316
+ end
317
+
318
+ def test_update_cached_up_votes_for_when_a_vote_up_is_removed
319
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
320
+ votable_cache.unvote(:voter => voter)
321
+ assert_equal votable_cache.cached_votes_up, 0
322
+ end
323
+
324
+ def test_update_cached_down_votes_for_when_a_vote_down_is_removed
325
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
326
+ votable_cache.unvote(:voter => voter)
327
+ assert_equal votable_cache.cached_votes_down, 0
328
+ end
329
+
330
+ def test_select_from_cached_total_votes_for_if_there_a_total_column
331
+ votable_cache.vote_by(:voter => voter)
332
+ votable_cache.cached_votes_total = 50
333
+ assert_equal votable_cache.count_votes_total, 50
334
+ end
335
+
336
+ def test_select_from_cached_up_votes_for_if_there_is_an_up_vote_column
337
+ votable_cache.vote_by(:voter => voter)
338
+ votable_cache.cached_votes_up = 50
339
+ assert_equal votable_cache.count_votes_up, 50
340
+ end
341
+
342
+ def test_select_from_cached_down_votes_for_if_there_is_a_down_vote_column
343
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
344
+ votable_cache.cached_votes_down = 50
345
+ assert_equal votable_cache.count_votes_down, 50
346
+ end
347
+
348
+ def test_select_from_cached_weighted_total_if_there_is_a_weighted_total_column
349
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
350
+ votable_cache.cached_weighted_total = 50
351
+ assert_equal votable_cache.weighted_total, 50
352
+ end
353
+
354
+ def test_select_from_cached_weighted_score_if_there_is_a_weighted_score_column
355
+ votable_cache.vote_by( :voter => voter, :vote => 'false')
356
+ votable_cache.cached_weighted_score = 50
357
+ assert_equal votable_cache.weighted_score, 50
358
+ end
359
+
360
+ def test_select_from_cached_weighted_average_if_there_is_a_weighted_average_column
361
+ votable_cache.vote_by( :voter => voter, :vote => 'false')
362
+ votable_cache.cached_weighted_average = 50
363
+ assert_equal votable_cache.weighted_average, 50
364
+ end
365
+
366
+ def test_update_cached_total_votes_for_when_voting_under_an_scope
367
+ votable_cache.vote_by( :voter => voter, :vote => 'true', :vote_scope => 'rank')
368
+ assert_equal votable_cache.cached_votes_total, 1
369
+ end
370
+
371
+ def test_update_cached_up_votes_for_when_voting_under_an_scope
372
+ votable_cache.vote_by( :voter => voter, :vote => 'true', :vote_scope => 'rank')
373
+ assert_equal votable_cache.cached_votes_up, 1
374
+ end
375
+
376
+ def test_update_cached_total_votes_for_when_a_scoped_vote_down_is_removed
377
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => 'rank')
378
+ votable_cache.unvote( :voter => voter, :vote_scope => 'rank')
379
+ assert_equal votable_cache.cached_votes_total, 0
380
+ end
381
+
382
+ def test_update_cached_up_votes_for_when_a_scoped_vote_down_is_removed
383
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => 'rank')
384
+ votable_cache.unvote(:voter => voter, :vote_scope => 'rank')
385
+ assert_equal votable_cache.cached_votes_up, 0
386
+ end
387
+
388
+ def test_update_cached_down_votes_for_when_downvoting_under_a_scope
389
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => 'rank')
390
+ assert_equal votable_cache.cached_votes_down, 1
391
+ end
392
+
393
+ def test_update_cached_down_votes_for_when_a_scoped_vote_down_is_removed
394
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => 'rank')
395
+ votable_cache.unvote(:voter => voter, :vote_scope => 'rank')
396
+ assert_equal votable_cache.cached_votes_down, 0
397
+ end
398
+
399
+ # describe "with scoped cached votes_for
400
+
401
+ def test_update_cached_total_votes_for_if_there_is_a_total_column
402
+ votable_cache.cached_scoped_test_votes_total = 50
403
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
404
+ assert_equal votable_cache.cached_scoped_test_votes_total, 1
405
+ end
406
+
407
+ def test_update_cached_total_votes_for_when_a_vote_up_is_removed
408
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => "test")
409
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
410
+ assert_equal votable_cache.cached_scoped_test_votes_total, 0
411
+ end
412
+
413
+ def test_update_cached_total_votes_for_when_a_vote_down_is_removed
414
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
415
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
416
+ assert_equal votable_cache.cached_scoped_test_votes_total, 0
417
+ end
418
+
419
+ def test_update_cached_score_votes_for_if_there_is_a_score_column
420
+ votable_cache.cached_scoped_test_votes_score = 50
421
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
422
+ assert_equal votable_cache.cached_scoped_test_votes_score, 1
423
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false', :vote_scope => "test")
424
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
425
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
426
+ assert_equal votable_cache.cached_scoped_test_votes_score, -2
427
+ end
428
+
429
+ def test_update_cached_score_votes_for_when_a_vote_up_is_removed
430
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => "test")
431
+ assert_equal votable_cache.cached_scoped_test_votes_score, 1
432
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
433
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
434
+ end
435
+
436
+ def test_update_cached_score_votes_for_when_a_vote_down_is_removed
437
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
438
+ assert_equal votable_cache.cached_scoped_test_votes_score, -1
439
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
440
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
441
+ end
442
+
443
+ def test_update_cached_up_votes_for_if_there_is_an_up_vote_column
444
+ votable_cache.cached_scoped_test_votes_up = 50
445
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
446
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
447
+ assert_equal votable_cache.cached_scoped_test_votes_up, 1
448
+ end
449
+
450
+ def test_update_cached_down_votes_for_if_there_is_a_down_vote_column
451
+ votable_cache.cached_scoped_test_votes_down = 50
452
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
453
+ assert_equal votable_cache.cached_scoped_test_votes_down, 1
454
+ end
455
+
456
+ def test_update_cached_up_votes_for_when_a_vote_up_is_removed
457
+ votable_cache.vote_by :voter => voter, :vote => 'true', :vote_scope => "test"
458
+ votable_cache.unvote :voter => voter, :vote_scope => "test"
459
+ assert_equal votable_cache.cached_scoped_test_votes_up, 0
460
+ end
461
+
462
+ def test_update_cached_down_votes_for_when_a_vote_down_is_removed
463
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
464
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
465
+ assert_equal votable_cache.cached_scoped_test_votes_down, 0
466
+ end
467
+
468
+ def test_select_from_cached_total_votes_for_if_there_a_total_column
469
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
470
+ votable_cache.cached_scoped_test_votes_total = 50
471
+ assert_equal votable_cache.count_votes_total(false, "test"), 50
472
+ end
473
+
474
+ def test_select_from_cached_up_votes_for_if_there_is_an_up_vote_column
475
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
476
+ votable_cache.cached_scoped_test_votes_up = 50
477
+ assert_equal votable_cache.count_votes_up(false, "test"), 50
478
+ end
479
+
480
+ def test_select_from_cached_down_votes_for_if_there_is_a_down_vote_column
481
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
482
+ votable_cache.cached_scoped_test_votes_down = 50
483
+ assert_equal votable_cache.count_votes_down(false, "test"), 50
484
+ end
485
+
486
+ # describe "sti models
487
+
488
+ def test_be_able_to_vote_on_a_votable_child_of_a_non_votable_sti_model
489
+ votable = VotableChildOfStiNotVotable.create(:name => 'sti child')
490
+
491
+ votable.vote_by(:voter => voter, :vote => 'yes')
492
+ assert_equal votable.votes_for.size, 1
493
+ end
494
+
495
+ def test_not_be_able_to_vote_on_a_parent_non_votable
496
+ assert !StiNotVotable.votable?
497
+ end
498
+
499
+ def test_be_able_to_vote_on_a_child_when_its_parent_is_votable
500
+ votable = ChildOfStiVotable.create(:name => 'sti child')
501
+
502
+ votable.vote_by(:voter => voter, :vote => 'yes')
503
+ assert_equal votable.votes_for.size, 1
504
+ end
505
+
506
+
507
+ end