voteable_mongoid 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +85 -0
- data/lib/mongoid/voteable.rb +33 -8
- data/lib/mongoid/voter.rb +20 -4
- metadata +11 -121
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
= Votable Mongoid
|
2
|
+
|
3
|
+
Votable Mongoid allows you to make your Mongoid::Document objects votable (up or down)
|
4
|
+
|
5
|
+
For instance, in a q&a site, a user can vote up (or down) on a post or a comment.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
=== Rails 3.0.x
|
10
|
+
|
11
|
+
To install the gem, add this to your Gemfile
|
12
|
+
|
13
|
+
gem 'votable_mongoid'
|
14
|
+
|
15
|
+
After that, remember to run "bundle install"
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
=== Making Post and Comment votable, User being the voter
|
20
|
+
|
21
|
+
post.rb
|
22
|
+
|
23
|
+
class Post
|
24
|
+
include Mongoid::Document
|
25
|
+
include Mongoid::Voteable
|
26
|
+
|
27
|
+
# set points for each vote
|
28
|
+
vote_point self, :up => +1, :down => -1
|
29
|
+
|
30
|
+
references_many :comments
|
31
|
+
end
|
32
|
+
|
33
|
+
comment.rb
|
34
|
+
|
35
|
+
require 'post'
|
36
|
+
|
37
|
+
class Comment
|
38
|
+
include Mongoid::Document
|
39
|
+
include Mongoid::Voteable
|
40
|
+
|
41
|
+
referenced_in :post
|
42
|
+
|
43
|
+
vote_point self, :up => +1, :down => -3
|
44
|
+
|
45
|
+
# each vote on a comment can affect votes count and point of the related post as well
|
46
|
+
vote_point Post, :up => +2, :down => -1
|
47
|
+
end
|
48
|
+
|
49
|
+
user.rb
|
50
|
+
|
51
|
+
class User
|
52
|
+
include Mongoid::Document
|
53
|
+
include Mongoid::Voter
|
54
|
+
end
|
55
|
+
|
56
|
+
=== Making a vote
|
57
|
+
|
58
|
+
@user.vote(@post, :up)
|
59
|
+
# is equivalent to
|
60
|
+
@post.vote(:voter_id => @user.id, :value => :up)
|
61
|
+
|
62
|
+
# this will affect @post.votes_count and @post.votes_point as well
|
63
|
+
@user.vote(@comment, :down)
|
64
|
+
|
65
|
+
=== Getting votes count and points
|
66
|
+
|
67
|
+
puts @post.votes_point
|
68
|
+
puts @post.votes_count
|
69
|
+
puts @post.up_votes_count
|
70
|
+
puts @post.down_votes_count
|
71
|
+
|
72
|
+
=== Getting the list of voted objects (votees) of a class
|
73
|
+
|
74
|
+
@user.votees(Post)
|
75
|
+
|
76
|
+
=== Undo a vote
|
77
|
+
|
78
|
+
@user.unvote(@comment)
|
79
|
+
|
80
|
+
== Contributors
|
81
|
+
|
82
|
+
* Alex N. - Author, Maintainer
|
83
|
+
* Stefan N. - Unvoting
|
84
|
+
|
85
|
+
Copyright (c) 2010-2011 Alex Nguyen (http://alex.vinova.sg) and Vinova Pre Ltd (http://vinova.sg)
|
data/lib/mongoid/voteable.rb
CHANGED
@@ -2,6 +2,8 @@ module Mongoid
|
|
2
2
|
module Voteable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
# How many points should be assigned for each up or down vote.
|
6
|
+
# This array should be manipulated using Votable.vote_point method
|
5
7
|
VOTE_POINT = {}
|
6
8
|
|
7
9
|
included do
|
@@ -18,7 +20,7 @@ module Mongoid
|
|
18
20
|
scope :most_voted, order_by(:votes_count.desc)
|
19
21
|
scope :best_voted, order_by(:votes_point.desc)
|
20
22
|
|
21
|
-
|
23
|
+
# Set vote point for each up (down) vote on an object of this class
|
22
24
|
def self.vote_point(klass = self, options = nil)
|
23
25
|
VOTE_POINT[self.name] ||= {}
|
24
26
|
VOTE_POINT[self.name][klass.name] ||= options
|
@@ -29,6 +31,13 @@ module Mongoid
|
|
29
31
|
# from voting value, we can decide it should be new vote or revote with :up or :down
|
30
32
|
# In this case, validation can be skip to maximize performance
|
31
33
|
|
34
|
+
# Make a vote on an object of this class
|
35
|
+
#
|
36
|
+
# @param [Hash] options a hash containings:
|
37
|
+
# - :votee_id: the votee document id
|
38
|
+
# - :voter_id: the voter document id
|
39
|
+
# - :value: :up or :down
|
40
|
+
# - :revote: change from vote up to vote down
|
32
41
|
def self.vote(options)
|
33
42
|
options.symbolize_keys!
|
34
43
|
|
@@ -102,7 +111,13 @@ module Mongoid
|
|
102
111
|
})
|
103
112
|
end
|
104
113
|
end
|
105
|
-
|
114
|
+
|
115
|
+
# Cancel a vote on an object of this class
|
116
|
+
#
|
117
|
+
# @param [Hash] options a hash containings:
|
118
|
+
# - :votee_id: the votee document id
|
119
|
+
# - :voter_id: the voter document id
|
120
|
+
# - :value: voted :up or :down
|
106
121
|
def self.unvote(options)
|
107
122
|
options.symbolize_keys!
|
108
123
|
|
@@ -146,7 +161,11 @@ module Mongoid
|
|
146
161
|
end
|
147
162
|
end
|
148
163
|
|
149
|
-
|
164
|
+
# Make a vote on this votee
|
165
|
+
#
|
166
|
+
# @param [Hash] options a hash containings:
|
167
|
+
# - :voter_id: the voter document id
|
168
|
+
# - :value: vote :up or vote :down
|
150
169
|
def vote(options)
|
151
170
|
VOTE_POINT[self.class.name].each do |class_name, value_point|
|
152
171
|
next unless relation_metadata = relations[class_name.underscore]
|
@@ -159,8 +178,12 @@ module Mongoid
|
|
159
178
|
|
160
179
|
self.class.vote(options)
|
161
180
|
end
|
162
|
-
|
163
|
-
|
181
|
+
|
182
|
+
# Cancel a vote on this votee
|
183
|
+
#
|
184
|
+
# @param [Hash] options a hash containings:
|
185
|
+
# - :voter_id: the voter document id
|
186
|
+
def unvote(options)
|
164
187
|
VOTE_POINT[self.class.name].each do |class_name, value_point|
|
165
188
|
next unless relation_metadata = relations[class_name.underscore]
|
166
189
|
next unless foreign_key = relation_metadata.foreign_key
|
@@ -172,6 +195,9 @@ module Mongoid
|
|
172
195
|
self.class.unvote(options)
|
173
196
|
end
|
174
197
|
|
198
|
+
# Get a voted value on this votee
|
199
|
+
#
|
200
|
+
# @param [String, BSON::ObjectId] x the id of the voter who made the vote
|
175
201
|
def vote_value(x)
|
176
202
|
voter_id = case x
|
177
203
|
when String
|
@@ -186,15 +212,14 @@ module Mongoid
|
|
186
212
|
return :down if down_voter_ids.try(:include?, voter_id)
|
187
213
|
end
|
188
214
|
|
189
|
-
|
215
|
+
# Get the number of up votes
|
190
216
|
def up_votes_count
|
191
217
|
up_voter_ids.length
|
192
218
|
end
|
193
219
|
|
194
|
-
|
220
|
+
# Get the number of down votes
|
195
221
|
def down_votes_count
|
196
222
|
down_voter_ids.length
|
197
223
|
end
|
198
|
-
|
199
224
|
end
|
200
225
|
end
|
data/lib/mongoid/voter.rb
CHANGED
@@ -2,12 +2,18 @@ module Mongoid
|
|
2
2
|
module Voter
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
|
5
|
+
# Get list of voted votees
|
6
|
+
#
|
7
|
+
# @param [Class] klass the votee class, e.g. `Post` or `Comment`
|
8
|
+
# @return [Array, nil] an array of votable objects voted by this voter
|
6
9
|
def votees(klass)
|
7
10
|
klass.any_of({ :up_voter_ids => _id }, { :down_voter_ids => _id })
|
8
11
|
end
|
9
12
|
|
10
|
-
|
13
|
+
# Check to see if this voter voted on the votee or not
|
14
|
+
#
|
15
|
+
# @param [Hash, Object] options the hash containing the votee, or the votee itself
|
16
|
+
# @return [true, false] true if voted, false otherwise
|
11
17
|
def voted?(options)
|
12
18
|
unless options.is_a?(Hash)
|
13
19
|
votee_class = options.class
|
@@ -25,11 +31,18 @@ module Mongoid
|
|
25
31
|
|
26
32
|
votees(votee_class).where(:_id => votee_id).count == 1
|
27
33
|
end
|
28
|
-
|
34
|
+
|
35
|
+
# Cancel the vote on a votee
|
36
|
+
#
|
37
|
+
# @param [Object] votee the votee to be unvoted
|
29
38
|
def unvote(votee)
|
30
39
|
votee.unvote(:voter_id => _id)
|
31
40
|
end
|
32
41
|
|
42
|
+
# Get the voted value on a votee
|
43
|
+
#
|
44
|
+
# @param (see #voted?)
|
45
|
+
# @return [Symbol, nil] :up or :down or nil if not voted
|
33
46
|
def vote_value(options)
|
34
47
|
votee = unless options.is_a?(Hash)
|
35
48
|
options
|
@@ -41,7 +54,10 @@ module Mongoid
|
|
41
54
|
votee.vote_value(_id)
|
42
55
|
end
|
43
56
|
|
44
|
-
|
57
|
+
# Vote on a votee
|
58
|
+
#
|
59
|
+
# @param (see #voted?)
|
60
|
+
# @param [:up, :down] vote_value vote up or vote down
|
45
61
|
def vote(options, vote_value = nil)
|
46
62
|
options.symbolize_keys!
|
47
63
|
|
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: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Nguyen
|
@@ -88,121 +88,9 @@ dependencies:
|
|
88
88
|
prerelease: false
|
89
89
|
type: :development
|
90
90
|
requirement: *id005
|
91
|
-
- !ruby/object:Gem::Dependency
|
92
|
-
name: bson_ext
|
93
|
-
version_requirements: &id006 !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
|
-
requirements:
|
96
|
-
- - ">="
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
hash: 3
|
99
|
-
segments:
|
100
|
-
- 0
|
101
|
-
version: "0"
|
102
|
-
prerelease: false
|
103
|
-
type: :development
|
104
|
-
requirement: *id006
|
105
|
-
- !ruby/object:Gem::Dependency
|
106
|
-
name: rspec
|
107
|
-
version_requirements: &id007 !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
|
-
requirements:
|
110
|
-
- - ">="
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
hash: 3
|
113
|
-
segments:
|
114
|
-
- 0
|
115
|
-
version: "0"
|
116
|
-
prerelease: false
|
117
|
-
type: :development
|
118
|
-
requirement: *id007
|
119
|
-
- !ruby/object:Gem::Dependency
|
120
|
-
name: bson_ext
|
121
|
-
version_requirements: &id008 !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
|
-
requirements:
|
124
|
-
- - ">="
|
125
|
-
- !ruby/object:Gem::Version
|
126
|
-
hash: 3
|
127
|
-
segments:
|
128
|
-
- 0
|
129
|
-
version: "0"
|
130
|
-
prerelease: false
|
131
|
-
type: :development
|
132
|
-
requirement: *id008
|
133
|
-
- !ruby/object:Gem::Dependency
|
134
|
-
name: rspec
|
135
|
-
version_requirements: &id009 !ruby/object:Gem::Requirement
|
136
|
-
none: false
|
137
|
-
requirements:
|
138
|
-
- - ">="
|
139
|
-
- !ruby/object:Gem::Version
|
140
|
-
hash: 3
|
141
|
-
segments:
|
142
|
-
- 0
|
143
|
-
version: "0"
|
144
|
-
prerelease: false
|
145
|
-
type: :development
|
146
|
-
requirement: *id009
|
147
|
-
- !ruby/object:Gem::Dependency
|
148
|
-
name: bson_ext
|
149
|
-
version_requirements: &id010 !ruby/object:Gem::Requirement
|
150
|
-
none: false
|
151
|
-
requirements:
|
152
|
-
- - ">="
|
153
|
-
- !ruby/object:Gem::Version
|
154
|
-
hash: 3
|
155
|
-
segments:
|
156
|
-
- 0
|
157
|
-
version: "0"
|
158
|
-
prerelease: false
|
159
|
-
type: :development
|
160
|
-
requirement: *id010
|
161
|
-
- !ruby/object:Gem::Dependency
|
162
|
-
name: rspec
|
163
|
-
version_requirements: &id011 !ruby/object:Gem::Requirement
|
164
|
-
none: false
|
165
|
-
requirements:
|
166
|
-
- - ">="
|
167
|
-
- !ruby/object:Gem::Version
|
168
|
-
hash: 3
|
169
|
-
segments:
|
170
|
-
- 0
|
171
|
-
version: "0"
|
172
|
-
prerelease: false
|
173
|
-
type: :development
|
174
|
-
requirement: *id011
|
175
|
-
- !ruby/object:Gem::Dependency
|
176
|
-
name: bson_ext
|
177
|
-
version_requirements: &id012 !ruby/object:Gem::Requirement
|
178
|
-
none: false
|
179
|
-
requirements:
|
180
|
-
- - ">="
|
181
|
-
- !ruby/object:Gem::Version
|
182
|
-
hash: 3
|
183
|
-
segments:
|
184
|
-
- 0
|
185
|
-
version: "0"
|
186
|
-
prerelease: false
|
187
|
-
type: :development
|
188
|
-
requirement: *id012
|
189
|
-
- !ruby/object:Gem::Dependency
|
190
|
-
name: rspec
|
191
|
-
version_requirements: &id013 !ruby/object:Gem::Requirement
|
192
|
-
none: false
|
193
|
-
requirements:
|
194
|
-
- - ">="
|
195
|
-
- !ruby/object:Gem::Version
|
196
|
-
hash: 3
|
197
|
-
segments:
|
198
|
-
- 0
|
199
|
-
version: "0"
|
200
|
-
prerelease: false
|
201
|
-
type: :development
|
202
|
-
requirement: *id013
|
203
91
|
- !ruby/object:Gem::Dependency
|
204
92
|
name: mongoid
|
205
|
-
version_requirements: &
|
93
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
206
94
|
none: false
|
207
95
|
requirements:
|
208
96
|
- - ~>
|
@@ -216,10 +104,10 @@ dependencies:
|
|
216
104
|
version: 2.0.0.rc
|
217
105
|
prerelease: false
|
218
106
|
type: :runtime
|
219
|
-
requirement: *
|
107
|
+
requirement: *id006
|
220
108
|
- !ruby/object:Gem::Dependency
|
221
109
|
name: bson_ext
|
222
|
-
version_requirements: &
|
110
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
223
111
|
none: false
|
224
112
|
requirements:
|
225
113
|
- - ">="
|
@@ -230,10 +118,10 @@ dependencies:
|
|
230
118
|
version: "0"
|
231
119
|
prerelease: false
|
232
120
|
type: :development
|
233
|
-
requirement: *
|
121
|
+
requirement: *id007
|
234
122
|
- !ruby/object:Gem::Dependency
|
235
123
|
name: rspec
|
236
|
-
version_requirements: &
|
124
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
237
125
|
none: false
|
238
126
|
requirements:
|
239
127
|
- - ">="
|
@@ -244,7 +132,7 @@ dependencies:
|
|
244
132
|
version: "0"
|
245
133
|
prerelease: false
|
246
134
|
type: :development
|
247
|
-
requirement: *
|
135
|
+
requirement: *id008
|
248
136
|
description: Add Up / Down Voting for Mongoid
|
249
137
|
email: alex@vinova.sg
|
250
138
|
executables: []
|
@@ -252,11 +140,13 @@ executables: []
|
|
252
140
|
extensions: []
|
253
141
|
|
254
142
|
extra_rdoc_files:
|
143
|
+
- README.rdoc
|
255
144
|
- TODO
|
256
145
|
files:
|
257
146
|
- lib/mongoid/voteable.rb
|
258
147
|
- lib/mongoid/voter.rb
|
259
148
|
- lib/voteable_mongoid.rb
|
149
|
+
- README.rdoc
|
260
150
|
- TODO
|
261
151
|
has_rdoc: true
|
262
152
|
homepage: http://github.com/vinova/voteable_mongoid
|