forked from jbgutierrez/acts_as_votable
/
votable.rb
191 lines (145 loc) · 4.24 KB
/
votable.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
181
182
183
184
185
186
187
188
189
190
191
require 'acts_as_votable/helpers/words'
module ActsAsVotable
module Votable
include Helpers::Words
def self.included base
# allow the user to define these himself
aliases = {
:vote_up => [
:up_by, :upvote_by, :like_by, :liked_by, :vote_by,
:up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from
],
:vote_down => [
:down_by, :downvote_by, :dislike_by, :disliked_by,
:down_from, :downvote_from, :downvote_by, :dislike_by, :disliked_by
],
:up_votes => [
:true_votes, :ups, :upvotes, :likes, :positives, :for_votes,
],
:down_votes => [
:false_votes, :downs, :downvotes, :dislikes, :negatives
],
:unvote => [
:unliked_by, :undisliked_by
]
}
base.class_eval do
belongs_to :votable, :polymorphic => true
aliases.each do |method, links|
links.each do |new_method|
alias_method(new_method, method)
end
end
end
end
attr_accessor :vote_registered
def vote_registered?
return self.vote_registered
end
def default_conditions
{
:votable_id => self.id,
:votable_type => self.class.base_class.name.to_s
}
end
# voting
def vote args = {}
options = {
:vote => true,
}.merge(args)
self.vote_registered = false
if options[:voter].nil?
return false
end
# find the vote
votes = find_votes({
:voter_id => options[:voter].id,
:voter_type => options[:voter].class.name
})
if votes.count == 0
# this voter has never voted
vote = ActsAsVotable::Vote.new(
:votable => self,
:voter => options[:voter]
)
else
# this voter is potentially changing his vote
vote = votes.first
end
last_update = vote.updated_at
vote.vote_flag = votable_words.meaning_of(options[:vote])
if vote.save
self.vote_registered = true if last_update != vote.updated_at
update_cached_votes
return true
else
self.vote_registered = false
return false
end
end
def unvote args = {}
return false if args[:voter].nil?
_votes_ = find_votes(:voter_id => args[:voter].id, :voter_type => args[:voter].class.name)
return true if _votes_.size == 0
_votes_.each(&:destroy)
update_cached_votes
self.vote_registered = false if votes.count == 0
return true
end
def vote_up voter
self.vote :voter => voter, :vote => true
end
def vote_down voter
self.vote :voter => voter, :vote => false
end
# caching
def update_cached_votes
updates = {}
if self.respond_to?(:cached_votes_total=)
updates[:cached_votes_total] = count_votes_total(true)
end
if self.respond_to?(:cached_votes_up=)
updates[:cached_votes_up] = count_votes_up(true)
end
if self.respond_to?(:cached_votes_down=)
updates[:cached_votes_down] = count_votes_down(true)
end
self.update_attributes(updates) if updates.size > 0
end
# results
def find_votes extra_conditions = {}
ActsAsVotable::Vote.find(:all, :conditions => default_conditions.merge(extra_conditions))
end
alias :votes :find_votes
def up_votes
find_votes(:vote_flag => true)
end
def down_votes
find_votes(:vote_flag => false)
end
# counting
def count_votes_total skip_cache = false
if !skip_cache && self.respond_to?(:cached_votes_total)
return self.send(:cached_votes_total)
end
find_votes.count
end
def count_votes_up skip_cache = false
if !skip_cache && self.respond_to?(:cached_votes_up)
return self.send(:cached_votes_up)
end
up_votes.count
end
def count_votes_down skip_cache = false
if !skip_cache && self.respond_to?(:cached_votes_down)
return self.send(:cached_votes_down)
end
down_votes.count
end
# voters
def voted_on_by? voter
votes = find_votes :voter_id => voter.id, :voter_type => voter.class.name
votes.count > 0
end
end
end