voteable_mongoid 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/.watchr ADDED
@@ -0,0 +1,23 @@
1
+ # vim:set filetype=ruby:
2
+ def run(cmd)
3
+ puts cmd
4
+ system cmd
5
+ end
6
+
7
+ def spec(file)
8
+ if File.exists?(file)
9
+ run("rspec #{file}")
10
+ else
11
+ puts("Spec: #{file} does not exist.")
12
+ end
13
+ end
14
+
15
+ watch("spec/.*/*_spec\.rb") do |match|
16
+ puts(match[0])
17
+ spec(match[0])
18
+ end
19
+
20
+ watch("lib/(.*/.*)\.rb") do |match|
21
+ puts(match[1])
22
+ spec("spec/#{match[1]}_spec.rb")
23
+ end
@@ -1,2 +1,4 @@
1
- require 'mongoid/voteable'
2
- require 'mongoid/voter'
1
+ require 'voteable_mongoid/voteable/stats'
2
+ require 'voteable_mongoid/voteable'
3
+ require 'voteable_mongoid/voter'
4
+
@@ -0,0 +1,10 @@
1
+ namespace :db do
2
+ namespace :mongoid do
3
+ namespace :voteable do
4
+ desc 'Update up_votes_count, down_votes_count, votes_count and votes_point'
5
+ task :remake_stats => :environment do
6
+ Mongoid::Voteable::Stats.remake_stats
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module VoteableMongoid
2
- VERSION = "0.4.3"
2
+ VERSION = "0.4.4"
3
3
  end
@@ -3,17 +3,23 @@ module Mongoid
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  # How many points should be assigned for each up or down vote.
6
- # This hash should be accessed and manipulated using Voteable.vote_point method
6
+ # This hash should manipulated using Voteable.vote_point method
7
7
  VOTE_POINT = {}
8
8
 
9
9
  included do
10
- index "voteable.votes_count"
11
- index "voteable.votes_point"
12
-
13
- scope :most_voted, order_by(["voteable.votes_count", :desc])
14
- scope :best_voted, order_by(["voteable.votes_point", :desc])
10
+ include Mongoid::Voteable::Stats
15
11
 
12
+ def self.voteable_related
13
+ foreign_keys = relations.values.map{ |meta| meta.try(:foreign_key) }.compact
14
+ only(foreign_keys + %w[voteable])
15
+ end
16
+
16
17
  # Set vote point for each up (down) vote on an object of this class
18
+ #
19
+ # @param [Hash] options a hash containings:
20
+ #
21
+ # vote_point self, :up => +1, :down => -3
22
+ # vote_point Post, :up => +2, :down => -1, :update_counters => false
17
23
  def self.vote_point(klass = self, options = nil)
18
24
  VOTE_POINT[self.name] ||= {}
19
25
  VOTE_POINT[self.name][klass.name] ||= options
@@ -48,52 +54,61 @@ module Mongoid
48
54
 
49
55
  if options[:revote]
50
56
  if value == :up
51
- positive_field = :up_voter_ids
52
- negative_field = :down_voter_ids
57
+ positive_voter_ids = 'voteable.up_voter_ids'
58
+ negative_voter_ids = 'voteable.down_voter_ids'
59
+ positive_votes_count = 'voteable.up_votes_count'
60
+ negative_votes_count = 'voteable.down_votes_count'
53
61
  point_delta = value_point[:up] - value_point[:down]
54
62
  else
55
- positive_field = :down_voter_ids
56
- negative_field = :up_voter_ids
63
+ positive_voter_ids = 'voteable.down_voter_ids'
64
+ negative_voter_ids = 'voteable.up_voter_ids'
65
+ positive_votes_count = 'voteable.down_votes_count'
66
+ negative_votes_count = 'voteable.up_votes_count'
57
67
  point_delta = -value_point[:up] + value_point[:down]
58
68
  end
59
69
 
60
70
  update_result = collection.update({
61
71
  # Validate voter_id did a vote with value for votee_id
62
72
  :_id => votee_id,
63
- "voteable.#{positive_field.to_s}" => { '$ne' => voter_id },
64
- "voteable.#{negative_field.to_s}" => voter_id
73
+ positive_voter_ids => { '$ne' => voter_id },
74
+ negative_voter_ids => voter_id
65
75
  }, {
66
76
  # then update
67
- '$pull' => { "voteable.#{negative_field.to_s}" => voter_id },
68
- '$push' => { "voteable.#{positive_field.to_s}" => voter_id },
77
+ '$pull' => { negative_voter_ids => voter_id },
78
+ '$push' => { positive_voter_ids => voter_id },
69
79
  '$inc' => {
70
- "voteable.votes_point" => point_delta
80
+ positive_votes_count => +1,
81
+ negative_votes_count => -1,
82
+ 'voteable.votes_point' => point_delta
71
83
  }
72
84
  }, {
73
85
  :safe => true
74
86
  })
75
87
 
76
88
  elsif options[:unvote]
77
- if value == :down
78
- positive_field = :down_voter_ids
79
- negative_field = :up_voter_ids
89
+ if value == :up
90
+ positive_voter_ids = 'voteable.up_voter_ids'
91
+ negative_voter_ids = 'voteable.down_voter_ids'
92
+ positive_votes_count = 'voteable.up_votes_count'
80
93
  else
81
- positive_field = :up_voter_ids
82
- negative_field = :down_voter_ids
94
+ positive_voter_ids = 'voteable.down_voter_ids'
95
+ negative_voter_ids = 'voteable.up_voter_ids'
96
+ positive_votes_count = 'voteable.down_votes_count'
83
97
  end
84
98
 
85
99
  # Check if voter_id did a vote with value for votee_id
86
100
  update_result = collection.update({
87
101
  # Validate voter_id did a vote with value for votee_id
88
102
  :_id => votee_id,
89
- "voteable.#{negative_field.to_s}" => { '$ne' => voter_id },
90
- "voteable.#{positive_field.to_s}" => voter_id
103
+ negative_voter_ids => { '$ne' => voter_id },
104
+ positive_voter_ids => voter_id
91
105
  }, {
92
106
  # then update
93
- '$pull' => { "voteable.#{positive_field.to_s}" => voter_id },
107
+ '$pull' => { positive_voter_ids => voter_id },
94
108
  '$inc' => {
95
- "voteable.votes_count" => -1,
96
- "voteable.votes_point" => -value_point[value]
109
+ positive_votes_count => -1,
110
+ 'voteable.votes_count' => -1,
111
+ 'voteable.votes_point' => -value_point[value]
97
112
  }
98
113
  }, {
99
114
  :safe => true
@@ -101,22 +116,25 @@ module Mongoid
101
116
 
102
117
  else # new vote
103
118
  if value.to_sym == :up
104
- positive_field = :up_voter_ids
119
+ positive_voter_ids = 'voteable.up_voter_ids'
120
+ positive_votes_count = 'voteable.up_votes_count'
105
121
  else
106
- positive_field = :down_voter_ids
122
+ positive_voter_ids = 'voteable.down_voter_ids'
123
+ positive_votes_count = 'voteable.down_votes_count'
107
124
  end
108
125
 
109
126
  update_result = collection.update({
110
127
  # Validate voter_id did not vote for votee_id yet
111
128
  :_id => votee_id,
112
- "voteable.up_voter_ids" => { '$ne' => voter_id },
113
- "voteable.down_voter_ids" => { '$ne' => voter_id }
129
+ 'voteable.up_voter_ids' => { '$ne' => voter_id },
130
+ 'voteable.down_voter_ids' => { '$ne' => voter_id }
114
131
  }, {
115
132
  # then update
116
- '$push' => { "voteable.#{positive_field.to_s}" => voter_id },
133
+ '$push' => { positive_voter_ids => voter_id },
117
134
  '$inc' => {
118
- "voteable.votes_count" => +1,
119
- "voteable.votes_point" => value_point[value] }
135
+ 'voteable.votes_count' => +1,
136
+ positive_votes_count => +1,
137
+ 'voteable.votes_point' => value_point[value] }
120
138
  }, {
121
139
  :safe => true
122
140
  })
@@ -131,27 +149,46 @@ module Mongoid
131
149
  VOTE_POINT[klass].each do |class_name, value_point|
132
150
  # For other class in VOTE_POINT options, if is parent of current class
133
151
  next unless relation_metadata = relations[class_name.underscore]
134
- votee ||= options[:votee] || find(options[:votee_id])
152
+ next unless votee ||= options[:votee] || voteable_related.where(:id => options[:votee_id]).first
135
153
  # If can find current votee foreign_key value for that class
136
- next unless foreign_key_value = votee.read_attribute(relation_metadata.foreign_key.to_sym)
154
+ next unless foreign_key_value = votee.read_attribute(relation_metadata.foreign_key)
137
155
 
138
- # Update that class / collection
139
- inc_options = if options[:revote]
140
- {
141
- "voteable.votes_point" => ( value == :up ?
142
- value_point[:up] - value_point[:down] :
143
- -value_point[:up] + value_point[:down] )
144
- }
156
+ inc_options = {}
157
+
158
+ if options[:revote]
159
+ if value == :up
160
+ inc_options['voteable.votes_point'] = value_point[:up] - value_point[:down]
161
+ unless value_point[:update_counters] == false
162
+ inc_options['voteable.up_votes_count'] = +1
163
+ inc_options['voteable.down_votes_count'] = -1
164
+ end
165
+ else
166
+ inc_options['voteable.votes_point'] = -value_point[:up] + value_point[:down]
167
+ unless value_point[:update_counters] == false
168
+ inc_options['voteable.up_votes_count'] = -1
169
+ inc_options['voteable.down_votes_count'] = +1
170
+ end
171
+ end
145
172
  elsif options[:unvote]
146
- {
147
- "voteable.votes_count" => value_point[:not_increase_votes_count] ? 0 : -1,
148
- "voteable.votes_point" => -value_point[value]
149
- }
150
- else
151
- {
152
- "voteable.votes_count" => value_point[:not_increase_votes_count] ? 0 : 1,
153
- "voteable.votes_point" => value_point[value]
154
- }
173
+ inc_options['voteable.votes_point'] = -value_point[value]
174
+ unless value_point[:update_counters] == false
175
+ inc_options['voteable.votes_count'] = -1
176
+ if value == :up
177
+ inc_options['voteable.up_votes_count'] = -1
178
+ else
179
+ inc_options['voteable.down_votes_count'] = -1
180
+ end
181
+ end
182
+ else # new vote
183
+ inc_options['voteable.votes_point'] = value_point[value]
184
+ unless value_point[:update_counters] == false
185
+ inc_options['voteable.votes_count'] = 1
186
+ if value == :up
187
+ inc_options['voteable.up_votes_count'] = 1
188
+ else
189
+ inc_options['voteable.down_votes_count'] = 1
190
+ end
191
+ end
155
192
  end
156
193
 
157
194
  class_name.constantize.collection.update(
@@ -160,8 +197,7 @@ module Mongoid
160
197
  )
161
198
  end
162
199
  end
163
-
164
- successed
200
+ true
165
201
  end
166
202
 
167
203
  end
@@ -193,26 +229,6 @@ module Mongoid
193
229
  return :down if down_voter_ids.include?(voter_id)
194
230
  end
195
231
 
196
- # Get the number of up votes
197
- def up_votes_count
198
- up_voter_ids.length
199
- end
200
-
201
- # Get the number of down votes
202
- def down_votes_count
203
- down_voter_ids.length
204
- end
205
-
206
- # Get the number of votes count
207
- def votes_count
208
- voteable.try(:[], 'votes_count') || 0
209
- end
210
-
211
- # Get the votes point
212
- def votes_point
213
- voteable.try(:[], 'votes_point') || 0
214
- end
215
-
216
232
  # Array of up voter ids
217
233
  def up_voter_ids
218
234
  voteable.try(:[], 'up_voter_ids') || []
@@ -222,10 +238,10 @@ module Mongoid
222
238
  def down_voter_ids
223
239
  voteable.try(:[], 'down_voter_ids') || []
224
240
  end
241
+
242
+ def voteable
243
+ read_attribute('voteable')
244
+ end
225
245
 
226
- private
227
- def voteable
228
- read_attribute('voteable')
229
- end
230
246
  end
231
247
  end
@@ -0,0 +1,104 @@
1
+ module Mongoid
2
+ module Voteable
3
+ module Stats
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ index 'voteable.up_votes_count'
8
+ index 'voteable.down_votes_count'
9
+ index 'voteable.votes_count'
10
+ index 'voteable.votes_point'
11
+
12
+ scope :most_up_voted, order_by(['voteable.up_votes_count', :desc])
13
+ scope :most_down_voted, order_by(['voteable.down_votes_count', :desc])
14
+ scope :most_voted, order_by(['voteable.votes_count', :desc])
15
+ scope :best_voted, order_by(['voteable.votes_point', :desc])
16
+ end
17
+
18
+ def self.remake_stats
19
+ Mongoid::Voteable::VOTE_POINT.each do |class_name, value_point|
20
+ klass = class_name.constantize
21
+ klass_value_point = value_point[class_name]
22
+ klass.voteable_related.each{ |doc|
23
+ doc.remake_stats(klass_value_point)
24
+ }
25
+ end
26
+
27
+ VOTE_POINT.each do |class_name, value_point|
28
+ klass = class_name.constantize
29
+ value_point.each do |parent_class_name, parent_value_point|
30
+ relation_metadata = klass.relations[parent_class_name.underscore]
31
+ if relation_metadata
32
+ parent_class = parent_class_name.constantize
33
+ foreign_key = relation_metadata.foreign_key
34
+ klass.voteable_related.each{ |doc|
35
+ doc.update_parent_stats(parent_class, foreign_key, parent_value_point)
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def remake_stats(value_point)
43
+ up_count = up_voter_ids.length
44
+ down_count = down_voter_ids.length
45
+ # puts "#{self.class}(#{id}): #{up_count}, #{down_count}" # DEBUG
46
+ update_attributes(
47
+ 'voteable.up_votes_count' => up_count,
48
+ 'voteable.down_votes_count' => down_count,
49
+ 'voteable.votes_count' => up_count + down_count,
50
+ 'voteable.votes_point' => value_point[:up]*up_count + value_point[:down]*down_count
51
+ )
52
+ end
53
+
54
+ def update_parent_stats(parent_class, foreign_key, value_point)
55
+ parent_id = read_attribute(foreign_key.to_sym)
56
+ if parent_id
57
+ up_count = up_voter_ids.length
58
+ down_count = down_voter_ids.length
59
+
60
+ return if up_count == 0 && down_count == 0
61
+
62
+ inc_options = {
63
+ 'voteable.votes_point' => value_point[:up]*up_count + value_point[:down]*down_count
64
+ }
65
+
66
+ unless value_point[:update_counters] == false
67
+ inc_options.merge!(
68
+ 'voteable.votes_count' => up_count + down_count,
69
+ 'voteable.up_votes_count' => up_count,
70
+ 'voteable.down_votes_count' => down_count
71
+ )
72
+ end
73
+
74
+ # puts inc_options.inspect # DEBUG
75
+ parent_class.collection.update(
76
+ { :_id => parent_id },
77
+ { '$inc' => inc_options }
78
+ )
79
+ end
80
+ end
81
+
82
+ # Get the number of up votes
83
+ def up_votes_count
84
+ voteable.try(:[], 'up_votes_count') || 0
85
+ end
86
+
87
+ # Get the number of down votes
88
+ def down_votes_count
89
+ voteable.try(:[], 'down_votes_count') || 0
90
+ end
91
+
92
+ # Get the number of votes
93
+ def votes_count
94
+ voteable.try(:[], 'votes_count') || 0
95
+ end
96
+
97
+ # Get the votes point
98
+ def votes_point
99
+ voteable.try(:[], 'votes_point') || 0
100
+ end
101
+
102
+ end
103
+ end
104
+ end
File without changes
@@ -7,5 +7,5 @@ class Comment
7
7
  referenced_in :post
8
8
 
9
9
  vote_point self, :up => +1, :down => -3
10
- vote_point Post, :up => +2, :down => -1
10
+ vote_point Post, :up => +2, :down => -1 #, :not_update_counters => true
11
11
  end
@@ -8,16 +8,19 @@ describe Mongoid::Voteable do
8
8
  @post2 = Post.create!
9
9
 
10
10
  @comment = @post2.comments.create!
11
-
12
11
  @user1 = User.create!
13
12
  @user2 = User.create!
14
13
  end
15
14
 
16
15
  context "just created" do
17
16
  it 'voteable votes_count, votes_point should be zero' do
17
+ @post1.up_votes_count.should == 0
18
+ @post1.down_votes_count.should == 0
18
19
  @post1.votes_count.should == 0
19
20
  @post1.votes_point.should == 0
20
21
 
22
+ @post2.up_votes_count.should == 0
23
+ @post2.down_votes_count.should == 0
21
24
  @post2.votes_count.should == 0
22
25
  @post2.votes_point.should == 0
23
26
  end
@@ -39,12 +42,16 @@ describe Mongoid::Voteable do
39
42
  Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => 'up')
40
43
  @post1.reload
41
44
 
45
+ @post1.up_votes_count.should == 0
46
+ @post1.down_votes_count.should == 0
42
47
  @post1.votes_count.should == 0
43
48
  @post1.votes_point.should == 0
44
49
 
45
50
  Post.vote(:revote => true, :votee_id => @post2.id, :voter_id => @user2.id, :value => :down)
46
51
  @post2.reload
47
52
 
53
+ @post2.up_votes_count.should == 0
54
+ @post2.down_votes_count.should == 0
48
55
  @post2.votes_count.should == 0
49
56
  @post2.votes_point.should == 0
50
57
  end
@@ -53,10 +60,13 @@ describe Mongoid::Voteable do
53
60
  context 'user1 vote up post1 the first time' do
54
61
  before :all do
55
62
  Post.vote(:votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
63
+ Mongoid::Voteable::Stats.remake_stats
56
64
  @post1.reload
57
65
  end
58
66
 
59
67
  it '' do
68
+ @post1.up_votes_count.should == 1
69
+ @post1.down_votes_count.should == 0
60
70
  @post1.votes_count.should == 1
61
71
  @post1.votes_point.should == 1
62
72
 
@@ -71,6 +81,8 @@ describe Mongoid::Voteable do
71
81
  Post.vote(:revote => false, :votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
72
82
  @post1.reload
73
83
 
84
+ @post1.up_votes_count.should == 1
85
+ @post1.down_votes_count.should == 0
74
86
  @post1.votes_count.should == 1
75
87
  @post1.votes_point.should == 1
76
88
 
@@ -81,10 +93,13 @@ describe Mongoid::Voteable do
81
93
  context 'user2 vote down post1 the first time' do
82
94
  before :all do
83
95
  Post.vote(:votee_id => @post1.id, :voter_id => @user2.id, :value => :down)
96
+ Mongoid::Voteable::Stats.remake_stats
84
97
  @post1.reload
85
98
  end
86
99
 
87
100
  it '' do
101
+ @post1.up_votes_count.should == 1
102
+ @post1.down_votes_count.should == 1
88
103
  @post1.votes_count.should == 2
89
104
  @post1.votes_point.should == 0
90
105
 
@@ -99,10 +114,13 @@ describe Mongoid::Voteable do
99
114
  context 'user1 change vote on post1 from up to down' do
100
115
  before :all do
101
116
  Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => :down)
117
+ Mongoid::Voteable::Stats.remake_stats
102
118
  @post1.reload
103
119
  end
104
120
 
105
121
  it '' do
122
+ @post1.up_votes_count.should == 0
123
+ @post1.down_votes_count.should == 2
106
124
  @post1.votes_count.should == 2
107
125
  @post1.votes_point.should == -2
108
126
 
@@ -117,10 +135,13 @@ describe Mongoid::Voteable do
117
135
  context 'user1 vote down post2 the first time' do
118
136
  before :all do
119
137
  Post.vote(:votee_id => @post2.id, :voter_id => @user1.id, :value => :down)
138
+ Mongoid::Voteable::Stats.remake_stats
120
139
  @post2.reload
121
140
  end
122
141
 
123
142
  it '' do
143
+ @post2.up_votes_count.should == 0
144
+ @post2.down_votes_count.should == 1
124
145
  @post2.votes_count.should == 1
125
146
  @post2.votes_point.should == -1
126
147
 
@@ -134,10 +155,13 @@ describe Mongoid::Voteable do
134
155
  context 'user1 change vote on post2 from down to up' do
135
156
  before :all do
136
157
  Post.vote(:revote => true, :votee_id => @post2.id.to_s, :voter_id => @user1.id.to_s, :value => :up)
158
+ Mongoid::Voteable::Stats.remake_stats
137
159
  @post2.reload
138
160
  end
139
161
 
140
162
  it '' do
163
+ @post2.up_votes_count.should == 1
164
+ @post2.down_votes_count.should == 0
141
165
  @post2.votes_count.should == 1
142
166
  @post2.votes_point.should == 1
143
167
 
@@ -152,14 +176,19 @@ describe Mongoid::Voteable do
152
176
  context 'user1 vote up post2 comment the first time' do
153
177
  before :all do
154
178
  @comment.vote(:voter_id => @user1.id, :value => :up)
179
+ Mongoid::Voteable::Stats.remake_stats
155
180
  @comment.reload
156
181
  @post2.reload
157
182
  end
158
183
 
159
184
  it '' do
185
+ @post2.up_votes_count.should == 2
186
+ @post2.down_votes_count.should == 0
160
187
  @post2.votes_count.should == 2
161
188
  @post2.votes_point.should == 3
162
189
 
190
+ @comment.up_votes_count.should == 1
191
+ @comment.down_votes_count.should == 0
163
192
  @comment.votes_count.should == 1
164
193
  @comment.votes_point.should == 1
165
194
  end
@@ -169,14 +198,19 @@ describe Mongoid::Voteable do
169
198
  context 'user1 revote post2 comment from up to down' do
170
199
  before :all do
171
200
  @user1.vote(:votee => @comment, :value => :down)
201
+ Mongoid::Voteable::Stats.remake_stats
172
202
  @comment.reload
173
203
  @post2.reload
174
204
  end
175
205
 
176
206
  it '' do
207
+ @post2.up_votes_count.should == 1
208
+ @post2.down_votes_count.should == 1
177
209
  @post2.votes_count.should == 2
178
210
  @post2.votes_point.should == 0
179
211
 
212
+ @comment.up_votes_count.should == 0
213
+ @comment.down_votes_count.should == 1
180
214
  @comment.votes_count.should == 1
181
215
  @comment.votes_point.should == -3
182
216
  end
@@ -184,9 +218,13 @@ describe Mongoid::Voteable do
184
218
  it 'revote with wrong value has no effect' do
185
219
  @user1.vote(:votee => @comment, :value => :down)
186
220
 
221
+ @post2.up_votes_count.should == 1
222
+ @post2.down_votes_count.should == 1
187
223
  @post2.votes_count.should == 2
188
224
  @post2.votes_point.should == 0
189
225
 
226
+ @comment.up_votes_count.should == 0
227
+ @comment.down_votes_count.should == 1
190
228
  @comment.votes_count.should == 1
191
229
  @comment.votes_point.should == -3
192
230
  end
@@ -195,10 +233,13 @@ describe Mongoid::Voteable do
195
233
  context "user1 unvote on post1" do
196
234
  before(:all) do
197
235
  @post1.vote(:voter_id => @user1.id, :votee_id => @post1.id, :unvote => true)
236
+ Mongoid::Voteable::Stats.remake_stats
198
237
  @post1.reload
199
238
  end
200
239
 
201
240
  it "" do
241
+ @post1.up_votes_count.should == 0
242
+ @post1.down_votes_count.should == 1
202
243
  @post1.votes_count.should == 1
203
244
  @post1.votes_point.should == -1
204
245
 
@@ -209,8 +250,24 @@ describe Mongoid::Voteable do
209
250
  end
210
251
  end
211
252
 
212
- context "@post1 has 1 vote and -1 point, @post2 has 2 votes and 0 point" do
213
- it "" do
253
+ context "@post1 has 1 down vote and -1 point, @post2 has 1 up vote, 1 down vote and 0 point" do
254
+ it "verify @post1 counters" do
255
+ @post1.up_votes_count.should == 0
256
+ @post1.down_votes_count.should == 1
257
+ @post1.votes_count.should == 1
258
+ @post1.votes_point.should == -1
259
+ end
260
+
261
+ it "verify @post2 counters" do
262
+ @post2.up_votes_count.should == 1
263
+ @post2.down_votes_count.should == 1
264
+ @post2.votes_count.should == 2
265
+ @post2.votes_point.should == 0
266
+ end
267
+
268
+ it "test scopes" do
269
+ Post.most_up_voted.first.should == @post2
270
+ Post.most_down_voted.first.should == @post1
214
271
  Post.most_voted.first.should == @post2
215
272
  Post.best_voted.first.should == @post2
216
273
  end
@@ -219,16 +276,42 @@ describe Mongoid::Voteable do
219
276
  context "user1 unvote on comment" do
220
277
  before(:all) do
221
278
  @user1.unvote(@comment)
279
+ Mongoid::Voteable::Stats.remake_stats
222
280
  @comment.reload
223
281
  @post2.reload
224
282
  end
225
283
 
226
- it "" do
284
+ it "" do
285
+ @post2.up_votes_count.should == 1
286
+ @post2.down_votes_count.should == 0
227
287
  @post2.votes_count.should == 1
228
288
  @post2.votes_point.should == 1
229
289
 
290
+ @comment.up_votes_count.should == 0
291
+ @comment.down_votes_count.should == 0
230
292
  @comment.votes_count.should == 0
231
293
  @comment.votes_point.should == 0
232
294
  end
233
295
  end
234
- end
296
+
297
+ context 'final' do
298
+ it "test remake stats" do
299
+ Mongoid::Voteable::Stats.remake_stats
300
+
301
+ @post1.up_votes_count.should == 0
302
+ @post1.down_votes_count.should == 1
303
+ @post1.votes_count.should == 1
304
+ @post1.votes_point.should == -1
305
+
306
+ @post2.up_votes_count.should == 1
307
+ @post2.down_votes_count.should == 0
308
+ @post2.votes_count.should == 1
309
+ @post2.votes_point.should == 1
310
+
311
+ @comment.up_votes_count.should == 0
312
+ @comment.down_votes_count.should == 0
313
+ @comment.votes_count.should == 0
314
+ @comment.votes_point.should == 0
315
+ end
316
+ end
317
+ end
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.name = "voteable_mongoid"
7
7
  s.version = VoteableMongoid::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Alex Nguyen", "Stefan Nguyen"]
9
+ s.authors = ["Alex Nguyen"]
10
10
  s.email = ["alex@vinova.sg"]
11
11
  s.homepage = "http://vinova.sg"
12
12
  s.summary = %q{Add Up / Down Voting for Mongoid}
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.add_dependency 'mongoid', '~> 2.0.0.rc'
16
16
  s.add_development_dependency 'rspec'
17
17
  s.add_development_dependency 'bson_ext'
18
+ s.add_development_dependency 'watchr'
18
19
 
19
20
  s.rubyforge_project = "voteable_mongoid"
20
21
 
metadata CHANGED
@@ -1,17 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voteable_mongoid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Nguyen
14
- - Stefan Nguyen
15
14
  autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
@@ -64,6 +63,20 @@ dependencies:
64
63
  version: "0"
65
64
  type: :development
66
65
  version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: watchr
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :development
79
+ version_requirements: *id004
67
80
  description: Add Up / Down Voting for Mongoid (built for speed by using one Mongodb update-in-place for each collection when provided enough information)
68
81
  email:
69
82
  - alex@vinova.sg
@@ -75,6 +88,7 @@ extra_rdoc_files: []
75
88
 
76
89
  files:
77
90
  - .gitignore
91
+ - .watchr
78
92
  - Gemfile
79
93
  - README.rdoc
80
94
  - Rakefile
@@ -98,17 +112,19 @@ files:
98
112
  - doc/js/jquery.js
99
113
  - doc/method_list.html
100
114
  - doc/top-level-namespace.html
101
- - lib/mongoid/voteable.rb
102
- - lib/mongoid/voter.rb
103
115
  - lib/voteable_mongoid.rb
116
+ - lib/voteable_mongoid/railties/database.rake
104
117
  - lib/voteable_mongoid/version.rb
118
+ - lib/voteable_mongoid/voteable.rb
119
+ - lib/voteable_mongoid/voteable/stats.rb
120
+ - lib/voteable_mongoid/voter.rb
105
121
  - spec/.rspec
106
122
  - spec/models/comment.rb
107
123
  - spec/models/post.rb
108
124
  - spec/models/user.rb
109
125
  - spec/spec_helper.rb
110
- - spec/voteable_spec.rb
111
- - spec/voter_spec.rb
126
+ - spec/voteable_mongoid/voteable_spec.rb
127
+ - spec/voteable_mongoid/voter_spec.rb
112
128
  - voteable_mongoid.gemspec
113
129
  has_rdoc: true
114
130
  homepage: http://vinova.sg
@@ -149,5 +165,5 @@ test_files:
149
165
  - spec/models/post.rb
150
166
  - spec/models/user.rb
151
167
  - spec/spec_helper.rb
152
- - spec/voteable_spec.rb
153
- - spec/voter_spec.rb
168
+ - spec/voteable_mongoid/voteable_spec.rb
169
+ - spec/voteable_mongoid/voter_spec.rb