voteable_mongoid 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/voteable_mongoid/railtie.rb +17 -0
- data/lib/voteable_mongoid/railties/database.rake +1 -1
- data/lib/voteable_mongoid/version.rb +1 -1
- data/lib/voteable_mongoid/voteable/stats.rb +47 -39
- data/lib/voteable_mongoid.rb +4 -0
- data/spec/voteable_mongoid/voteable_spec.rb +10 -10
- data/voteable_mongoid.gemspec +2 -2
- metadata +7 -7
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rails #:nodoc:
|
2
|
+
module VoteableMongoid #:nodoc:
|
3
|
+
class Railtie < Rails::Railtie #:nodoc:
|
4
|
+
|
5
|
+
initializer "preload all application models" do |app|
|
6
|
+
config.to_prepare do
|
7
|
+
::Rails::Mongoid.load_models(app)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
rake_tasks do
|
12
|
+
load 'voteable_mongoid/railties/database.rake'
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -15,15 +15,55 @@ module Mongoid
|
|
15
15
|
scope :best_voted, order_by(['voteable.votes_point', :desc])
|
16
16
|
end
|
17
17
|
|
18
|
-
|
18
|
+
# Get the number of up votes
|
19
|
+
def up_votes_count
|
20
|
+
voteable.try(:[], 'up_votes_count') || 0
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get the number of down votes
|
24
|
+
def down_votes_count
|
25
|
+
voteable.try(:[], 'down_votes_count') || 0
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get the number of votes
|
29
|
+
def votes_count
|
30
|
+
voteable.try(:[], 'votes_count') || 0
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get the votes point
|
34
|
+
def votes_point
|
35
|
+
voteable.try(:[], 'votes_point') || 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.remake(log = false)
|
39
|
+
remake_stats(log)
|
40
|
+
update_parent_stats(log)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.remake_stats(log)
|
19
44
|
Mongoid::Voteable::VOTE_POINT.each do |class_name, value_point|
|
20
45
|
klass = class_name.constantize
|
21
46
|
klass_value_point = value_point[class_name]
|
47
|
+
puts "Generating stats for #{class_name}" if log
|
22
48
|
klass.voteable_related.each{ |doc|
|
23
49
|
doc.remake_stats(klass_value_point)
|
24
50
|
}
|
25
51
|
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def remake_stats(value_point)
|
55
|
+
up_count = up_voter_ids.length
|
56
|
+
down_count = down_voter_ids.length
|
26
57
|
|
58
|
+
update_attributes(
|
59
|
+
'voteable.up_votes_count' => up_count,
|
60
|
+
'voteable.down_votes_count' => down_count,
|
61
|
+
'voteable.votes_count' => up_count + down_count,
|
62
|
+
'voteable.votes_point' => value_point[:up]*up_count + value_point[:down]*down_count
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.update_parent_stats(log)
|
27
67
|
VOTE_POINT.each do |class_name, value_point|
|
28
68
|
klass = class_name.constantize
|
29
69
|
value_point.each do |parent_class_name, parent_value_point|
|
@@ -31,6 +71,7 @@ module Mongoid
|
|
31
71
|
if relation_metadata
|
32
72
|
parent_class = parent_class_name.constantize
|
33
73
|
foreign_key = relation_metadata.foreign_key
|
74
|
+
puts "Updating stats for #{class_name} > #{parent_class_name}" if log
|
34
75
|
klass.voteable_related.each{ |doc|
|
35
76
|
doc.update_parent_stats(parent_class, foreign_key, parent_value_point)
|
36
77
|
}
|
@@ -38,31 +79,19 @@ module Mongoid
|
|
38
79
|
end
|
39
80
|
end
|
40
81
|
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
|
-
|
82
|
+
|
54
83
|
def update_parent_stats(parent_class, foreign_key, value_point)
|
55
84
|
parent_id = read_attribute(foreign_key.to_sym)
|
56
85
|
if parent_id
|
57
86
|
up_count = up_voter_ids.length
|
58
87
|
down_count = down_voter_ids.length
|
59
|
-
|
88
|
+
|
60
89
|
return if up_count == 0 && down_count == 0
|
61
90
|
|
62
91
|
inc_options = {
|
63
92
|
'voteable.votes_point' => value_point[:up]*up_count + value_point[:down]*down_count
|
64
93
|
}
|
65
|
-
|
94
|
+
|
66
95
|
unless value_point[:update_counters] == false
|
67
96
|
inc_options.merge!(
|
68
97
|
'voteable.votes_count' => up_count + down_count,
|
@@ -70,35 +99,14 @@ module Mongoid
|
|
70
99
|
'voteable.down_votes_count' => down_count
|
71
100
|
)
|
72
101
|
end
|
73
|
-
|
74
|
-
# puts inc_options.inspect # DEBUG
|
102
|
+
|
75
103
|
parent_class.collection.update(
|
76
104
|
{ :_id => parent_id },
|
77
105
|
{ '$inc' => inc_options }
|
78
106
|
)
|
79
107
|
end
|
80
108
|
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
|
-
|
109
|
+
|
102
110
|
end
|
103
111
|
end
|
104
112
|
end
|
data/lib/voteable_mongoid.rb
CHANGED
@@ -60,7 +60,7 @@ describe Mongoid::Voteable do
|
|
60
60
|
context 'user1 vote up post1 the first time' do
|
61
61
|
before :all do
|
62
62
|
Post.vote(:votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
|
63
|
-
Mongoid::Voteable::Stats.
|
63
|
+
Mongoid::Voteable::Stats.remake
|
64
64
|
@post1.reload
|
65
65
|
end
|
66
66
|
|
@@ -93,7 +93,7 @@ describe Mongoid::Voteable do
|
|
93
93
|
context 'user2 vote down post1 the first time' do
|
94
94
|
before :all do
|
95
95
|
Post.vote(:votee_id => @post1.id, :voter_id => @user2.id, :value => :down)
|
96
|
-
Mongoid::Voteable::Stats.
|
96
|
+
Mongoid::Voteable::Stats.remake
|
97
97
|
@post1.reload
|
98
98
|
end
|
99
99
|
|
@@ -114,7 +114,7 @@ describe Mongoid::Voteable do
|
|
114
114
|
context 'user1 change vote on post1 from up to down' do
|
115
115
|
before :all do
|
116
116
|
Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => :down)
|
117
|
-
Mongoid::Voteable::Stats.
|
117
|
+
Mongoid::Voteable::Stats.remake
|
118
118
|
@post1.reload
|
119
119
|
end
|
120
120
|
|
@@ -135,7 +135,7 @@ describe Mongoid::Voteable do
|
|
135
135
|
context 'user1 vote down post2 the first time' do
|
136
136
|
before :all do
|
137
137
|
Post.vote(:votee_id => @post2.id, :voter_id => @user1.id, :value => :down)
|
138
|
-
Mongoid::Voteable::Stats.
|
138
|
+
Mongoid::Voteable::Stats.remake
|
139
139
|
@post2.reload
|
140
140
|
end
|
141
141
|
|
@@ -155,7 +155,7 @@ describe Mongoid::Voteable do
|
|
155
155
|
context 'user1 change vote on post2 from down to up' do
|
156
156
|
before :all do
|
157
157
|
Post.vote(:revote => true, :votee_id => @post2.id.to_s, :voter_id => @user1.id.to_s, :value => :up)
|
158
|
-
Mongoid::Voteable::Stats.
|
158
|
+
Mongoid::Voteable::Stats.remake
|
159
159
|
@post2.reload
|
160
160
|
end
|
161
161
|
|
@@ -176,7 +176,7 @@ describe Mongoid::Voteable do
|
|
176
176
|
context 'user1 vote up post2 comment the first time' do
|
177
177
|
before :all do
|
178
178
|
@comment.vote(:voter_id => @user1.id, :value => :up)
|
179
|
-
Mongoid::Voteable::Stats.
|
179
|
+
Mongoid::Voteable::Stats.remake
|
180
180
|
@comment.reload
|
181
181
|
@post2.reload
|
182
182
|
end
|
@@ -198,7 +198,7 @@ describe Mongoid::Voteable do
|
|
198
198
|
context 'user1 revote post2 comment from up to down' do
|
199
199
|
before :all do
|
200
200
|
@user1.vote(:votee => @comment, :value => :down)
|
201
|
-
Mongoid::Voteable::Stats.
|
201
|
+
Mongoid::Voteable::Stats.remake
|
202
202
|
@comment.reload
|
203
203
|
@post2.reload
|
204
204
|
end
|
@@ -233,7 +233,7 @@ describe Mongoid::Voteable do
|
|
233
233
|
context "user1 unvote on post1" do
|
234
234
|
before(:all) do
|
235
235
|
@post1.vote(:voter_id => @user1.id, :votee_id => @post1.id, :unvote => true)
|
236
|
-
Mongoid::Voteable::Stats.
|
236
|
+
Mongoid::Voteable::Stats.remake
|
237
237
|
@post1.reload
|
238
238
|
end
|
239
239
|
|
@@ -276,7 +276,7 @@ describe Mongoid::Voteable do
|
|
276
276
|
context "user1 unvote on comment" do
|
277
277
|
before(:all) do
|
278
278
|
@user1.unvote(@comment)
|
279
|
-
Mongoid::Voteable::Stats.
|
279
|
+
Mongoid::Voteable::Stats.remake
|
280
280
|
@comment.reload
|
281
281
|
@post2.reload
|
282
282
|
end
|
@@ -296,7 +296,7 @@ describe Mongoid::Voteable do
|
|
296
296
|
|
297
297
|
context 'final' do
|
298
298
|
it "test remake stats" do
|
299
|
-
Mongoid::Voteable::Stats.
|
299
|
+
Mongoid::Voteable::Stats.remake
|
300
300
|
|
301
301
|
@post1.up_votes_count.should == 0
|
302
302
|
@post1.down_votes_count.should == 1
|
data/voteable_mongoid.gemspec
CHANGED
@@ -8,11 +8,11 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Alex Nguyen"]
|
10
10
|
s.email = ["alex@vinova.sg"]
|
11
|
-
s.homepage = "http://vinova.sg"
|
11
|
+
s.homepage = "http://vinova.sg/"
|
12
12
|
s.summary = %q{Add Up / Down Voting for Mongoid}
|
13
13
|
s.description = %q{Add Up / Down Voting for Mongoid (built for speed by using one Mongodb update-in-place for each collection when provided enough information)}
|
14
14
|
|
15
|
-
s.add_dependency 'mongoid', '~> 2.0.0
|
15
|
+
s.add_dependency 'mongoid', '~> 2.0.0'
|
16
16
|
s.add_development_dependency 'rspec'
|
17
17
|
s.add_development_dependency 'bson_ext'
|
18
18
|
s.add_development_dependency 'watchr'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voteable_mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 5
|
10
|
+
version: 0.4.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Nguyen
|
@@ -26,13 +26,12 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 15
|
30
30
|
segments:
|
31
31
|
- 2
|
32
32
|
- 0
|
33
33
|
- 0
|
34
|
-
|
35
|
-
version: 2.0.0.rc
|
34
|
+
version: 2.0.0
|
36
35
|
type: :runtime
|
37
36
|
version_requirements: *id001
|
38
37
|
- !ruby/object:Gem::Dependency
|
@@ -113,6 +112,7 @@ files:
|
|
113
112
|
- doc/method_list.html
|
114
113
|
- doc/top-level-namespace.html
|
115
114
|
- lib/voteable_mongoid.rb
|
115
|
+
- lib/voteable_mongoid/railtie.rb
|
116
116
|
- lib/voteable_mongoid/railties/database.rake
|
117
117
|
- lib/voteable_mongoid/version.rb
|
118
118
|
- lib/voteable_mongoid/voteable.rb
|
@@ -127,7 +127,7 @@ files:
|
|
127
127
|
- spec/voteable_mongoid/voter_spec.rb
|
128
128
|
- voteable_mongoid.gemspec
|
129
129
|
has_rdoc: true
|
130
|
-
homepage: http://vinova.sg
|
130
|
+
homepage: http://vinova.sg/
|
131
131
|
licenses: []
|
132
132
|
|
133
133
|
post_install_message:
|