voteable_mongoid 0.6.3 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +3 -0
- data/README.rdoc +15 -8
- data/lib/voteable_mongoid/version.rb +1 -1
- data/lib/voteable_mongoid/voteable.rb +16 -2
- data/lib/voteable_mongoid/voter.rb +1 -25
- data/spec/voteable_mongoid/voteable_spec.rb +12 -12
- data/spec/voteable_mongoid/voter_spec.rb +18 -18
- metadata +4 -4
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
== 0.6.4
|
2
|
+
* Drop Voter#votees, Voter#up_votees, Voter#down_votees in favor of Voteable#voted_by(voter), Voteable#up_voted_by(voter), Voteable#down_voted_by(voter) scopes
|
3
|
+
|
1
4
|
== 0.6.3
|
2
5
|
* Add rake db:mongoid:voteable:migrate_old_votes to migrate vote data created by version < 0.6.0 to new vote data storage
|
3
6
|
|
data/README.rdoc
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
= Voteable Mongoid
|
2
2
|
|
3
|
-
Voteable Mongoid allows you to make your Mongoid::Document objects voteable (up or down)
|
3
|
+
Voteable Mongoid allows you to make your Mongoid::Document objects voteable (up or down).
|
4
4
|
For instance, in a forum, a user can vote up (or down) on a post or a comment.
|
5
5
|
|
6
|
-
Voteable Mongoid is built for speed. It minimizes vote data storage and uses
|
6
|
+
Voteable Mongoid is built for speed. It minimizes vote data storage and uses
|
7
|
+
only one database request per collection to do both data validation and data update.
|
7
8
|
|
8
9
|
Sample app at https://github.com/vinova/simple_qa
|
9
10
|
|
@@ -72,30 +73,36 @@ user.rb
|
|
72
73
|
puts @post.up_votes_count
|
73
74
|
puts @post.down_votes_count
|
74
75
|
|
75
|
-
=== Getting the list of voted objects
|
76
|
+
=== Getting the list of voted objects of a class
|
76
77
|
|
77
|
-
@user
|
78
|
+
Post.voted_by(@user)
|
79
|
+
Post.up_voted_by(@user)
|
80
|
+
Post.down_voted_by(@user)
|
78
81
|
|
79
82
|
=== Undo a vote
|
80
83
|
|
81
84
|
@user.unvote(@comment)
|
82
85
|
|
83
|
-
|
86
|
+
== Utilities
|
84
87
|
|
88
|
+
=== Re-generate counters and vote points in case you change :up / :down vote points
|
85
89
|
Rails
|
86
90
|
rake db:mongoid:voteable:remake_stats
|
87
|
-
|
88
91
|
Ruby
|
89
92
|
Mongoid::Voteable::Stats.remake
|
90
93
|
|
91
94
|
=== Set counters and point to 0 for uninitialized voteable objects in order sort and query
|
92
|
-
|
93
95
|
Rails
|
94
96
|
rake db:mongoid:voteable:init_stats
|
95
|
-
|
96
97
|
Ruby
|
97
98
|
Mongoid::Voteable::Stats.init
|
98
99
|
|
100
|
+
=== Migrate from version < 0.6.0
|
101
|
+
Rails
|
102
|
+
rake db:mongoid:voteable:migrate_old_votes
|
103
|
+
Ruby
|
104
|
+
Mongoid::Voteable.migrate_old_votes
|
105
|
+
|
99
106
|
== Credits
|
100
107
|
|
101
108
|
* Alex N. - Author
|
@@ -11,6 +11,21 @@ module Mongoid
|
|
11
11
|
include Mongoid::Voteable::Stats
|
12
12
|
field :votes, :type => Mongoid::Voteable::Votes
|
13
13
|
|
14
|
+
scope :voted_by, lambda { |voter|
|
15
|
+
voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter._id
|
16
|
+
any_of({ UP_VOTER_IDS => voter_id }, { DOWN_VOTER_IDS => voter_id })
|
17
|
+
}
|
18
|
+
|
19
|
+
scope :up_voted_by, lambda { |voter|
|
20
|
+
voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter._id
|
21
|
+
where( UP_VOTER_IDS => voter_id )
|
22
|
+
}
|
23
|
+
|
24
|
+
scope :down_voted_by, lambda { |voter|
|
25
|
+
voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter._id
|
26
|
+
where( DOWN_VOTER_IDS => voter_id )
|
27
|
+
}
|
28
|
+
|
14
29
|
before_create do
|
15
30
|
# Init votes so that counters and point have numeric values (0)
|
16
31
|
self.votes = VOTES_DEFAULT_ATTRIBUTES
|
@@ -201,7 +216,6 @@ module Mongoid
|
|
201
216
|
end
|
202
217
|
true
|
203
218
|
end
|
204
|
-
|
205
219
|
end
|
206
220
|
|
207
221
|
# Make a vote on this votee
|
@@ -224,7 +238,7 @@ module Mongoid
|
|
224
238
|
|
225
239
|
# Get a voted value on this votee
|
226
240
|
#
|
227
|
-
# @param [Mongoid Object, BSON::ObjectId] voter is Mongoid object the id of the voter who made the vote
|
241
|
+
# @param [Mongoid Object, BSON::ObjectId] voter is Mongoid object or the id of the voter who made the vote
|
228
242
|
def vote_value(voter)
|
229
243
|
voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter._id
|
230
244
|
return :up if up_voter_ids.include?(voter_id)
|
@@ -2,30 +2,6 @@ module Mongoid
|
|
2
2
|
module Voter
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
# Get list of voted votees
|
6
|
-
#
|
7
|
-
# @param [Class] klass the voteable class, e.g. `Post` or `Comment`
|
8
|
-
# @return [Array, nil] an array of voteable objects voted by this voter
|
9
|
-
def votees(klass)
|
10
|
-
klass.any_of({ Voteable::UP_VOTER_IDS => _id }, { Voteable::DOWN_VOTER_IDS => _id })
|
11
|
-
end
|
12
|
-
|
13
|
-
# Get list of up voted votees
|
14
|
-
#
|
15
|
-
# @param [Class] klass the voteable class, e.g. `Post` or `Comment`
|
16
|
-
# @return [Array, nil] an array of voteable objects up voted by this voter
|
17
|
-
def up_votees(klass)
|
18
|
-
klass.where(Voteable::UP_VOTER_IDS => _id)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Get list of down voted votees
|
22
|
-
#
|
23
|
-
# @param [Class] klass the voteable class, e.g. `Post` or `Comment`
|
24
|
-
# @return [Array, nil] an array of voteable objects down voted by this voter
|
25
|
-
def down_votees(klass)
|
26
|
-
klass.where(Voteable::DOWN_VOTER_IDS => _id)
|
27
|
-
end
|
28
|
-
|
29
5
|
# Check to see if this voter voted on the votee or not
|
30
6
|
#
|
31
7
|
# @param [Hash, Object] options the hash containing the votee, or the votee itself
|
@@ -45,7 +21,7 @@ module Mongoid
|
|
45
21
|
end
|
46
22
|
end
|
47
23
|
|
48
|
-
|
24
|
+
votee_class.voted_by(self).where(:_id => votee_id).count == 1
|
49
25
|
end
|
50
26
|
|
51
27
|
# Get the voted value on a votee
|
@@ -33,9 +33,9 @@ describe Mongoid::Voteable do
|
|
33
33
|
@post2.down_voter_ids.should be_empty
|
34
34
|
end
|
35
35
|
|
36
|
-
it 'voter
|
37
|
-
@user1
|
38
|
-
@user2
|
36
|
+
it 'posts voted voter should be empty' do
|
37
|
+
Post.voted_by(@user1).should be_empty
|
38
|
+
Post.voted_by(@user2).should be_empty
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'test Stats.init' do
|
@@ -91,8 +91,8 @@ describe Mongoid::Voteable do
|
|
91
91
|
@post1.vote_value(@user1).should == :up
|
92
92
|
@post1.vote_value(@user2.id).should be_nil
|
93
93
|
|
94
|
-
@user1
|
95
|
-
@user2
|
94
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
95
|
+
Post.voted_by(@user2).to_a.should be_empty
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'user1 vote post1 has no effect' do
|
@@ -123,8 +123,8 @@ describe Mongoid::Voteable do
|
|
123
123
|
@post1.vote_value(@user1.id).should == :up
|
124
124
|
@post1.vote_value(@user2.id).should == :down
|
125
125
|
|
126
|
-
@user1
|
127
|
-
@user2
|
126
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
127
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
@@ -144,8 +144,8 @@ describe Mongoid::Voteable do
|
|
144
144
|
@post1.vote_value(@user1.id).should == :down
|
145
145
|
@post1.vote_value(@user2.id).should == :down
|
146
146
|
|
147
|
-
@user1
|
148
|
-
@user2
|
147
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
148
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
@@ -164,7 +164,7 @@ describe Mongoid::Voteable do
|
|
164
164
|
@post2.vote_value(@user1.id).should == :down
|
165
165
|
@post2.vote_value(@user2.id).should be_nil
|
166
166
|
|
167
|
-
@user1
|
167
|
+
Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
|
168
168
|
end
|
169
169
|
end
|
170
170
|
|
@@ -184,7 +184,7 @@ describe Mongoid::Voteable do
|
|
184
184
|
@post2.vote_value(@user1.id).should == :up
|
185
185
|
@post2.vote_value(@user2.id).should be_nil
|
186
186
|
|
187
|
-
@user1
|
187
|
+
Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
@@ -260,7 +260,7 @@ describe Mongoid::Voteable do
|
|
260
260
|
@post1.vote_value(@user1.id).should be_nil
|
261
261
|
@post1.vote_value(@user2.id).should == :down
|
262
262
|
|
263
|
-
@user1
|
263
|
+
Post.voted_by(@user1).to_a.should_not include(@post1)
|
264
264
|
end
|
265
265
|
end
|
266
266
|
|
@@ -13,15 +13,15 @@ describe Mongoid::Voter do
|
|
13
13
|
|
14
14
|
context "just created" do
|
15
15
|
it '' do
|
16
|
-
@user1
|
17
|
-
@user1
|
18
|
-
@user1
|
16
|
+
Post.voted_by(@user1).should be_empty
|
17
|
+
Post.up_voted_by(@user1).should be_empty
|
18
|
+
Post.down_voted_by(@user1).should be_empty
|
19
19
|
@user1.voted?(@post1).should be_false
|
20
20
|
@user1.voted?(@post2).should be_false
|
21
21
|
|
22
|
-
@user2
|
23
|
-
@user2
|
24
|
-
@user2
|
22
|
+
Post.voted_by(@user2).should be_empty
|
23
|
+
Post.up_voted_by(@user2).should be_empty
|
24
|
+
Post.down_voted_by(@user2).should be_empty
|
25
25
|
@user2.voted?(@post1).should be_false
|
26
26
|
@user2.voted?(@post2).should be_false
|
27
27
|
end
|
@@ -51,10 +51,10 @@ describe Mongoid::Voter do
|
|
51
51
|
@user1.should be_voted(@post1)
|
52
52
|
@user2.should_not be_voted(:votee_type => 'Post', :votee_id => @post1.id)
|
53
53
|
|
54
|
-
@user1
|
55
|
-
@user1
|
56
|
-
@user1
|
57
|
-
@user2
|
54
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
55
|
+
Post.up_voted_by(@user1).to_a.should == [ @post1 ]
|
56
|
+
Post.down_voted_by(@user1).to_a.should be_empty
|
57
|
+
Post.voted_by(@user2).to_a.should be_empty
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'user1 vote post1 has no effect' do
|
@@ -81,10 +81,10 @@ describe Mongoid::Voter do
|
|
81
81
|
@user1.vote_value(@post1).should == :up
|
82
82
|
@user2.vote_value(@post1).should == :down
|
83
83
|
|
84
|
-
@user1
|
85
|
-
@user2
|
86
|
-
@user2
|
87
|
-
@user2
|
84
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
85
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
86
|
+
Post.up_voted_by(@user2).to_a.should be_empty
|
87
|
+
Post.down_voted_by(@user2).to_a.should == [ @post1 ]
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -101,8 +101,8 @@ describe Mongoid::Voter do
|
|
101
101
|
@user1.vote_value(@post1).should == :down
|
102
102
|
@user2.vote_value(@post1).should == :down
|
103
103
|
|
104
|
-
@user1
|
105
|
-
@user2
|
104
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
105
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -119,7 +119,7 @@ describe Mongoid::Voter do
|
|
119
119
|
@user1.vote_value(@post2).should == :down
|
120
120
|
@user2.vote_value(@post2).should be_nil
|
121
121
|
|
122
|
-
@user1
|
122
|
+
Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
@@ -136,7 +136,7 @@ describe Mongoid::Voter do
|
|
136
136
|
@user1.vote_value(@post2).should == :up
|
137
137
|
@user2.vote_value(@post2).should be_nil
|
138
138
|
|
139
|
-
@user1
|
139
|
+
Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
|
140
140
|
end
|
141
141
|
end
|
142
142
|
end
|
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: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 4
|
10
|
+
version: 0.6.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Nguyen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-02 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|