voteable_mongoid 0.2.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/TODO +2 -0
- data/lib/mongoid/voteable.rb +82 -0
- data/lib/mongoid/voter.rb +66 -0
- data/lib/voteable_mongoid.rb +2 -0
- metadata +132 -0
data/TODO
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Voteable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
field :up_voter_ids, :type => Array, :default => []
|
7
|
+
field :down_voter_ids, :type => Array, :default => []
|
8
|
+
|
9
|
+
field :votes_count, :type => Integer, :default => 0
|
10
|
+
field :votes_point, :type => Integer, :default => 0
|
11
|
+
|
12
|
+
index :votes_count
|
13
|
+
index :votes_point
|
14
|
+
|
15
|
+
|
16
|
+
# We usually need to show current_user his voting value on voteable object
|
17
|
+
# voting value can be nil (not voted yet), :up or :down
|
18
|
+
# from voting value, we can decide it should be new vote or revote with :up or :down
|
19
|
+
# In this case, validation can be skip to maximize performance
|
20
|
+
|
21
|
+
def self.vote(options)
|
22
|
+
votee_id = options[:votee_id]
|
23
|
+
voter_id = options[:voter_id]
|
24
|
+
value = options[:value]
|
25
|
+
|
26
|
+
votee_id = BSON::ObjectID(votee_id) if votee_id.is_a?(String)
|
27
|
+
voter_id = BSON::ObjectID(voter_id) if voter_id.is_a?(String)
|
28
|
+
|
29
|
+
if options[:revote]
|
30
|
+
if value == :up
|
31
|
+
push_field = :up_voter_ids
|
32
|
+
pull_field = :down_voter_ids
|
33
|
+
point_delta = +2
|
34
|
+
else
|
35
|
+
push_field = :down_voter_ids
|
36
|
+
pull_field = :up_voter_ids
|
37
|
+
point_delta = -2
|
38
|
+
end
|
39
|
+
|
40
|
+
collection.update({
|
41
|
+
:_id => votee_id,
|
42
|
+
push_field => { '$ne' => voter_id },
|
43
|
+
pull_field => voter_id
|
44
|
+
}, {
|
45
|
+
'$pull' => { pull_field => voter_id },
|
46
|
+
'$push' => { push_field => voter_id },
|
47
|
+
'$inc' => {
|
48
|
+
:votes_point => point_delta
|
49
|
+
}
|
50
|
+
})
|
51
|
+
|
52
|
+
else # new vote
|
53
|
+
if value == :up
|
54
|
+
push_field = :up_voter_ids
|
55
|
+
point_delta = +1
|
56
|
+
else
|
57
|
+
push_field = :down_voter_ids
|
58
|
+
point_delta = -1
|
59
|
+
end
|
60
|
+
|
61
|
+
collection.update({
|
62
|
+
:_id => votee_id,
|
63
|
+
:up_voter_ids => { '$ne' => voter_id },
|
64
|
+
:down_voter_ids => { '$ne' => voter_id },
|
65
|
+
}, {
|
66
|
+
'$push' => { push_field => voter_id },
|
67
|
+
'$inc' => {
|
68
|
+
:votes_count => +1,
|
69
|
+
:votes_point => point_delta
|
70
|
+
}
|
71
|
+
})
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def vote_value(voter_id)
|
77
|
+
return :up if up_voter_ids.try(:include?, voter_id)
|
78
|
+
return :down if down_voter_ids.try(:include?, voter_id)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Voter
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
|
6
|
+
def votees(klass)
|
7
|
+
klass.any_of({ :up_voter_ids => _id }, { :down_voter_ids => _id })
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def voted?(options)
|
12
|
+
unless options.is_a?(Hash)
|
13
|
+
votee_class = options.class
|
14
|
+
votee_id = options._id
|
15
|
+
else
|
16
|
+
votee = options[:votee]
|
17
|
+
if votee
|
18
|
+
votee_class = votee.class
|
19
|
+
votee_id = votee.id
|
20
|
+
else
|
21
|
+
votee_class = options[:votee_type].constantize
|
22
|
+
votee_id = options[:votee_id]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
votees(votee_class).where(:_id => votee_id).count == 1
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def vote_value(options)
|
31
|
+
votee = unless options.is_a?(Hash)
|
32
|
+
options
|
33
|
+
else
|
34
|
+
options[:votee_type].constantize.only(:up_vote_ids, :down_vote_ids).where(
|
35
|
+
:_id => options[:votee_id]
|
36
|
+
).first
|
37
|
+
end
|
38
|
+
votee.vote_value(_id)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def vote(options, vote_value = nil)
|
43
|
+
if options.is_a?(Hash)
|
44
|
+
votee = options[:votee]
|
45
|
+
else
|
46
|
+
votee = options
|
47
|
+
options = { :votee => votee, :value => vote_value }
|
48
|
+
end
|
49
|
+
|
50
|
+
if votee
|
51
|
+
options[:votee_id] = votee.id
|
52
|
+
votee_class = votee.class
|
53
|
+
else
|
54
|
+
votee_class = options[:votee_type].constantize
|
55
|
+
end
|
56
|
+
|
57
|
+
unless options.has_key?(:revote)
|
58
|
+
options[:revote] = voted?(options)
|
59
|
+
end
|
60
|
+
|
61
|
+
options[:voter_id] = _id
|
62
|
+
|
63
|
+
votee_class.vote(options)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voteable_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alex Nguyen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-02 00:00:00 +07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongoid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 31098209
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta
|
35
|
+
version: 2.0.0.beta
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: ruby-debug
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: bson_ext
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: rspec
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 62196427
|
75
|
+
segments:
|
76
|
+
- 2
|
77
|
+
- 0
|
78
|
+
- 0
|
79
|
+
- beta
|
80
|
+
- 20
|
81
|
+
version: 2.0.0.beta.20
|
82
|
+
type: :development
|
83
|
+
version_requirements: *id004
|
84
|
+
description: Add Up / Down Voting for Mongoid
|
85
|
+
email: alex@vinova.sg
|
86
|
+
executables: []
|
87
|
+
|
88
|
+
extensions: []
|
89
|
+
|
90
|
+
extra_rdoc_files:
|
91
|
+
- TODO
|
92
|
+
files:
|
93
|
+
- lib/mongoid/voteable.rb
|
94
|
+
- lib/mongoid/voter.rb
|
95
|
+
- lib/voteable_mongoid.rb
|
96
|
+
- TODO
|
97
|
+
has_rdoc: true
|
98
|
+
homepage: http://github.com/vinova/voteable_mongoid
|
99
|
+
licenses: []
|
100
|
+
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options:
|
103
|
+
- --charset=UTF-8
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
version: "0"
|
124
|
+
requirements: []
|
125
|
+
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.3.7
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: Add Up / Down Voting for Mongoid
|
131
|
+
test_files: []
|
132
|
+
|