/
votable.rb
178 lines (142 loc) · 4.94 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
# frozen_string_literal: true
require "acts_as_votable/helpers/words"
module ActsAsVotable
module Votable
include Helpers::Words
include Cacheable
def self.included(base)
# allow the user to define these himself
aliases = {
vote_up: [
:up_by, :upvote_by, :like_by, :liked_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
],
get_up_votes: [
:get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes,
],
get_down_votes: [
:get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
],
unvote_by: [
:unvote_up, :unvote_down, :unliked_by, :undisliked_by
]
}
base.class_eval do
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :delete_all do
def voters
includes(:voter).map(&:voter)
end
end
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_by(args = {})
return false if args[:voter].nil?
options = { vote: true, vote_scope: nil }.merge(args)
self.vote_registered = false
# find the vote
votes = find_votes_by(options[:voter], options[:vote_scope])
if options[:duplicate] || !votes.exists?
# this voter has never voted
vote = ActsAsVotable::Vote.new(
votable: self,
voter: options[:voter],
vote_scope: options[:vote_scope]
)
else
# this voter is potentially changing his vote
vote = votes.last
end
last_update = vote.updated_at
vote.vote_flag = votable_words.meaning_of(options[:vote])
#Allowing for a vote_weight to be associated with every vote. Could change with every voter object
vote.vote_weight = (options[:vote_weight].to_i if options[:vote_weight].present?) || 1
vote_saved = false
ActiveRecord::Base.transaction do
self.vote_registered = false
vote_saved = vote.save
if vote_saved
self.vote_registered = true if last_update != vote.updated_at
update_cached_votes(options[:vote_scope])
end
end
vote_saved
end
def unvote(args = {})
return false if args[:voter].nil?
votes = find_votes_by(args[:voter], args[:vote_scope])
ActiveRecord::Base.transaction do
deleted_count = votes.delete_all
update_cached_votes(args[:vote_scope]) if deleted_count > 0
end
self.vote_registered = false
return true
end
def vote_up(voter, options = {})
self.vote_by voter: voter, vote: true, vote_scope: options[:vote_scope], vote_weight: options[:vote_weight]
end
def vote_down(voter, options = {})
self.vote_by voter: voter, vote: false, vote_scope: options[:vote_scope], vote_weight: options[:vote_weight]
end
def unvote_by(voter, options = {})
self.unvote voter: voter, vote_scope: options[:vote_scope] #Does not need vote_weight since the votes_for are anyway getting destroyed
end
# results
def find_votes_for(extra_conditions = {})
votes_for.where(extra_conditions)
end
def find_votes_by(voter, vote_scope)
find_votes_for(voter_id: voter.id,
vote_scope: vote_scope,
voter_type: voter.class.base_class.name)
end
def get_up_votes(options = {})
vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
find_votes_for({ vote_flag: true }.merge(vote_scope_hash))
end
def get_down_votes(options = {})
vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
find_votes_for({ vote_flag: false }.merge(vote_scope_hash))
end
# voters
def voted_on_by?(voter)
votes = find_votes_for voter_id: voter.id, voter_type: voter.class.base_class.name
votes.exists?
end
def voted_up_by?(voter)
votes = find_votes_for(voter_id: voter.id,
vote_flag: true,
voter_type: voter.class.base_class.name)
votes.exists?
end
def voted_down_by?(voter)
votes = find_votes_for(voter_id: voter.id,
vote_flag: false,
voter_type: voter.class.base_class.name)
votes.exists?
end
private
def scope_or_empty_hash(vote_scope)
vote_scope ? { vote_scope: vote_scope } : {}
end
end
end