acts_as_votable 0.10.0 → 0.13.0
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.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +44 -0
- data/.gitignore +10 -6
- data/.rubocop.yml +121 -0
- data/.ruby-version +1 -0
- data/Appraisals +23 -0
- data/Gemfile +3 -14
- data/{README.markdown → README.md} +87 -52
- data/Rakefile +6 -4
- data/acts_as_votable.gemspec +12 -7
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/rails_4.gemfile +7 -0
- data/gemfiles/rails_5.gemfile +7 -0
- data/gemfiles/rails_5_1.gemfile +7 -0
- data/gemfiles/rails_5_2.gemfile +7 -0
- data/gemfiles/rails_6.gemfile +8 -0
- data/gemfiles/rails_6_1.gemfile +8 -0
- data/gemfiles/rails_6_rc1.gemfile +8 -0
- data/lib/acts_as_votable.rb +9 -9
- data/lib/acts_as_votable/cacheable.rb +174 -0
- data/lib/acts_as_votable/extenders/controller.rb +3 -4
- data/lib/acts_as_votable/extenders/votable.rb +17 -6
- data/lib/acts_as_votable/extenders/voter.rb +4 -6
- data/lib/acts_as_votable/helpers/words.rb +7 -10
- data/lib/acts_as_votable/version.rb +3 -1
- data/lib/acts_as_votable/votable.rb +74 -194
- data/lib/acts_as_votable/vote.rb +10 -12
- data/lib/acts_as_votable/voter.rb +55 -56
- data/lib/generators/acts_as_votable/migration/migration_generator.rb +25 -4
- data/lib/generators/acts_as_votable/migration/templates/active_record/{migration.rb → migration.erb} +1 -6
- data/spec/factories/votable.rb +6 -0
- data/spec/factories/votable_cache.rb +6 -0
- data/spec/factories/votable_cache_update_attributes.rb +6 -0
- data/spec/factories/votable_cache_update_columns.rb +6 -0
- data/spec/factories/votable_child_of_sti_not_votable.rb +6 -0
- data/spec/factories/votable_child_of_sti_votable.rb +6 -0
- data/spec/factories/votable_voter.rb +6 -0
- data/spec/factories/vote.rb +6 -0
- data/spec/factories/voter.rb +6 -0
- data/spec/generators/active_record_generator_spec.rb +13 -0
- data/spec/shared_example/votable_model.rb +542 -0
- data/spec/shared_example/voter_model.rb +280 -0
- data/spec/spec_helper.rb +28 -18
- data/spec/support/factory_bot.rb +9 -0
- data/spec/votable_spec.rb +10 -9
- data/spec/votable_voter_spec.rb +12 -12
- data/spec/voter_spec.rb +9 -10
- data/spec/words_spec.rb +9 -12
- metadata +116 -26
- data/.travis.yml +0 -25
- data/spec/shared_example/votable_model_spec.rb +0 -421
- data/spec/shared_example/voter_model_spec.rb +0 -279
@@ -1,39 +1,41 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "acts_as_votable/helpers/words"
|
4
|
+
require "acts_as_votable/cacheable"
|
2
5
|
|
3
6
|
module ActsAsVotable
|
4
7
|
module Votable
|
5
|
-
|
6
8
|
include Helpers::Words
|
9
|
+
include Cacheable
|
7
10
|
|
8
|
-
def self.included
|
9
|
-
|
11
|
+
def self.included(base)
|
10
12
|
# allow the user to define these himself
|
11
13
|
aliases = {
|
12
14
|
|
13
|
-
:
|
14
|
-
:up_by, :upvote_by, :like_by, :liked_by,
|
15
|
+
vote_up: [
|
16
|
+
:up_by, :upvote_by, :like_by, :liked_by,
|
15
17
|
:up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from
|
16
18
|
],
|
17
19
|
|
18
|
-
:
|
20
|
+
vote_down: [
|
19
21
|
:down_by, :downvote_by, :dislike_by, :disliked_by,
|
20
22
|
:down_from, :downvote_from, :downvote_by, :dislike_by, :disliked_by
|
21
23
|
],
|
22
24
|
|
23
|
-
:
|
25
|
+
get_up_votes: [
|
24
26
|
:get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes,
|
25
27
|
],
|
26
28
|
|
27
|
-
:
|
29
|
+
get_down_votes: [
|
28
30
|
:get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
|
29
31
|
],
|
30
|
-
:
|
32
|
+
unvote_by: [
|
31
33
|
:unvote_up, :unvote_down, :unliked_by, :undisliked_by
|
32
34
|
]
|
33
35
|
}
|
34
36
|
|
35
37
|
base.class_eval do
|
36
|
-
has_many :votes_for, :
|
38
|
+
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :destroy do
|
37
39
|
def voters
|
38
40
|
includes(:voter).map(&:voter)
|
39
41
|
end
|
@@ -56,42 +58,32 @@ module ActsAsVotable
|
|
56
58
|
|
57
59
|
def default_conditions
|
58
60
|
{
|
59
|
-
:
|
60
|
-
:
|
61
|
+
votable_id: self.id,
|
62
|
+
votable_type: self.class.base_class.name.to_s
|
61
63
|
}
|
62
64
|
end
|
63
65
|
|
64
66
|
# voting
|
65
|
-
def vote_by
|
67
|
+
def vote_by(args = {})
|
68
|
+
return false if args[:voter].nil?
|
66
69
|
|
67
|
-
options = {
|
68
|
-
:vote => true,
|
69
|
-
:vote_scope => nil
|
70
|
-
}.merge(args)
|
70
|
+
options = { vote: true, vote_scope: nil }.merge(args)
|
71
71
|
|
72
72
|
self.vote_registered = false
|
73
73
|
|
74
|
-
if options[:voter].nil?
|
75
|
-
return false
|
76
|
-
end
|
77
|
-
|
78
74
|
# find the vote
|
79
|
-
|
80
|
-
:voter_id => options[:voter].id,
|
81
|
-
:vote_scope => options[:vote_scope],
|
82
|
-
:voter_type => options[:voter].class.base_class.name
|
83
|
-
})
|
75
|
+
votes = find_votes_by(options[:voter], options[:vote_scope])
|
84
76
|
|
85
|
-
if
|
77
|
+
if votes.empty? || options[:duplicate]
|
86
78
|
# this voter has never voted
|
87
79
|
vote = ActsAsVotable::Vote.new(
|
88
|
-
:
|
89
|
-
:
|
90
|
-
:
|
80
|
+
votable: self,
|
81
|
+
voter: options[:voter],
|
82
|
+
vote_scope: options[:vote_scope]
|
91
83
|
)
|
92
84
|
else
|
93
85
|
# this voter is potentially changing his vote
|
94
|
-
vote =
|
86
|
+
vote = votes.last
|
95
87
|
end
|
96
88
|
|
97
89
|
last_update = vote.updated_at
|
@@ -101,200 +93,88 @@ module ActsAsVotable
|
|
101
93
|
#Allowing for a vote_weight to be associated with every vote. Could change with every voter object
|
102
94
|
vote.vote_weight = (options[:vote_weight].to_i if options[:vote_weight].present?) || 1
|
103
95
|
|
104
|
-
|
105
|
-
|
106
|
-
update_cached_votes options[:vote_scope]
|
107
|
-
return true
|
108
|
-
else
|
96
|
+
vote_saved = false
|
97
|
+
ActiveRecord::Base.transaction do
|
109
98
|
self.vote_registered = false
|
110
|
-
|
99
|
+
vote_saved = vote.save
|
100
|
+
if vote_saved
|
101
|
+
self.vote_registered = true if last_update != vote.updated_at
|
102
|
+
update_cached_votes(options[:vote_scope])
|
103
|
+
end
|
111
104
|
end
|
112
|
-
|
105
|
+
vote_saved
|
113
106
|
end
|
114
107
|
|
115
|
-
def unvote
|
108
|
+
def unvote(args = {})
|
116
109
|
return false if args[:voter].nil?
|
117
|
-
|
110
|
+
votes = find_votes_by(args[:voter], args[:vote_scope])
|
118
111
|
|
119
|
-
return true if
|
120
|
-
|
121
|
-
|
122
|
-
|
112
|
+
return true if votes.empty?
|
113
|
+
ActiveRecord::Base.transaction do
|
114
|
+
votes.each(&:destroy)
|
115
|
+
update_cached_votes args[:vote_scope]
|
116
|
+
end
|
117
|
+
self.vote_registered = false if votes_for.empty?
|
123
118
|
return true
|
124
119
|
end
|
125
120
|
|
126
|
-
def vote_up
|
127
|
-
self.vote_by :
|
121
|
+
def vote_up(voter, options = {})
|
122
|
+
self.vote_by voter: voter, vote: true, vote_scope: options[:vote_scope], vote_weight: options[:vote_weight]
|
128
123
|
end
|
129
124
|
|
130
|
-
def vote_down
|
131
|
-
self.vote_by :
|
125
|
+
def vote_down(voter, options = {})
|
126
|
+
self.vote_by voter: voter, vote: false, vote_scope: options[:vote_scope], vote_weight: options[:vote_weight]
|
132
127
|
end
|
133
128
|
|
134
|
-
def unvote_by
|
135
|
-
self.unvote :
|
129
|
+
def unvote_by(voter, options = {})
|
130
|
+
self.unvote voter: voter, vote_scope: options[:vote_scope] #Does not need vote_weight since the votes_for are anyway getting destroyed
|
136
131
|
end
|
137
132
|
|
138
|
-
def scope_cache_field field, vote_scope
|
139
|
-
return field if vote_scope.nil?
|
140
|
-
|
141
|
-
case field
|
142
|
-
when :cached_votes_total=
|
143
|
-
"cached_scoped_#{vote_scope}_votes_total="
|
144
|
-
when :cached_votes_total
|
145
|
-
"cached_scoped_#{vote_scope}_votes_total"
|
146
|
-
when :cached_votes_up=
|
147
|
-
"cached_scoped_#{vote_scope}_votes_up="
|
148
|
-
when :cached_votes_up
|
149
|
-
"cached_scoped_#{vote_scope}_votes_up"
|
150
|
-
when :cached_votes_down=
|
151
|
-
"cached_scoped_#{vote_scope}_votes_down="
|
152
|
-
when :cached_votes_down
|
153
|
-
"cached_scoped_#{vote_scope}_votes_down"
|
154
|
-
when :cached_votes_score=
|
155
|
-
"cached_scoped_#{vote_scope}_votes_score="
|
156
|
-
when :cached_votes_score
|
157
|
-
"cached_scoped_#{vote_scope}_votes_score"
|
158
|
-
when :cached_weighted_total
|
159
|
-
"cached_weighted_#{vote_scope}_total"
|
160
|
-
when :cached_weighted_total=
|
161
|
-
"cached_weighted_#{vote_scope}_total="
|
162
|
-
when :cached_weighted_score
|
163
|
-
"cached_weighted_#{vote_scope}_score"
|
164
|
-
when :cached_weighted_score=
|
165
|
-
"cached_weighted_#{vote_scope}_score="
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
# caching
|
170
|
-
def update_cached_votes vote_scope = nil
|
171
|
-
|
172
|
-
updates = {}
|
173
|
-
|
174
|
-
if self.respond_to?(:cached_votes_total=)
|
175
|
-
updates[:cached_votes_total] = count_votes_total(true)
|
176
|
-
end
|
177
|
-
|
178
|
-
if self.respond_to?(:cached_votes_up=)
|
179
|
-
updates[:cached_votes_up] = count_votes_up(true)
|
180
|
-
end
|
181
|
-
|
182
|
-
if self.respond_to?(:cached_votes_down=)
|
183
|
-
updates[:cached_votes_down] = count_votes_down(true)
|
184
|
-
end
|
185
|
-
|
186
|
-
if self.respond_to?(:cached_votes_score=)
|
187
|
-
updates[:cached_votes_score] = (
|
188
|
-
(updates[:cached_votes_up] || count_votes_up(true)) -
|
189
|
-
(updates[:cached_votes_down] || count_votes_down(true))
|
190
|
-
)
|
191
|
-
end
|
192
|
-
|
193
|
-
if self.respond_to?(:cached_weighted_total=)
|
194
|
-
updates[:cached_weighted_total] = weighted_total(true)
|
195
|
-
end
|
196
|
-
|
197
|
-
if self.respond_to?(:cached_weighted_score=)
|
198
|
-
updates[:cached_weighted_score] = weighted_score(true)
|
199
|
-
end
|
200
|
-
|
201
|
-
if vote_scope
|
202
|
-
if self.respond_to?(scope_cache_field :cached_votes_total=, vote_scope)
|
203
|
-
updates[scope_cache_field :cached_votes_total, vote_scope] = count_votes_total(true, vote_scope)
|
204
|
-
end
|
205
|
-
|
206
|
-
if self.respond_to?(scope_cache_field :cached_votes_up=, vote_scope)
|
207
|
-
updates[scope_cache_field :cached_votes_up, vote_scope] = count_votes_up(true, vote_scope)
|
208
|
-
end
|
209
|
-
|
210
|
-
if self.respond_to?(scope_cache_field :cached_votes_down=, vote_scope)
|
211
|
-
updates[scope_cache_field :cached_votes_down, vote_scope] = count_votes_down(true, vote_scope)
|
212
|
-
end
|
213
|
-
|
214
|
-
if self.respond_to?(scope_cache_field :cached_weighted_total=, vote_scope)
|
215
|
-
updates[scope_cache_field :cached_weighted_total, vote_scope] = weighted_total(true, vote_scope)
|
216
|
-
end
|
217
|
-
|
218
|
-
if self.respond_to?(scope_cache_field :cached_weighted_score=, vote_scope)
|
219
|
-
updates[scope_cache_field :cached_weighted_score, vote_scope] = weighted_score(true, vote_scope)
|
220
|
-
end
|
221
|
-
|
222
|
-
if self.respond_to?(scope_cache_field :cached_votes_score=, vote_scope)
|
223
|
-
updates[scope_cache_field :cached_votes_score, vote_scope] = (
|
224
|
-
(updates[scope_cache_field :cached_votes_up, vote_scope] || count_votes_up(true, vote_scope)) -
|
225
|
-
(updates[scope_cache_field :cached_votes_down, vote_scope] || count_votes_down(true, vote_scope))
|
226
|
-
)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
if (::ActiveRecord::VERSION::MAJOR == 3) && (::ActiveRecord::VERSION::MINOR != 0)
|
231
|
-
self.update_attributes(updates, :without_protection => true) if updates.size > 0
|
232
|
-
else
|
233
|
-
self.update_attributes(updates) if updates.size > 0
|
234
|
-
end
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
133
|
# results
|
240
|
-
def find_votes_for
|
134
|
+
def find_votes_for(extra_conditions = {})
|
241
135
|
votes_for.where(extra_conditions)
|
242
136
|
end
|
243
137
|
|
244
|
-
def
|
245
|
-
find_votes_for(:
|
138
|
+
def find_votes_by(voter, vote_scope)
|
139
|
+
find_votes_for(voter_id: voter.id,
|
140
|
+
vote_scope: vote_scope,
|
141
|
+
voter_type: voter.class.base_class.name)
|
246
142
|
end
|
247
143
|
|
248
|
-
def
|
249
|
-
|
144
|
+
def get_up_votes(options = {})
|
145
|
+
vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
|
146
|
+
find_votes_for({ vote_flag: true }.merge(vote_scope_hash))
|
250
147
|
end
|
251
148
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_total, vote_scope)
|
256
|
-
return self.send(scope_cache_field :cached_votes_total, vote_scope)
|
257
|
-
end
|
258
|
-
find_votes_for(:vote_scope => vote_scope).count
|
149
|
+
def get_down_votes(options = {})
|
150
|
+
vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
|
151
|
+
find_votes_for({ vote_flag: false }.merge(vote_scope_hash))
|
259
152
|
end
|
260
153
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
get_up_votes(:vote_scope => vote_scope).count
|
154
|
+
# voters
|
155
|
+
def voted_on_by?(voter)
|
156
|
+
votes = find_votes_for voter_id: voter.id, voter_type: voter.class.base_class.name
|
157
|
+
votes.exists?
|
266
158
|
end
|
267
159
|
|
268
|
-
def
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
160
|
+
def voted_up_by?(voter)
|
161
|
+
votes = find_votes_for(voter_id: voter.id,
|
162
|
+
vote_flag: true,
|
163
|
+
voter_type: voter.class.base_class.name)
|
164
|
+
votes.exists?
|
273
165
|
end
|
274
166
|
|
275
|
-
def
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight)
|
281
|
-
ups + downs
|
167
|
+
def voted_down_by?(voter)
|
168
|
+
votes = find_votes_for(voter_id: voter.id,
|
169
|
+
vote_flag: false,
|
170
|
+
voter_type: voter.class.base_class.name)
|
171
|
+
votes.exists?
|
282
172
|
end
|
283
173
|
|
284
|
-
|
285
|
-
if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_score, vote_scope)
|
286
|
-
return self.send(scope_cache_field :cached_weighted_score, vote_scope)
|
287
|
-
end
|
288
|
-
ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight)
|
289
|
-
downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight)
|
290
|
-
ups - downs
|
291
|
-
end
|
174
|
+
private
|
292
175
|
|
293
|
-
|
294
|
-
|
295
|
-
votes = find_votes_for :voter_id => voter.id, :voter_type => voter.class.base_class.name
|
296
|
-
votes.count > 0
|
176
|
+
def scope_or_empty_hash(vote_scope)
|
177
|
+
vote_scope ? { vote_scope: vote_scope } : {}
|
297
178
|
end
|
298
|
-
|
299
179
|
end
|
300
180
|
end
|
data/lib/acts_as_votable/vote.rb
CHANGED
@@ -1,29 +1,27 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "acts_as_votable/helpers/words"
|
2
4
|
|
3
5
|
module ActsAsVotable
|
4
6
|
class Vote < ::ActiveRecord::Base
|
5
|
-
|
6
7
|
include Helpers::Words
|
7
8
|
|
8
|
-
if
|
9
|
+
if defined?(ProtectedAttributes)
|
9
10
|
attr_accessible :votable_id, :votable_type,
|
10
11
|
:voter_id, :voter_type,
|
11
12
|
:votable, :voter,
|
12
13
|
:vote_flag, :vote_scope
|
13
14
|
end
|
14
15
|
|
15
|
-
belongs_to :votable, :
|
16
|
-
belongs_to :voter, :
|
16
|
+
belongs_to :votable, polymorphic: true
|
17
|
+
belongs_to :voter, polymorphic: true
|
17
18
|
|
18
|
-
scope :up,
|
19
|
-
scope :down,
|
20
|
-
scope :for_type,
|
21
|
-
scope :by_type,
|
19
|
+
scope :up, -> { where(vote_flag: true) }
|
20
|
+
scope :down, -> { where(vote_flag: false) }
|
21
|
+
scope :for_type, ->(klass) { where(votable_type: klass.to_s) }
|
22
|
+
scope :by_type, ->(klass) { where(voter_type: klass.to_s) }
|
22
23
|
|
23
24
|
validates_presence_of :votable_id
|
24
25
|
validates_presence_of :voter_id
|
25
|
-
|
26
26
|
end
|
27
|
-
|
28
27
|
end
|
29
|
-
|
@@ -1,24 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActsAsVotable
|
2
4
|
module Voter
|
3
|
-
|
4
5
|
def self.included(base)
|
5
|
-
|
6
6
|
# allow user to define these
|
7
7
|
aliases = {
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
8
|
+
vote_up_for: [:likes, :upvotes, :up_votes],
|
9
|
+
vote_down_for: [:dislikes, :downvotes, :down_votes],
|
10
|
+
unvote_for: [:unlike, :undislike],
|
11
|
+
voted_on?: [:voted_for?],
|
12
|
+
voted_up_on?: [:voted_up_for?, :liked?],
|
13
|
+
voted_down_on?: [:voted_down_for?, :disliked?],
|
14
|
+
voted_as_when_voting_on: [:voted_as_when_voted_on, :voted_as_when_voting_for, :voted_as_when_voted_for],
|
15
|
+
find_up_voted_items: [:find_liked_items],
|
16
|
+
find_down_voted_items: [:find_disliked_items]
|
17
17
|
}
|
18
18
|
|
19
19
|
base.class_eval do
|
20
20
|
|
21
|
-
has_many :votes, :
|
21
|
+
has_many :votes, class_name: "ActsAsVotable::Vote", as: :voter, dependent: :destroy do
|
22
22
|
def votables
|
23
23
|
includes(:votable).map(&:votable)
|
24
24
|
end
|
@@ -31,74 +31,73 @@ module ActsAsVotable
|
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
36
35
|
|
37
36
|
# voting
|
38
|
-
def vote
|
39
|
-
args[:votable].vote_by args.merge(
|
37
|
+
def vote(args)
|
38
|
+
args[:votable].vote_by args.merge(voter: self)
|
40
39
|
end
|
41
40
|
|
42
|
-
def vote_up_for
|
43
|
-
vote :
|
41
|
+
def vote_up_for(model = nil, args = {})
|
42
|
+
vote votable: model, vote_scope: args[:vote_scope], vote: true
|
44
43
|
end
|
45
44
|
|
46
|
-
def vote_down_for
|
47
|
-
vote :
|
45
|
+
def vote_down_for(model = nil, args = {})
|
46
|
+
vote votable: model, vote_scope: args[:vote_scope], vote: false
|
48
47
|
end
|
49
48
|
|
50
|
-
def unvote_for
|
51
|
-
model.unvote :
|
49
|
+
def unvote_for(model, args = {})
|
50
|
+
model.unvote voter: self, vote_scope: args[:vote_scope]
|
52
51
|
end
|
53
52
|
|
54
53
|
# results
|
55
|
-
def voted_on?
|
56
|
-
votes = find_votes(:
|
57
|
-
:
|
58
|
-
votes.
|
54
|
+
def voted_on?(votable, args = {})
|
55
|
+
votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
|
56
|
+
vote_scope: args[:vote_scope])
|
57
|
+
votes.exists?
|
59
58
|
end
|
60
59
|
|
61
|
-
def voted_up_on?
|
62
|
-
votes = find_votes(:
|
63
|
-
:
|
64
|
-
votes.
|
60
|
+
def voted_up_on?(votable, args = {})
|
61
|
+
votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
|
62
|
+
vote_scope: args[:vote_scope], vote_flag: true)
|
63
|
+
votes.exists?
|
65
64
|
end
|
66
65
|
|
67
|
-
def voted_down_on?
|
68
|
-
votes = find_votes(:
|
69
|
-
:
|
70
|
-
votes.
|
66
|
+
def voted_down_on?(votable, args = {})
|
67
|
+
votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
|
68
|
+
vote_scope: args[:vote_scope], vote_flag: false)
|
69
|
+
votes.exists?
|
71
70
|
end
|
72
71
|
|
73
|
-
def voted_as_when_voting_on
|
74
|
-
vote = find_votes(:
|
75
|
-
:
|
72
|
+
def voted_as_when_voting_on(votable, args = {})
|
73
|
+
vote = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
|
74
|
+
vote_scope: args[:vote_scope]).select(:vote_flag).last
|
76
75
|
return nil unless vote
|
77
76
|
return vote.vote_flag
|
78
77
|
end
|
79
78
|
|
80
|
-
def find_votes
|
79
|
+
def find_votes(extra_conditions = {})
|
81
80
|
votes.where(extra_conditions)
|
82
81
|
end
|
83
82
|
|
84
|
-
def find_up_votes
|
85
|
-
find_votes :
|
83
|
+
def find_up_votes(args = {})
|
84
|
+
find_votes vote_flag: true, vote_scope: args[:vote_scope]
|
86
85
|
end
|
87
86
|
|
88
|
-
def find_down_votes
|
89
|
-
find_votes :
|
87
|
+
def find_down_votes(args = {})
|
88
|
+
find_votes vote_flag: false, vote_scope: args[:vote_scope]
|
90
89
|
end
|
91
90
|
|
92
|
-
def find_votes_for_class
|
93
|
-
find_votes extra_conditions.merge(
|
91
|
+
def find_votes_for_class(klass, extra_conditions = {})
|
92
|
+
find_votes extra_conditions.merge(votable_type: klass.name)
|
94
93
|
end
|
95
94
|
|
96
|
-
def find_up_votes_for_class
|
97
|
-
find_votes_for_class klass, :
|
95
|
+
def find_up_votes_for_class(klass, args = {})
|
96
|
+
find_votes_for_class klass, vote_flag: true, vote_scope: args[:vote_scope]
|
98
97
|
end
|
99
98
|
|
100
|
-
def find_down_votes_for_class
|
101
|
-
find_votes_for_class klass, :
|
99
|
+
def find_down_votes_for_class(klass, args = {})
|
100
|
+
find_votes_for_class klass, vote_flag: false, vote_scope: args[:vote_scope]
|
102
101
|
end
|
103
102
|
|
104
103
|
# Including polymporphic relations for eager loading
|
@@ -106,28 +105,28 @@ module ActsAsVotable
|
|
106
105
|
ActsAsVotable::Vote.includes(:votable)
|
107
106
|
end
|
108
107
|
|
109
|
-
def find_voted_items
|
110
|
-
options = extra_conditions.merge :
|
108
|
+
def find_voted_items(extra_conditions = {})
|
109
|
+
options = extra_conditions.merge voter_id: id, voter_type: self.class.base_class.name
|
111
110
|
include_objects.where(options).collect(&:votable)
|
112
111
|
end
|
113
112
|
|
114
|
-
def find_up_voted_items
|
115
|
-
find_voted_items extra_conditions.merge(:
|
113
|
+
def find_up_voted_items(extra_conditions = {})
|
114
|
+
find_voted_items extra_conditions.merge(vote_flag: true)
|
116
115
|
end
|
117
116
|
|
118
|
-
def find_down_voted_items
|
119
|
-
find_voted_items extra_conditions.merge(:
|
117
|
+
def find_down_voted_items(extra_conditions = {})
|
118
|
+
find_voted_items extra_conditions.merge(vote_flag: false)
|
120
119
|
end
|
121
120
|
|
122
|
-
def get_voted
|
121
|
+
def get_voted(klass, extra_conditions = {})
|
123
122
|
klass.joins(:votes_for).merge find_votes(extra_conditions)
|
124
123
|
end
|
125
124
|
|
126
|
-
def get_up_voted
|
125
|
+
def get_up_voted(klass)
|
127
126
|
klass.joins(:votes_for).merge find_up_votes
|
128
127
|
end
|
129
128
|
|
130
|
-
def get_down_voted
|
129
|
+
def get_down_voted(klass)
|
131
130
|
klass.joins(:votes_for).merge find_down_votes
|
132
131
|
end
|
133
132
|
end
|