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.
@@ -1,3 +1,3 @@
1
1
  module MongoidVote
2
- VERSION = "0.0.1.3"
2
+ VERSION = "0.0.1.4"
3
3
  end
@@ -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
- unless current.to_s == vote
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) # up vote
25
- @comment.downvote(@user) # down vote
24
+ @comment.upvote(@user) # up vote
25
+ @comment.downvote(@user) # down vote
26
26
 
27
- @comment.votecount # vote count
28
- @comment.upcount # up vote count
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.upvoters # up voters
32
- @comment.downvoters # down voters
30
+ @comment.votecount # vote count
31
+ @comment.upcount # up vote count
32
+ @comment.downcount # down vote count
33
33
 
34
- @comment.current_vote(@user) # returns :up / :down / nil
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
 
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.3
4
+ version: 0.0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: