voteable_mongoid 0.7.1 → 0.7.2
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
CHANGED
@@ -14,28 +14,43 @@ module Mongoid
|
|
14
14
|
# - :unvote: unvote the vote value (:up or :down)
|
15
15
|
def vote(options)
|
16
16
|
options.symbolize_keys!
|
17
|
+
options[:votee_id] ||= options[:votee].id
|
17
18
|
options[:votee_id] = BSON::ObjectId(options[:votee_id]) if options[:votee_id].is_a?(String)
|
18
19
|
options[:voter_id] = BSON::ObjectId(options[:voter_id]) if options[:voter_id].is_a?(String)
|
19
20
|
options[:value] = options[:value].to_sym
|
20
21
|
options[:voteable] = VOTEABLE[name][name]
|
21
22
|
|
22
|
-
update_parents =
|
23
|
+
update_parents = options[:voteable][:update_parents]
|
23
24
|
|
24
25
|
if options[:voteable]
|
25
|
-
|
26
|
+
query, update = if options[:revote]
|
26
27
|
revote(options)
|
27
28
|
elsif options[:unvote]
|
28
29
|
unvote(options)
|
29
30
|
else
|
30
31
|
new_vote(options)
|
31
32
|
end
|
32
|
-
# Only update parent votes if votee is updated successfully
|
33
|
-
update_parents = ( update_result['err'] == nil and
|
34
|
-
update_result['updatedExisting'] == true and
|
35
|
-
update_result['n'] == 1 )
|
36
|
-
end
|
37
33
|
|
38
|
-
|
34
|
+
if options[:votee] || update_parents
|
35
|
+
# If votee exits or need to update parent
|
36
|
+
# use Collection#find_and_modify to retrieve updated votes data and parent_ids
|
37
|
+
begin
|
38
|
+
doc = collection.master.collection.find_and_modify(
|
39
|
+
:query => query,
|
40
|
+
:update => update,
|
41
|
+
:new => true
|
42
|
+
)
|
43
|
+
# Update new votes data
|
44
|
+
options[:votee].write_attribute('votes', doc['votes']) if options[:votee]
|
45
|
+
update_parent_votes(doc, options) if update_parents
|
46
|
+
rescue
|
47
|
+
# Don't update parents if operation fail or no matching object found
|
48
|
+
end
|
49
|
+
else
|
50
|
+
# Just update and don't care the result
|
51
|
+
collection.update(query, update)
|
52
|
+
end
|
53
|
+
end
|
39
54
|
end
|
40
55
|
|
41
56
|
|
@@ -49,7 +64,7 @@ module Mongoid
|
|
49
64
|
positive_votes_count = 'votes.down_count'
|
50
65
|
end
|
51
66
|
|
52
|
-
|
67
|
+
return {
|
53
68
|
# Validate voter_id did not vote for votee_id yet
|
54
69
|
:_id => options[:votee_id],
|
55
70
|
'votes.up' => { '$ne' => options[:voter_id] },
|
@@ -61,9 +76,7 @@ module Mongoid
|
|
61
76
|
'votes.count' => +1,
|
62
77
|
positive_votes_count => +1,
|
63
78
|
'votes.point' => options[:voteable][options[:value]] }
|
64
|
-
}
|
65
|
-
:safe => true
|
66
|
-
})
|
79
|
+
}
|
67
80
|
end
|
68
81
|
|
69
82
|
|
@@ -82,7 +95,7 @@ module Mongoid
|
|
82
95
|
point_delta = -options[:voteable][:up] + options[:voteable][:down]
|
83
96
|
end
|
84
97
|
|
85
|
-
|
98
|
+
return {
|
86
99
|
# Validate voter_id did a vote with value for votee_id
|
87
100
|
:_id => options[:votee_id],
|
88
101
|
positive_voter_ids => { '$ne' => options[:voter_id] },
|
@@ -96,9 +109,7 @@ module Mongoid
|
|
96
109
|
negative_votes_count => -1,
|
97
110
|
'votes.point' => point_delta
|
98
111
|
}
|
99
|
-
}
|
100
|
-
:safe => true
|
101
|
-
})
|
112
|
+
}
|
102
113
|
end
|
103
114
|
|
104
115
|
|
@@ -113,10 +124,9 @@ module Mongoid
|
|
113
124
|
positive_votes_count = 'votes.down_count'
|
114
125
|
end
|
115
126
|
|
116
|
-
|
117
|
-
update_result = collection.update({
|
118
|
-
# Validate voter_id did a vote with value for votee_id
|
127
|
+
return {
|
119
128
|
:_id => options[:votee_id],
|
129
|
+
# Validate if voter_id did a vote with value for votee_id
|
120
130
|
negative_voter_ids => { '$ne' => options[:voter_id] },
|
121
131
|
positive_voter_ids => options[:voter_id]
|
122
132
|
}, {
|
@@ -127,22 +137,19 @@ module Mongoid
|
|
127
137
|
'votes.count' => -1,
|
128
138
|
'votes.point' => -options[:voteable][options[:value]]
|
129
139
|
}
|
130
|
-
}
|
131
|
-
:safe => true
|
132
|
-
})
|
140
|
+
}
|
133
141
|
end
|
134
142
|
|
135
143
|
|
136
|
-
def update_parent_votes(options)
|
144
|
+
def update_parent_votes(doc, options)
|
137
145
|
value = options[:value]
|
138
146
|
votee ||= options[:votee]
|
139
147
|
|
140
148
|
VOTEABLE[name].each do |class_name, voteable|
|
141
149
|
# For other class in VOTEABLE options, if is parent of current class
|
142
150
|
next unless relation_metadata = relations[class_name.underscore]
|
143
|
-
votee ||= find(options[:votee_id])
|
144
151
|
# If can find current votee foreign_key value for that class
|
145
|
-
next unless foreign_key_value =
|
152
|
+
next unless foreign_key_value = doc[relation_metadata.foreign_key.to_s]
|
146
153
|
|
147
154
|
class_name.constantize.collection.update(
|
148
155
|
{ '_id' => foreign_key_value },
|
@@ -44,8 +44,11 @@ module Mongoid
|
|
44
44
|
# voteable self, :up => +1, :down => -3
|
45
45
|
# voteable Post, :up => +2, :down => -1, :update_counters => false # skip counter update
|
46
46
|
def voteable(klass = self, options = nil)
|
47
|
-
VOTEABLE[
|
48
|
-
VOTEABLE[
|
47
|
+
VOTEABLE[name] ||= {}
|
48
|
+
VOTEABLE[name][klass.name] ||= options
|
49
|
+
if klass != self
|
50
|
+
VOTEABLE[name][name][:update_parents] = true
|
51
|
+
end
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|
@@ -76,8 +76,7 @@ describe Mongoid::Voteable do
|
|
76
76
|
|
77
77
|
context 'user1 vote up post1 the first time' do
|
78
78
|
before :all do
|
79
|
-
Post.vote(:
|
80
|
-
@post1.reload
|
79
|
+
Post.vote(:votee => @post1, :voter_id => @user1.id, :value => :up)
|
81
80
|
end
|
82
81
|
|
83
82
|
it '' do
|
@@ -151,8 +150,7 @@ describe Mongoid::Voteable do
|
|
151
150
|
|
152
151
|
context 'user1 vote down post2 the first time' do
|
153
152
|
before :all do
|
154
|
-
|
155
|
-
@post2.reload
|
153
|
+
@post2.vote(:voter_id => @user1.id, :value => :down)
|
156
154
|
end
|
157
155
|
|
158
156
|
it '' do
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voteable_mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 1
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
- 1
|
10
|
-
version: 0.7.1
|
5
|
+
version: 0.7.2
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Alex Nguyen
|
@@ -15,7 +10,8 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-04 00:00:00 +08:00
|
14
|
+
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
21
17
|
name: mongoid
|
@@ -25,11 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ~>
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
hash: 15
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 0
|
32
|
-
- 0
|
33
24
|
version: 2.0.0
|
34
25
|
type: :runtime
|
35
26
|
version_requirements: *id001
|
@@ -41,9 +32,6 @@ dependencies:
|
|
41
32
|
requirements:
|
42
33
|
- - ">="
|
43
34
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 3
|
45
|
-
segments:
|
46
|
-
- 0
|
47
35
|
version: "0"
|
48
36
|
type: :development
|
49
37
|
version_requirements: *id002
|
@@ -81,6 +69,7 @@ files:
|
|
81
69
|
- spec/voteable_mongoid/voteable_spec.rb
|
82
70
|
- spec/voteable_mongoid/voter_spec.rb
|
83
71
|
- voteable_mongoid.gemspec
|
72
|
+
has_rdoc: true
|
84
73
|
homepage: https://github.com/vinova/voteable_mongoid
|
85
74
|
licenses: []
|
86
75
|
|
@@ -94,23 +83,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
83
|
requirements:
|
95
84
|
- - ">="
|
96
85
|
- !ruby/object:Gem::Version
|
97
|
-
hash: 3
|
98
|
-
segments:
|
99
|
-
- 0
|
100
86
|
version: "0"
|
101
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
88
|
none: false
|
103
89
|
requirements:
|
104
90
|
- - ">="
|
105
91
|
- !ruby/object:Gem::Version
|
106
|
-
hash: 3
|
107
|
-
segments:
|
108
|
-
- 0
|
109
92
|
version: "0"
|
110
93
|
requirements: []
|
111
94
|
|
112
95
|
rubyforge_project: voteable_mongoid
|
113
|
-
rubygems_version: 1.
|
96
|
+
rubygems_version: 1.6.2
|
114
97
|
signing_key:
|
115
98
|
specification_version: 3
|
116
99
|
summary: Add Up / Down Voting for Mongoid
|