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.
@@ -1,3 +1,3 @@
1
1
  module MongoidVote
2
- VERSION = "0.0.1.4"
2
+ VERSION = "0.0.1.5"
3
3
  end
@@ -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 :votes, :type => Hash, :default => DEFAULT_VOTES
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
- vote_adjust(current_vote(user), 'up', user)
16
+ vote_handler(current_vote(user), :up, user)
21
17
  end
22
18
 
23
19
  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)
20
+ vote_handler(current_vote(user), :up, user, 1)
29
21
  end
30
22
 
31
23
  def downvote(user)
32
- vote_adjust(current_vote(user), 'down', user)
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 downvoters
52
- self.votes["down"]
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.votes["up"].include? user.id.to_s
32
+ if self.up_voters.include? user.id.to_s
57
33
  :up
58
- elsif self.votes["down"].include? user.id.to_s
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 vote_adjust(current, vote, user, toggle = 0)
41
+ def vote_handler(current, vote, user, toggle = 0)
66
42
  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}
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 == "up"
78
- self.votes["up_count"] += 1
79
- self.votes["count"] += 1
80
- self.votes["up"] << user.id.to_s
81
- elsif !current && vote == "down"
82
- self.votes["down_count"] -= 1
83
- self.votes["count"] -= 1
84
- self.votes["down"] << user.id.to_s
85
- elsif current == :up && vote == "down"
86
- self.votes["up_count"] -= 1
87
- self.votes["down_count"] -= 1
88
- self.votes["count"] -= 2
89
- self.votes["up"].delete_if {|x| x == user.id.to_s}
90
- self.votes["down"] << user.id.to_s
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
@@ -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 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)
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.votecount # vote count
31
- @comment.upcount # up vote count
32
- @comment.downcount # down vote count
29
+ @comment.vote_count # vote count
30
+ @comment.up_count # up vote count
31
+ @comment.down_count # down vote count
33
32
 
34
- @comment.upvoters # up voters
35
- @comment.downvoters # down voters
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
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-04 00:00:00.000000000 Z
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: