blob: 40b3a2ab49b0b2ccfd0a6a5f23c4f54f9747f8e6 (
plain) (
blame)
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
|
module Votable
extend ActiveSupport::Concern
included do
has_many :votes, as: :votable
def already_upvoted?(ip)
!votes.where(ip: ip, upvote: 1).empty?
end
def already_downvoted?(ip)
!votes.where(ip: ip, upvote: 0).empty?
end
def upvote!(ip)
return false if already_upvoted?(ip)
if already_downvoted?(ip)
votes.where(ip: ip, upvote: 0).first.delete
self.downvotes -= 1
save!
else
votes.create(ip: ip, upvote: 1).save
self.upvotes += 1
save!
end
end
def downvote!(ip)
return false if already_downvoted?(ip)
if already_upvoted?(ip)
votes.where(ip: ip, upvote: 1).first.delete
self.upvotes -= 1
save!
else
votes.create(ip: ip, upvote: 0).save
self.downvotes += 1
save!
end
end
def like!(url, name)
return false unless votes.where(liker_url: url).empty?
votes.create(liker_url: url, liker_name: name, upvote: 1).save
self.upvotes += 1
save!
end
end
end
|