/
voter.rb
180 lines (151 loc) · 4.64 KB
/
voter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module MakeVoteable
module Voter
extend ActiveSupport::Concern
included do
has_many :votings, :class_name => "MakeVoteable::Voting", :as => :voter
end
module ClassMethods
def voter?
true
end
end
# Up vote a +voteable+.
# Raises an AlreadyVotedError if the voter already up voted the voteable.
# Changes a down vote to an up vote if the the voter already down voted the voteable.
def up_vote(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
if voting
if voting.up_vote
raise Exceptions::AlreadyVotedError.new(true)
else
voting.up_vote = true
voteable.down_votes -= 1
self.down_votes -= 1 if has_attribute?(:down_votes)
end
else
voting = Voting.create(:voteable => voteable, :voter_id => self.id, :voter_type => self.class.to_s, :up_vote => true)
end
voteable.up_votes += 1
self.up_votes += 1 if has_attribute?(:up_votes)
Voting.transaction do
save
voteable.save
voting.save
end
true
end
# Up votes the +voteable+, but doesn't raise an error if the votelable was already up voted.
# The vote is simply ignored then.
def up_vote!(voteable)
begin
up_vote(voteable)
success = true
rescue Exceptions::AlreadyVotedError
success = false
end
success
end
# Down vote a +voteable+.
# Raises an AlreadyVotedError if the voter already down voted the voteable.
# Changes an up vote to a down vote if the the voter already up voted the voteable.
def down_vote(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
if voting
unless voting.up_vote
raise Exceptions::AlreadyVotedError.new(false)
else
voting.up_vote = false
voteable.up_votes -= 1
self.up_votes -= 1 if has_attribute?(:up_votes)
end
else
voting = Voting.create(:voteable => voteable, :voter_id => self.id, :voter_type => self.class.to_s, :up_vote => false)
end
voteable.down_votes += 1
self.down_votes += 1 if has_attribute?(:down_votes)
Voting.transaction do
save
voteable.save
voting.save
end
true
end
# Down votes the +voteable+, but doesn't raise an error if the votelable was already down voted.
# The vote is simply ignored then.
def down_vote!(voteable)
begin
down_vote(voteable)
success = true
rescue Exceptions::AlreadyVotedError
success = false
end
success
end
# Clears an already done vote on a +voteable+.
# Raises a NotVotedError if the voter didn't voted for the voteable.
def unvote(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
raise Exceptions::NotVotedError unless voting
if voting.up_vote
voteable.up_votes -= 1
self.up_votes -= 1 if has_attribute?(:up_votes)
else
voteable.down_votes -= 1
self.down_votes -= 1 if has_attribute?(:down_votes)
end
Voting.transaction do
save
voteable.save
voting.destroy
end
true
end
# Clears an already done vote on a +voteable+, but doesn't raise an error if
# the voteable was not voted. It ignores the unvote then.
def unvote!(voteable)
begin
unvote(voteable)
success = true
rescue Exceptions::NotVotedError
success = false
end
success
end
# Returns true if the voter voted for the +voteable+.
def voted?(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
!voting.nil?
end
# Returns true if the voter up voted the +voteable+.
def up_voted?(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
return false if voting.nil?
return true if voting.has_attribute?(:up_vote) && voting.up_vote
false
end
# Returns true if the voter down voted the +voteable+.
def down_voted?(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
return false if voting.nil?
return true if voting.has_attribute?(:up_vote) && !voting.up_vote
false
end
private
def fetch_voting(voteable)
Voting.where(
:voteable_type => voteable.class.to_s,
:voteable_id => voteable.id,
:voter_type => self.class.to_s,
:voter_id => self.id).try(:first)
end
def check_voteable(voteable)
raise Exceptions::InvalidVoteableError unless voteable.class.voteable?
end
end
end