mongoid_vote 0.0.1.3 → 0.0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid_vote/version.rb +1 -1
- data/lib/mongoid_vote/voteable.rb +20 -2
- data/readme.rdoc +11 -8
- metadata +1 -1
data/lib/mongoid_vote/version.rb
CHANGED
@@ -20,6 +20,14 @@ module MongoidVote
|
|
20
20
|
vote_adjust(current_vote(user), 'up', user)
|
21
21
|
end
|
22
22
|
|
23
|
+
def upvote_toggle(user)
|
24
|
+
vote_adjust(current_vote(user), 'up', user, 1)
|
25
|
+
end
|
26
|
+
|
27
|
+
def downvote_toggle(user)
|
28
|
+
vote_adjust(current_vote(user), 'down', user, 1)
|
29
|
+
end
|
30
|
+
|
23
31
|
def downvote(user)
|
24
32
|
vote_adjust(current_vote(user), 'down', user)
|
25
33
|
end
|
@@ -54,8 +62,18 @@ module MongoidVote
|
|
54
62
|
|
55
63
|
private
|
56
64
|
|
57
|
-
def vote_adjust(current, vote, user)
|
58
|
-
|
65
|
+
def vote_adjust(current, vote, user, toggle = 0)
|
66
|
+
if current.to_s == vote
|
67
|
+
if current == :up && vote == "up" && toggle == 1
|
68
|
+
self.votes["up_count"] -= 1
|
69
|
+
self.votes["count"] -= 1
|
70
|
+
self.votes["up"].delete_if {|x| x == user.id.to_s}
|
71
|
+
elsif current == :down && vote == "down" && toggle == 1
|
72
|
+
self.votes["down_count"] += 1
|
73
|
+
self.votes["count"] += 1
|
74
|
+
self.votes["down"].delete_if {|x| x == user.id.to_s}
|
75
|
+
end
|
76
|
+
else
|
59
77
|
if !current && vote == "up"
|
60
78
|
self.votes["up_count"] += 1
|
61
79
|
self.votes["count"] += 1
|
data/readme.rdoc
CHANGED
@@ -21,17 +21,20 @@ Then add this to the document model you'd like to add voting capabilites to:
|
|
21
21
|
== Usage Examples
|
22
22
|
|
23
23
|
|
24
|
-
@comment.upvote(@user)
|
25
|
-
@comment.downvote(@user)
|
24
|
+
@comment.upvote(@user) # up vote
|
25
|
+
@comment.downvote(@user) # down vote
|
26
26
|
|
27
|
-
@comment.
|
28
|
-
@comment.
|
29
|
-
@comment.downcount # down vote count
|
27
|
+
@comment.upvote_toggle(@user) # acts as upvote but if up vote is already present, removes vote instead. (i.e. reddit style)
|
28
|
+
@comment.downvote_toggle(@user) # acts as upvote but if up vote is already present, removes vote instead. (i.e. reddit style)
|
30
29
|
|
31
|
-
@comment.
|
32
|
-
@comment.
|
30
|
+
@comment.votecount # vote count
|
31
|
+
@comment.upcount # up vote count
|
32
|
+
@comment.downcount # down vote count
|
33
33
|
|
34
|
-
@comment.
|
34
|
+
@comment.upvoters # up voters
|
35
|
+
@comment.downvoters # down voters
|
36
|
+
|
37
|
+
@comment.current_vote(@user) # returns :up / :down / nil
|
35
38
|
|
36
39
|
== Notes
|
37
40
|
|