redmine_crm 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/redmine_crm.gemspec CHANGED
@@ -19,3 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  end
22
+
@@ -1,8 +1,13 @@
1
1
  class Issue < ActiveRecord::Base
2
2
  rcrm_acts_as_taggable
3
+ rcrm_acts_as_viewed
3
4
  # default_scope {where(:closed => false)}
4
5
  belongs_to :user
5
6
  validates_presence_of :description
7
+
8
+ def save_without_validation
9
+ save
10
+ end
6
11
  end
7
12
 
8
13
  class SpecialIssue < Issue
@@ -1,3 +1,7 @@
1
1
  class User < ActiveRecord::Base
2
2
  has_many :issues
3
+
4
+ def anonymous?
5
+ false
6
+ end
3
7
  end
@@ -0,0 +1,2 @@
1
+ votable_cache:
2
+ name: 'votiong model with cache'
@@ -0,0 +1,4 @@
1
+ votable:
2
+ name: 'a voting model'
3
+ votable2:
4
+ name: 'another voting things'
@@ -0,0 +1,54 @@
1
+ class Voter < ActiveRecord::Base
2
+ rcrm_acts_as_voter
3
+ end
4
+
5
+ class NotVoter < ActiveRecord::Base
6
+
7
+ end
8
+
9
+ class Votable < ActiveRecord::Base
10
+ rcrm_acts_as_votable
11
+ validates_presence_of :name
12
+ end
13
+
14
+ class VotableVoter < ActiveRecord::Base
15
+ rcrm_acts_as_votable
16
+ rcrm_acts_as_voter
17
+ end
18
+
19
+ class StiVotable < ActiveRecord::Base
20
+ rcrm_acts_as_votable
21
+ end
22
+
23
+ class ChildOfStiVotable < StiVotable
24
+ end
25
+
26
+ class StiNotVotable < ActiveRecord::Base
27
+ validates_presence_of :name
28
+ end
29
+
30
+ class VotableChildOfStiNotVotable < StiNotVotable
31
+ rcrm_acts_as_votable
32
+ end
33
+
34
+ class NotVotable < ActiveRecord::Base
35
+ end
36
+
37
+ class VotableCache < ActiveRecord::Base
38
+ rcrm_acts_as_votable
39
+ validates_presence_of :name
40
+ end
41
+
42
+ class ABoringClass
43
+ def self.hw
44
+ 'hello world'
45
+ end
46
+ end
47
+
48
+
49
+ # def clean_database
50
+ # models = [ActsAsVotable::Vote, Voter, NotVoter, Votable, NotVotable, VotableCache]
51
+ # models.each do |model|
52
+ # ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
53
+ # end
54
+ # end
@@ -0,0 +1,6 @@
1
+ voter:
2
+ name: 'i can vote!'
3
+ voter2:
4
+ name: 'a new person'
5
+ voter3:
6
+ name: 'another person'
data/test/schema.rb CHANGED
@@ -21,5 +21,79 @@ ActiveRecord::Schema.define :version => 0 do
21
21
  t.column "closed", :boolean
22
22
  t.column "cached_tag_list", :string
23
23
  t.column "user_id", :integer
24
+ t.column "views", :integer, :default => 0
25
+ end
26
+
27
+ create_table "votes", :force => true do |t|
28
+ t.references "votable", :polymorphic => true
29
+ t.references "voter", :polymorphic => true
30
+
31
+ t.boolean "vote_flag"
32
+ t.string "vote_scope"
33
+ t.integer "vote_weight"
34
+
35
+ t.timestamps
36
+ end
37
+
38
+ # add_index :votes, [:votable_id, :votable_type]
39
+ # add_index :votes, [:voter_id, :voter_type]
40
+ # add_index :votes, [:voter_id, :voter_type, :vote_scope]
41
+ # add_index :votes, [:votable_id, :votable_type, :vote_scope]
42
+
43
+ create_table :voters, :force => true do |t|
44
+ t.string :name
45
+ end
46
+
47
+ create_table :not_voters, :force => true do |t|
48
+ t.string :name
49
+ end
50
+
51
+ create_table :votables, :force => true do |t|
52
+ t.string :name
53
+ end
54
+
55
+ create_table :votable_voters, :force => true do |t|
56
+ t.string :name
57
+ end
58
+
59
+ create_table :sti_votables, :force => true do |t|
60
+ t.string :name
61
+ t.string :type
62
+ end
63
+
64
+ create_table :sti_not_votables, :force => true do |t|
65
+ t.string :name
66
+ t.string :type
67
+ end
68
+
69
+ create_table :not_votables, :force => true do |t|
70
+ t.string :name
71
+ end
72
+
73
+ create_table :votable_caches, :force => true do |t|
74
+ t.string :name
75
+ t.integer :cached_votes_total
76
+ t.integer :cached_votes_score
77
+ t.integer :cached_votes_up
78
+ t.integer :cached_votes_down
79
+ t.integer :cached_weighted_total
80
+ t.integer :cached_weighted_score
81
+ t.float :cached_weighted_average
82
+
83
+ t.integer :cached_scoped_test_votes_total
84
+ t.integer :cached_scoped_test_votes_score
85
+ t.integer :cached_scoped_test_votes_up
86
+ t.integer :cached_scoped_test_votes_down
87
+ t.integer :cached_scoped_weighted_total
88
+ t.integer :cached_scoped_weighted_score
89
+ t.float :cached_scoped_weighted_average
90
+ end
91
+
92
+ create_table :viewings, :force => true do |t|
93
+ t.column :viewer_id, :integer
94
+ t.column :viewed_id, :integer
95
+ t.column :viewed_type, :string
96
+ t.column :ip, :string, :limit => '24'
97
+ t.column :created_at, :datetime
24
98
  end
25
99
  end
data/test/test_helper.rb CHANGED
@@ -18,7 +18,9 @@ require File.dirname(__FILE__) + '/../lib/redmine_crm/rcrm_acts_as_taggable'
18
18
  require File.dirname(__FILE__) + '/../lib/redmine_crm/tag_list'
19
19
  require File.dirname(__FILE__) + '/../lib/redmine_crm/tag'
20
20
  require File.dirname(__FILE__) + '/../lib/redmine_crm/tagging'
21
- require File.dirname(__FILE__) + '/../lib/redmine_crm/tags_helper'
21
+ require File.dirname(__FILE__) + '/../lib/redmine_crm/helpers/tags_helper'
22
+ require File.dirname(__FILE__) + '/../lib/redmine_crm/rcrm_acts_as_votable'
23
+ require File.dirname(__FILE__) + '/../lib/redmine_crm/rcrm_acts_as_voter'
22
24
 
23
25
  # require_dependency File.dirname(__FILE__) + '/../lib/redmine_crm/tags_helper'
24
26
 
@@ -27,8 +29,11 @@ ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) +
27
29
  ActiveRecord::Base.establish_connection(ENV['DB'] || 'sqlite3')
28
30
 
29
31
  load(File.dirname(__FILE__) + '/schema.rb')
30
- load(File.dirname(__FILE__) + '/fixtures/issue.rb')
31
32
  load(File.dirname(__FILE__) + '/fixtures/user.rb')
33
+ load(File.dirname(__FILE__) + '/fixtures/issue.rb')
34
+ load(File.dirname(__FILE__) + "/fixtures/vote_classes.rb")
35
+
36
+ # Dir["/fixtures/*.rb"].each{|file| load file}
32
37
 
33
38
  class ActiveSupport::TestCase #:nodoc:
34
39
  include ActiveRecord::TestFixtures
@@ -40,6 +45,7 @@ class ActiveSupport::TestCase #:nodoc:
40
45
  set_fixture_class :tags => RedmineCrm::Tag
41
46
  set_fixture_class :taggings => RedmineCrm::Tagging
42
47
 
48
+ set_fixture_class :votable_caches => VotableCache
43
49
  fixtures :all
44
50
 
45
51
  def assert_equivalent(expected, actual, message = nil)
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ViewedTest < ActiveSupport::TestCase
4
+
5
+ def user
6
+ user = users(:jonathan)
7
+ end
8
+
9
+ def issue
10
+ issue = issues(:first_issue)
11
+ end
12
+
13
+
14
+ def test_zero_of_view_count
15
+ assert_equal issue.view_count, 0
16
+ end
17
+
18
+ def test_can_view
19
+ issue.view '127.0.0.1', user
20
+ assert_equal issue.view_count, 1
21
+ end
22
+
23
+ def test_viewed_by
24
+ assert !issue.viewed_by?('127.0.0.1', user)
25
+ issue.view '127.0.0.1', user
26
+ assert issue.viewed_by?('127.0.0.1', user)
27
+ end
28
+
29
+ def test_twice_view
30
+ issue.view '127.0.0.1', user
31
+ issue.view '127.0.0.1', user
32
+ assert_equal issue.view_count, 1
33
+ end
34
+
35
+ def test_viewed?
36
+ assert !issue.viewed?
37
+ issue.view '127.0.0.1', user
38
+ assert issue.viewed?
39
+ end
40
+
41
+
42
+ end
@@ -0,0 +1,478 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class VotableModelTest < ActiveSupport::TestCase
4
+
5
+ def votable
6
+ votables(:votable)
7
+ end
8
+
9
+ def test_vote_without_voter
10
+ assert !votables(:votable).vote_by
11
+ end
12
+
13
+ def test_have_vote_when_saved
14
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
15
+ assert_equal votable.votes_for.size, 1
16
+ end
17
+
18
+ def test_voted_twice_by_same_person
19
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
20
+ votable.vote_by(:voter => voters(:voter), :vote => "no")
21
+ assert_equal votable.votes_for.size, 1
22
+ end
23
+
24
+ def test_voted_twice_by_same_person_with_duplicate_params
25
+ votable.vote_by(:voter => voters(:voter), :vote => "yes")
26
+ votable.vote_by(:voter => voters(:voter), :vote => "no", :duplicate => true)
27
+ assert_equal votable.votes_for.size, 2
28
+ end
29
+
30
+ def test_one_scoped_vote
31
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
32
+ assert_equal votable.find_votes_for(:vote_scope => 'rank').size, 1
33
+ end
34
+
35
+ def test_one_scoped_vote_when_using_scope_by_same_person
36
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
37
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'rank')
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')
43
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes', :vote_scope => 'monthly_rank')
44
+ assert_equal votable.votes_for.size, 2
45
+ end
46
+
47
+ def test_called_with_vote_up
48
+ votables(:votable).vote_up voters(:voter)
49
+ assert_equal votables(:votable).get_up_votes.first.voter, voters(:voter)
50
+ end
51
+
52
+ def test_called_with_vote_down
53
+ votables(:votable).vote_down voters(:voter)
54
+ assert_equal votables(:votable).get_down_votes.first.voter, voters(:voter)
55
+ end
56
+
57
+ def test_have_two_votes_when_voted_two_different_people
58
+ votable.vote_by(:voter => voters(:voter))
59
+ votable.vote_by(:voter => voters(:voter2))
60
+ assert_equal votable.votes_for.size, 2
61
+ end
62
+
63
+ def test_one_true_vote
64
+ votable.vote_by(:voter => voters(:voter))
65
+ votable.vote_by(:voter => voters(:voter2), :vote => "dislike")
66
+ assert_equal votable.get_up_votes.size, 1
67
+ end
68
+
69
+ def test_two_false_votes
70
+ votable.vote_by(:voter => voters(:voter), :vote => 'no')
71
+ votable.vote_by(:voter => voters(:voter2), :vote => "dislike")
72
+ assert_equal votable.get_down_votes.size, 2
73
+ end
74
+
75
+ def test_have_been_voted_on_by_voter2
76
+ votable.vote_by(:voter => voters(:voter2), :vote => true)
77
+ assert_equal votable.find_votes_for.first.voter.id, voters(:voter2).id
78
+ end
79
+
80
+ def test_count_the_vote_as_registered_if_this_the_voters_first_vote
81
+ votable.vote_by(:voter => voters(:voter))
82
+ assert votable.vote_registered?
83
+ end
84
+
85
+ def test_not_count_the_vote_as_being_registered_if_that_voter_has_alredy_voted_and_voted_has_not_chanded
86
+ votable.vote_by(:voter => voters(:voter), :vote => true)
87
+ votable.vote_by(:voter => voters(:voter), :vote => 'yes')
88
+ assert !votable.vote_registered?
89
+ end
90
+
91
+ def test_count_the_vote_as_registered_if_the_voter_has_voted_and_the_flag_has_changed
92
+ votable.vote_by(:voter => voters(:voter), :vote => true)
93
+ votable.vote_by(:voter => voters(:voter), :vote => 'dislike')
94
+ assert votable.vote_registered?
95
+ end
96
+
97
+ def test_count_the_vote_as_registered_if_the_voter_has_voted_and_vote_weight_has_changed
98
+ votable.vote_by(:voter => voters(:voter), :vote => true, :vote_weight => 1)
99
+ votable.vote_by(:voter => voters(:voter), :vote => true, :vote_weight => 2)
100
+ assert votable.vote_registered?
101
+ end
102
+
103
+ def test_voted_on_by_voter
104
+ votable.vote_by(:voter => voters(:voter))
105
+ assert votable.voted_on_by?(voters(:voter))
106
+ end
107
+
108
+ def test_unvoted
109
+ votable.liked_by(voters(:voter))
110
+ votable.unliked_by(voters(:voter))
111
+ assert !votable.voted_on_by?(voters(:voter))
112
+ end
113
+
114
+ def test_unvoted_positive_vote
115
+ votable.vote_by(:voter => voters(:voter))
116
+ votable.unvote(:voter => voters(:voter))
117
+ assert_equal votable.find_votes_for.count, 0
118
+ end
119
+
120
+ def test_set_the_votable_to_unregistered_after_unvoting
121
+ votable.vote_by(:voter => voters(:voter))
122
+ votable.unvote(:voter => voters(:voter))
123
+ assert !votable.vote_registered?
124
+ end
125
+
126
+ def test_unvote_a_negative_vote
127
+ votable.vote_by(:voter => voters(:voter), :vote => "no")
128
+ votable.unvote(:voter => voters(:voter))
129
+ assert_equal votable.find_votes_for.count, 0
130
+ end
131
+
132
+ def test_unvote_only_the_from_a_single_voter
133
+ votable.vote_by(:voter => voters(:voter))
134
+ votable.vote_by(:voter => voters(:voter2))
135
+ votable.unvote(:voter => voters(:voter))
136
+ assert_equal votable.find_votes_for.count, 1
137
+ end
138
+
139
+ def test_contained_to_instance
140
+ votable2 = Votable.new(:name => "2nd votable")
141
+ votable2.save
142
+
143
+ votable.vote_by(:voter => voters(:voter), :vote => false)
144
+ votable2.vote_by(:voter => voters(:voter), :vote => true)
145
+ votable2.vote_by(:voter => voters(:voter), :vote => true)
146
+
147
+ assert votable.vote_registered?
148
+ assert !votable2.vote_registered?
149
+ end
150
+
151
+ def test_default_weight_if_not_specified
152
+ votable.upvote_by(voters(:voter))
153
+ assert_equal votable.find_votes_for.first.vote_weight, 1
154
+ end
155
+
156
+ # with cached votes_for
157
+
158
+ def voter
159
+ voters(:voter)
160
+ end
161
+
162
+ def votable_cache
163
+ votable_caches(:votable_cache)
164
+ end
165
+
166
+ def test_not_update_cached_votes_for_if_there_are_no_colums
167
+ votable.vote_by(:voter => voter)
168
+ end
169
+
170
+ def test_update_chaced_votes_for_if_there_is_a_total_column
171
+ votable_cache.cached_votes_total = 50
172
+ votable_cache.vote_by(voter: voter)
173
+ assert_equal votable_cache.cached_votes_total, 1
174
+ end
175
+
176
+ def test_update_cached_total_votes_for_when_a_vote_up_is_removed
177
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
178
+ votable_cache.unvote(:voter => voter)
179
+ assert_equal votable_cache.cached_votes_total, 0
180
+ end
181
+
182
+ def test_update_cachded_score_votes_for_if_there_is_a_score_column
183
+ votable_cache.cached_votes_score = 50
184
+ votable_cache.vote_by(:voter => voter)
185
+ assert_equal votable_cache.cached_votes_score, 1
186
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false')
187
+ assert_equal votable_cache.cached_votes_score, 0
188
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
189
+ assert_equal votable_cache.cached_votes_score, -2
190
+ end
191
+
192
+ def test_update_cached_score_votef_for_when_a_vote_up_is_removed
193
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
194
+ assert_equal votable_cache.cached_votes_score, 1
195
+ votable_cache.unvote(:voter => voter)
196
+ assert_equal votable_cache.cached_votes_score, 0
197
+ end
198
+
199
+ def test_update_cached_score_votef_for_when_a_vote_down_is_removed
200
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
201
+ assert_equal votable_cache.cached_votes_score, -1
202
+ votable_cache.unvote(:voter => voter)
203
+ assert_equal votable_cache.cached_votes_score, 0
204
+ end
205
+
206
+ def test_updata_cached_weighted_total_if_there_is_a_weighted_total_column
207
+ votable_cache.cached_weighted_total = 50
208
+ votable_cache.vote_by(:voter => voter)
209
+ assert_equal votable_cache.cached_weighted_total, 1
210
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false')
211
+ assert_equal votable_cache.cached_weighted_total, 2
212
+ end
213
+
214
+ def test_update_cached_weighted_total_votes_for_when_a_vote_up_is_removed
215
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 3)
216
+ assert_equal votable_cache.cached_weighted_total, 3
217
+ votable_cache.unvote(:voter => voter)
218
+ assert_equal votable_cache.cached_weighted_total, 0
219
+ end
220
+
221
+ def test_update_cached_weighted_total_votes_for_when_a_vote_down_is_removed
222
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_weight => 4)
223
+ assert_equal votable_cache.cached_weighted_total, 4
224
+ votable_cache.unvote(:voter => voter)
225
+ assert_equal votable_cache.cached_weighted_total, 0
226
+ end
227
+
228
+ def test_update_cached_weighted_score_if_there_is_a_weighted_score_column
229
+ votable_cache.cached_weighted_score = 50
230
+ votable_cache.vote_by(:voter => voter, :vote_weight => 3)
231
+ assert_equal votable_cache.cached_weighted_score, 3
232
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false', :vote_weight => 5)
233
+ assert_equal votable_cache.cached_weighted_score, -2
234
+ # voter changes her vote from 3 to 5
235
+ votable_cache.vote_by(:voter => voter, :vote_weight => 5)
236
+ assert_equal votable_cache.cached_weighted_score, 0
237
+ votable_cache.vote_by :voter => voters(:voter3), :vote_weight => 4
238
+ assert_equal votable_cache.cached_weighted_score, 4
239
+ end
240
+
241
+ def test_update_cached_weighted_score_votes_for_when_a_vote_up_is_removed
242
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 3)
243
+ assert_equal votable_cache.cached_weighted_score, 3
244
+ votable_cache.unvote :voter => voter
245
+ assert_equal votable_cache.cached_weighted_score, 0
246
+ end
247
+
248
+ def test_update_cached_weighted_score_votes_for_when_a_vote_down_is_removed
249
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_weight => 4)
250
+ assert_equal votable_cache.cached_weighted_score, -4
251
+ votable_cache.unvote :voter => voter
252
+ assert_equal votable_cache.cached_weighted_score, 0
253
+ end
254
+
255
+ def test_update_cached_weighted_average_if_there_is_a_weighted_average_column
256
+ votable_cache.cached_weighted_average = 50.0
257
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 5)
258
+ assert_equal votable_cache.cached_weighted_average, 5.0
259
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'true', :vote_weight => 3)
260
+ assert_equal votable_cache.cached_weighted_average, 4.0
261
+ # voter changes her vote from 5 to 4
262
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 4)
263
+ assert_equal votable_cache.cached_weighted_average, 3.5
264
+ votable_cache.vote_by(:voter => voters(:voter3), :vote => 'true', :vote_weight => 5)
265
+ assert_equal votable_cache.cached_weighted_average, 4.0
266
+ end
267
+
268
+ def test_update_cached_weighted_average_votes_for_when_a_vote_up_is_removed
269
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_weight => 5)
270
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'true', :vote_weight => 3)
271
+ assert_equal votable_cache.cached_weighted_average, 4
272
+ votable_cache.unvote :voter => voter
273
+ assert_equal votable_cache.cached_weighted_average, 3
274
+ end
275
+
276
+ def test_update_cached_up_votes_for_if_there_is_an_up_vote_column
277
+ votable_cache.cached_votes_up = 50
278
+ votable_cache.vote_by(:voter => voter)
279
+ votable_cache.vote_by(:voter => voter)
280
+ assert_equal votable_cache.cached_votes_up, 1
281
+ end
282
+
283
+ def test_update_cached_down_votes_for_if_there_is_a_down_vote_column
284
+ votable_cache.cached_votes_down = 50
285
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
286
+ assert_equal votable_cache.cached_votes_down, 1
287
+ end
288
+
289
+ def test_update_cached_up_votes_for_when_a_vote_up_is_removed
290
+ votable_cache.vote_by(:voter => voter, :vote => 'true')
291
+ votable_cache.unvote(:voter => voter)
292
+ assert_equal votable_cache.cached_votes_up, 0
293
+ end
294
+
295
+ def test_update_cached_down_votes_for_when_a_vote_down_is_removed
296
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
297
+ votable_cache.unvote(:voter => voter)
298
+ assert_equal votable_cache.cached_votes_down, 0
299
+ end
300
+
301
+ def test_select_from_cached_total_votes_for_if_there_a_total_column
302
+ votable_cache.vote_by(:voter => voter)
303
+ votable_cache.cached_votes_total = 50
304
+ assert_equal votable_cache.count_votes_total, 50
305
+ end
306
+
307
+ def test_select_from_cached_up_votes_for_if_there_is_an_up_vote_column
308
+ votable_cache.vote_by(:voter => voter)
309
+ votable_cache.cached_votes_up = 50
310
+ assert_equal votable_cache.count_votes_up, 50
311
+ end
312
+
313
+ def test_select_from_cached_down_votes_for_if_there_is_a_down_vote_column
314
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
315
+ votable_cache.cached_votes_down = 50
316
+ assert_equal votable_cache.count_votes_down, 50
317
+ end
318
+
319
+ def test_select_from_cached_weighted_total_if_there_is_a_weighted_total_column
320
+ votable_cache.vote_by(:voter => voter, :vote => 'false')
321
+ votable_cache.cached_weighted_total = 50
322
+ assert_equal votable_cache.weighted_total, 50
323
+ end
324
+
325
+ def test_select_from_cached_weighted_score_if_there_is_a_weighted_score_column
326
+ votable_cache.vote_by( :voter => voter, :vote => 'false')
327
+ votable_cache.cached_weighted_score = 50
328
+ assert_equal votable_cache.weighted_score, 50
329
+ end
330
+
331
+ def test_select_from_cached_weighted_average_if_there_is_a_weighted_average_column
332
+ votable_cache.vote_by( :voter => voter, :vote => 'false')
333
+ votable_cache.cached_weighted_average = 50
334
+ assert_equal votable_cache.weighted_average, 50
335
+ end
336
+
337
+ def test_update_cached_total_votes_for_when_voting_under_an_scope
338
+ votable_cache.vote_by( :voter => voter, :vote => 'true', :vote_scope => 'rank')
339
+ assert_equal votable_cache.cached_votes_total, 1
340
+ end
341
+
342
+ def test_update_cached_up_votes_for_when_voting_under_an_scope
343
+ votable_cache.vote_by( :voter => voter, :vote => 'true', :vote_scope => 'rank')
344
+ assert_equal votable_cache.cached_votes_up, 1
345
+ end
346
+
347
+ def test_update_cached_total_votes_for_when_a_scoped_vote_down_is_removed
348
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => 'rank')
349
+ votable_cache.unvote( :voter => voter, :vote_scope => 'rank')
350
+ assert_equal votable_cache.cached_votes_total, 0
351
+ end
352
+
353
+ def test_update_cached_up_votes_for_when_a_scoped_vote_down_is_removed
354
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => 'rank')
355
+ votable_cache.unvote(:voter => voter, :vote_scope => 'rank')
356
+ assert_equal votable_cache.cached_votes_up, 0
357
+ end
358
+
359
+ def test_update_cached_down_votes_for_when_downvoting_under_a_scope
360
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => 'rank')
361
+ assert_equal votable_cache.cached_votes_down, 1
362
+ end
363
+
364
+ def test_update_cached_down_votes_for_when_a_scoped_vote_down_is_removed
365
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => 'rank')
366
+ votable_cache.unvote(:voter => voter, :vote_scope => 'rank')
367
+ assert_equal votable_cache.cached_votes_down, 0
368
+ end
369
+
370
+ # describe "with scoped cached votes_for
371
+
372
+ def test_update_cached_total_votes_for_if_there_is_a_total_column
373
+ votable_cache.cached_scoped_test_votes_total = 50
374
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
375
+ assert_equal votable_cache.cached_scoped_test_votes_total, 1
376
+ end
377
+
378
+ def test_update_cached_total_votes_for_when_a_vote_up_is_removed
379
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => "test")
380
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
381
+ assert_equal votable_cache.cached_scoped_test_votes_total, 0
382
+ end
383
+
384
+ def test_update_cached_total_votes_for_when_a_vote_down_is_removed
385
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
386
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
387
+ assert_equal votable_cache.cached_scoped_test_votes_total, 0
388
+ end
389
+
390
+ def test_update_cached_score_votes_for_if_there_is_a_score_column
391
+ votable_cache.cached_scoped_test_votes_score = 50
392
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
393
+ assert_equal votable_cache.cached_scoped_test_votes_score, 1
394
+ votable_cache.vote_by(:voter => voters(:voter2), :vote => 'false', :vote_scope => "test")
395
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
396
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
397
+ assert_equal votable_cache.cached_scoped_test_votes_score, -2
398
+ end
399
+
400
+ def test_update_cached_score_votes_for_when_a_vote_up_is_removed
401
+ votable_cache.vote_by(:voter => voter, :vote => 'true', :vote_scope => "test")
402
+ assert_equal votable_cache.cached_scoped_test_votes_score, 1
403
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
404
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
405
+ end
406
+
407
+ def test_update_cached_score_votes_for_when_a_vote_down_is_removed
408
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
409
+ assert_equal votable_cache.cached_scoped_test_votes_score, -1
410
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
411
+ assert_equal votable_cache.cached_scoped_test_votes_score, 0
412
+ end
413
+
414
+ def test_update_cached_up_votes_for_if_there_is_an_up_vote_column
415
+ votable_cache.cached_scoped_test_votes_up = 50
416
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
417
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
418
+ assert_equal votable_cache.cached_scoped_test_votes_up, 1
419
+ end
420
+
421
+ def test_update_cached_down_votes_for_if_there_is_a_down_vote_column
422
+ votable_cache.cached_scoped_test_votes_down = 50
423
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
424
+ assert_equal votable_cache.cached_scoped_test_votes_down, 1
425
+ end
426
+
427
+ def test_update_cached_up_votes_for_when_a_vote_up_is_removed
428
+ votable_cache.vote_by :voter => voter, :vote => 'true', :vote_scope => "test"
429
+ votable_cache.unvote :voter => voter, :vote_scope => "test"
430
+ assert_equal votable_cache.cached_scoped_test_votes_up, 0
431
+ end
432
+
433
+ def test_update_cached_down_votes_for_when_a_vote_down_is_removed
434
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
435
+ votable_cache.unvote(:voter => voter, :vote_scope => "test")
436
+ assert_equal votable_cache.cached_scoped_test_votes_down, 0
437
+ end
438
+
439
+ def test_select_from_cached_total_votes_for_if_there_a_total_column
440
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
441
+ votable_cache.cached_scoped_test_votes_total = 50
442
+ assert_equal votable_cache.count_votes_total(false, "test"), 50
443
+ end
444
+
445
+ def test_select_from_cached_up_votes_for_if_there_is_an_up_vote_column
446
+ votable_cache.vote_by(:voter => voter, :vote_scope => "test")
447
+ votable_cache.cached_scoped_test_votes_up = 50
448
+ assert_equal votable_cache.count_votes_up(false, "test"), 50
449
+ end
450
+
451
+ def test_select_from_cached_down_votes_for_if_there_is_a_down_vote_column
452
+ votable_cache.vote_by(:voter => voter, :vote => 'false', :vote_scope => "test")
453
+ votable_cache.cached_scoped_test_votes_down = 50
454
+ assert_equal votable_cache.count_votes_down(false, "test"), 50
455
+ end
456
+
457
+ # describe "sti models
458
+
459
+ def test_be_able_to_vote_on_a_votable_child_of_a_non_votable_sti_model
460
+ votable = VotableChildOfStiNotVotable.create(:name => 'sti child')
461
+
462
+ votable.vote_by(:voter => voter, :vote => 'yes')
463
+ assert_equal votable.votes_for.size, 1
464
+ end
465
+
466
+ def test_not_be_able_to_vote_on_a_parent_non_votable
467
+ assert !StiNotVotable.votable?
468
+ end
469
+
470
+ def test_be_able_to_vote_on_a_child_when_its_parent_is_votable
471
+ votable = ChildOfStiVotable.create(:name => 'sti child')
472
+
473
+ votable.vote_by(:voter => voter, :vote => 'yes')
474
+ assert_equal votable.votes_for.size, 1
475
+ end
476
+
477
+
478
+ end