voteable_mongoid 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid/voteable.rb +55 -3
- data/lib/mongoid/voter.rb +4 -1
- metadata +39 -11
data/lib/mongoid/voteable.rb
CHANGED
@@ -22,8 +22,7 @@ module Mongoid
|
|
22
22
|
def self.vote_point(klass = self, options = nil)
|
23
23
|
VOTE_POINT[self.name] ||= {}
|
24
24
|
VOTE_POINT[self.name][klass.name] ||= options
|
25
|
-
end
|
26
|
-
|
25
|
+
end
|
27
26
|
|
28
27
|
# We usually need to show current_user his voting value on voteable object
|
29
28
|
# voting value can be nil (not voted yet), :up or :down
|
@@ -103,6 +102,48 @@ module Mongoid
|
|
103
102
|
})
|
104
103
|
end
|
105
104
|
end
|
105
|
+
|
106
|
+
def self.unvote(options)
|
107
|
+
options.symbolize_keys!
|
108
|
+
|
109
|
+
voter_id = options[:voter_id]
|
110
|
+
voter_id = BSON::ObjectId(voter_id) if voter_id.is_a?(String)
|
111
|
+
votee_id = options[:votee_id]
|
112
|
+
votee_id = BSON::ObjectId(votee_id) if votee_id.is_a?(String)
|
113
|
+
value = options[:value].to_sym
|
114
|
+
return unless value # not voted yet!
|
115
|
+
|
116
|
+
value_point = VOTE_POINT[self.name][self.name]
|
117
|
+
|
118
|
+
pull_field = if value == :up
|
119
|
+
:up_voter_ids
|
120
|
+
else
|
121
|
+
:down_voter_ids
|
122
|
+
end
|
123
|
+
|
124
|
+
collection.update({
|
125
|
+
:_id => votee_id,
|
126
|
+
}, {
|
127
|
+
'$pull' => { pull_field => voter_id },
|
128
|
+
'$inc' => {
|
129
|
+
:votes_count => -1,
|
130
|
+
:votes_point => -value_point[value]
|
131
|
+
}
|
132
|
+
})
|
133
|
+
|
134
|
+
VOTE_POINT[self.name].each do |class_name, value_point|
|
135
|
+
next unless relation_metadata = relations[class_name.underscore]
|
136
|
+
next unless foreign_key_value = options[relation_metadata.foreign_key.to_sym]
|
137
|
+
foreign_key_value = BSON::ObjectId(foreign_key_value) if foreign_key_value.is_a?(String)
|
138
|
+
|
139
|
+
class_name.constantize.collection.update({ :_id => foreign_key_value }, {
|
140
|
+
'$inc' => {
|
141
|
+
:votes_count => value_point[:not_increase_votes_count] ? 0 : -1,
|
142
|
+
:votes_point => -value_point[value]
|
143
|
+
}
|
144
|
+
})
|
145
|
+
end
|
146
|
+
end
|
106
147
|
end
|
107
148
|
|
108
149
|
|
@@ -118,7 +159,18 @@ module Mongoid
|
|
118
159
|
|
119
160
|
self.class.vote(options)
|
120
161
|
end
|
121
|
-
|
162
|
+
|
163
|
+
def unvote(options)
|
164
|
+
VOTE_POINT[self.class.name].each do |class_name, value_point|
|
165
|
+
next unless relation_metadata = relations[class_name.underscore]
|
166
|
+
next unless foreign_key = relation_metadata.foreign_key
|
167
|
+
options[foreign_key.to_sym] = read_attribute(foreign_key)
|
168
|
+
end
|
169
|
+
|
170
|
+
options[:votee_id] ||= _id
|
171
|
+
options[:value] = vote_value(options[:voter_id])
|
172
|
+
self.class.unvote(options)
|
173
|
+
end
|
122
174
|
|
123
175
|
def vote_value(x)
|
124
176
|
voter_id = case x
|
data/lib/mongoid/voter.rb
CHANGED
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
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
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-02-
|
18
|
+
date: 2011-02-23 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -173,8 +173,36 @@ dependencies:
|
|
173
173
|
type: :development
|
174
174
|
requirement: *id011
|
175
175
|
- !ruby/object:Gem::Dependency
|
176
|
-
name:
|
176
|
+
name: bson_ext
|
177
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
|
+
- !ruby/object:Gem::Dependency
|
204
|
+
name: mongoid
|
205
|
+
version_requirements: &id014 !ruby/object:Gem::Requirement
|
178
206
|
none: false
|
179
207
|
requirements:
|
180
208
|
- - ~>
|
@@ -188,10 +216,10 @@ dependencies:
|
|
188
216
|
version: 2.0.0.rc
|
189
217
|
prerelease: false
|
190
218
|
type: :runtime
|
191
|
-
requirement: *
|
219
|
+
requirement: *id014
|
192
220
|
- !ruby/object:Gem::Dependency
|
193
221
|
name: bson_ext
|
194
|
-
version_requirements: &
|
222
|
+
version_requirements: &id015 !ruby/object:Gem::Requirement
|
195
223
|
none: false
|
196
224
|
requirements:
|
197
225
|
- - ">="
|
@@ -202,10 +230,10 @@ dependencies:
|
|
202
230
|
version: "0"
|
203
231
|
prerelease: false
|
204
232
|
type: :development
|
205
|
-
requirement: *
|
233
|
+
requirement: *id015
|
206
234
|
- !ruby/object:Gem::Dependency
|
207
235
|
name: rspec
|
208
|
-
version_requirements: &
|
236
|
+
version_requirements: &id016 !ruby/object:Gem::Requirement
|
209
237
|
none: false
|
210
238
|
requirements:
|
211
239
|
- - ">="
|
@@ -216,7 +244,7 @@ dependencies:
|
|
216
244
|
version: "0"
|
217
245
|
prerelease: false
|
218
246
|
type: :development
|
219
|
-
requirement: *
|
247
|
+
requirement: *id016
|
220
248
|
description: Add Up / Down Voting for Mongoid
|
221
249
|
email: alex@vinova.sg
|
222
250
|
executables: []
|