mongoid_vote 0.0.1.4 → 0.0.1.5
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/lib/mongoid_vote/version.rb +1 -1
- data/lib/mongoid_vote/voteable.rb +40 -66
- data/readme.rdoc +7 -8
- metadata +2 -2
data/lib/mongoid_vote/version.rb
CHANGED
@@ -2,101 +2,75 @@ module MongoidVote
|
|
2
2
|
module Voteable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
DEFAULT_VOTES = {
|
6
|
-
"up" => [],
|
7
|
-
"down" => [],
|
8
|
-
"up_count" => 0,
|
9
|
-
"down_count" => 0,
|
10
|
-
"count" => 0,
|
11
|
-
}
|
12
|
-
|
13
5
|
included do
|
14
|
-
field :
|
6
|
+
field :up_voters, :type => Array, :default => []
|
7
|
+
field :down_voters, :type => Array, :default => []
|
8
|
+
field :up_count, :type => Integer, :default => 0
|
9
|
+
field :down_count, :type => Integer, :default => 0
|
10
|
+
field :vote_count, :type => Integer, :default => 0
|
15
11
|
end
|
16
12
|
|
17
13
|
module InstanceMethods
|
18
|
-
|
14
|
+
|
19
15
|
def upvote(user)
|
20
|
-
|
16
|
+
vote_handler(current_vote(user), :up, user)
|
21
17
|
end
|
22
18
|
|
23
19
|
def upvote_toggle(user)
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
def downvote_toggle(user)
|
28
|
-
vote_adjust(current_vote(user), 'down', user, 1)
|
20
|
+
vote_handler(current_vote(user), :up, user, 1)
|
29
21
|
end
|
30
22
|
|
31
23
|
def downvote(user)
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
def votecount
|
36
|
-
self.votes["count"]
|
37
|
-
end
|
38
|
-
|
39
|
-
def downcount
|
40
|
-
self.votes["down_count"]
|
41
|
-
end
|
42
|
-
|
43
|
-
def upcount
|
44
|
-
self.votes["up_count"]
|
45
|
-
end
|
46
|
-
|
47
|
-
def upvoters
|
48
|
-
self.votes["up"]
|
24
|
+
vote_handler(current_vote(user), :down, user)
|
49
25
|
end
|
50
26
|
|
51
|
-
def
|
52
|
-
|
27
|
+
def downvote_toggle(user)
|
28
|
+
vote_handler(current_vote(user), :down, user, 1)
|
53
29
|
end
|
54
30
|
|
55
31
|
def current_vote(user)
|
56
|
-
if self.
|
32
|
+
if self.up_voters.include? user.id.to_s
|
57
33
|
:up
|
58
|
-
elsif self.
|
34
|
+
elsif self.down_voters.include? user.id.to_s
|
59
35
|
:down
|
60
36
|
end
|
61
37
|
end
|
62
38
|
|
63
39
|
private
|
64
40
|
|
65
|
-
def
|
41
|
+
def vote_handler(current, vote, user, toggle = 0)
|
66
42
|
if current.to_s == vote
|
67
|
-
if current
|
68
|
-
|
69
|
-
self.
|
70
|
-
|
71
|
-
|
72
|
-
self.
|
73
|
-
self.votes["count"] += 1
|
74
|
-
self.votes["down"].delete_if {|x| x == user.id.to_s}
|
43
|
+
if (current && vote == :up) && toggle == 1
|
44
|
+
vote_adjust(-1, 0, -1)
|
45
|
+
self.up_voters.delete_if {|x| x == user.id.to_s}
|
46
|
+
elsif (current && vote == :down) && toggle == 1
|
47
|
+
vote_adjust(0, 1, 1)
|
48
|
+
self.down_voters.delete_if {|x| x == user.id.to_s}
|
75
49
|
end
|
76
50
|
else
|
77
|
-
if !current && vote ==
|
78
|
-
|
79
|
-
self.
|
80
|
-
|
81
|
-
|
82
|
-
self.
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
self.
|
87
|
-
|
88
|
-
|
89
|
-
self.
|
90
|
-
self.
|
91
|
-
elsif current == :down && vote == "up"
|
92
|
-
self.votes["down_count"] += 1
|
93
|
-
self.votes["up_count"] += 1
|
94
|
-
self.votes["count"] += 2
|
95
|
-
self.votes["down"].delete_if {|x| x == user.id.to_s}
|
96
|
-
self.votes["up"] << user.id.to_s
|
51
|
+
if !current && vote == :up
|
52
|
+
vote_adjust(1, 0, 1)
|
53
|
+
self.up_voters << user.id.to_s
|
54
|
+
elsif !current && vote == :down
|
55
|
+
vote_adjust(0, -1, -1)
|
56
|
+
self.down_voters << user.id.to_s
|
57
|
+
elsif current == :up && vote == :down
|
58
|
+
vote_adjust(-1, -1, -2)
|
59
|
+
self.up_voters.delete_if {|x| x == user.id.to_s}
|
60
|
+
self.down_voters << user.id.to_s
|
61
|
+
elsif current == :down && vote == :up
|
62
|
+
vote_adjust(1, 1, 2)
|
63
|
+
self.down_voters.delete_if {|x| x == user.id.to_s}
|
64
|
+
self.up_voters << user.id.to_s
|
97
65
|
end
|
98
66
|
end
|
99
67
|
end
|
68
|
+
|
69
|
+
def vote_adjust(up, down, count)
|
70
|
+
self.up_count += up
|
71
|
+
self.down_count += down
|
72
|
+
self.vote_count += count
|
73
|
+
end
|
100
74
|
end
|
101
75
|
end
|
102
76
|
end
|
data/readme.rdoc
CHANGED
@@ -20,19 +20,18 @@ Then add this to the document model you'd like to add voting capabilites to:
|
|
20
20
|
|
21
21
|
== Usage Examples
|
22
22
|
|
23
|
-
|
24
23
|
@comment.upvote(@user) # up vote
|
25
24
|
@comment.downvote(@user) # down vote
|
26
25
|
|
27
|
-
@comment.upvote_toggle(@user) # acts as upvote
|
28
|
-
@comment.downvote_toggle(@user) # acts as
|
26
|
+
@comment.upvote_toggle(@user) # acts as upvote / if up vote already present, removes vote instead. (i.e. reddit style)
|
27
|
+
@comment.downvote_toggle(@user) # acts as downvote / if down vote already present, removes vote instead. (i.e. reddit style)
|
29
28
|
|
30
|
-
@comment.
|
31
|
-
@comment.
|
32
|
-
@comment.
|
29
|
+
@comment.vote_count # vote count
|
30
|
+
@comment.up_count # up vote count
|
31
|
+
@comment.down_count # down vote count
|
33
32
|
|
34
|
-
@comment.
|
35
|
-
@comment.
|
33
|
+
@comment.up_voters # up voters
|
34
|
+
@comment.down_voters # down voters
|
36
35
|
|
37
36
|
@comment.current_vote(@user) # returns :up / :down / nil
|
38
37
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_vote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-05 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A basic Mongoid voting gem which allows voting on embedded documents.
|
15
15
|
email:
|