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.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +44 -0
  3. data/.gitignore +10 -6
  4. data/.rubocop.yml +121 -0
  5. data/.ruby-version +1 -0
  6. data/Appraisals +23 -0
  7. data/Gemfile +3 -14
  8. data/{README.markdown → README.md} +87 -52
  9. data/Rakefile +6 -4
  10. data/acts_as_votable.gemspec +12 -7
  11. data/gemfiles/.bundle/config +2 -0
  12. data/gemfiles/rails_4.gemfile +7 -0
  13. data/gemfiles/rails_5.gemfile +7 -0
  14. data/gemfiles/rails_5_1.gemfile +7 -0
  15. data/gemfiles/rails_5_2.gemfile +7 -0
  16. data/gemfiles/rails_6.gemfile +8 -0
  17. data/gemfiles/rails_6_1.gemfile +8 -0
  18. data/gemfiles/rails_6_rc1.gemfile +8 -0
  19. data/lib/acts_as_votable.rb +9 -9
  20. data/lib/acts_as_votable/cacheable.rb +174 -0
  21. data/lib/acts_as_votable/extenders/controller.rb +3 -4
  22. data/lib/acts_as_votable/extenders/votable.rb +17 -6
  23. data/lib/acts_as_votable/extenders/voter.rb +4 -6
  24. data/lib/acts_as_votable/helpers/words.rb +7 -10
  25. data/lib/acts_as_votable/version.rb +3 -1
  26. data/lib/acts_as_votable/votable.rb +74 -194
  27. data/lib/acts_as_votable/vote.rb +10 -12
  28. data/lib/acts_as_votable/voter.rb +55 -56
  29. data/lib/generators/acts_as_votable/migration/migration_generator.rb +25 -4
  30. data/lib/generators/acts_as_votable/migration/templates/active_record/{migration.rb → migration.erb} +1 -6
  31. data/spec/factories/votable.rb +6 -0
  32. data/spec/factories/votable_cache.rb +6 -0
  33. data/spec/factories/votable_cache_update_attributes.rb +6 -0
  34. data/spec/factories/votable_cache_update_columns.rb +6 -0
  35. data/spec/factories/votable_child_of_sti_not_votable.rb +6 -0
  36. data/spec/factories/votable_child_of_sti_votable.rb +6 -0
  37. data/spec/factories/votable_voter.rb +6 -0
  38. data/spec/factories/vote.rb +6 -0
  39. data/spec/factories/voter.rb +6 -0
  40. data/spec/generators/active_record_generator_spec.rb +13 -0
  41. data/spec/shared_example/votable_model.rb +542 -0
  42. data/spec/shared_example/voter_model.rb +280 -0
  43. data/spec/spec_helper.rb +28 -18
  44. data/spec/support/factory_bot.rb +9 -0
  45. data/spec/votable_spec.rb +10 -9
  46. data/spec/votable_voter_spec.rb +12 -12
  47. data/spec/voter_spec.rb +9 -10
  48. data/spec/words_spec.rb +9 -12
  49. metadata +116 -26
  50. data/.travis.yml +0 -25
  51. data/spec/shared_example/votable_model_spec.rb +0 -421
  52. data/spec/shared_example/voter_model_spec.rb +0 -279
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActsAsVotable
2
- VERSION = "0.10.0"
4
+ VERSION = "0.13.0"
3
5
  end
@@ -1,39 +1,41 @@
1
- require 'acts_as_votable/helpers/words'
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 base
9
-
11
+ def self.included(base)
10
12
  # allow the user to define these himself
11
13
  aliases = {
12
14
 
13
- :vote_up => [
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
- :vote_down => [
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
- :get_up_votes => [
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
- :get_down_votes => [
29
+ get_down_votes: [
28
30
  :get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
29
31
  ],
30
- :unvote_by => [
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, :class_name => 'ActsAsVotable::Vote', :as => :votable, :dependent => :destroy do
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
- :votable_id => self.id,
60
- :votable_type => self.class.base_class.name.to_s
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 args = {}
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
- _votes_ = find_votes_for({
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 _votes_.count == 0 or options[:duplicate]
77
+ if votes.empty? || options[:duplicate]
86
78
  # this voter has never voted
87
79
  vote = ActsAsVotable::Vote.new(
88
- :votable => self,
89
- :voter => options[:voter],
90
- :vote_scope => options[:vote_scope]
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 = _votes_.last
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
- if vote.save
105
- self.vote_registered = true if last_update != vote.updated_at
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
- return false
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 args = {}
108
+ def unvote(args = {})
116
109
  return false if args[:voter].nil?
117
- _votes_ = find_votes_for(:voter_id => args[:voter].id, :vote_scope => args[:vote_scope], :voter_type => args[:voter].class.base_class.name)
110
+ votes = find_votes_by(args[:voter], args[:vote_scope])
118
111
 
119
- return true if _votes_.size == 0
120
- _votes_.each(&:destroy)
121
- update_cached_votes args[:vote_scope]
122
- self.vote_registered = false if votes_for.count == 0
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 voter, options={}
127
- self.vote_by :voter => voter, :vote => true, :vote_scope => options[:vote_scope], :vote_weight => options[:vote_weight]
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 voter, options={}
131
- self.vote_by :voter => voter, :vote => false, :vote_scope => options[:vote_scope], :vote_weight => options[:vote_weight]
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 voter, options = {}
135
- self.unvote :voter => voter, :vote_scope => options[:vote_scope] #Does not need vote_weight since the votes_for are anyway getting destroyed
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 extra_conditions = {}
134
+ def find_votes_for(extra_conditions = {})
241
135
  votes_for.where(extra_conditions)
242
136
  end
243
137
 
244
- def get_up_votes options={}
245
- find_votes_for(:vote_flag => true, :vote_scope => options[:vote_scope])
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 get_down_votes options={}
249
- find_votes_for(:vote_flag => false, :vote_scope => options[:vote_scope])
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
- # counting
254
- def count_votes_total skip_cache = false, vote_scope = nil
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
- def count_votes_up skip_cache = false, vote_scope = nil
262
- if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_up, vote_scope)
263
- return self.send(scope_cache_field :cached_votes_up, vote_scope)
264
- end
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 count_votes_down skip_cache = false, vote_scope = nil
269
- if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_down, vote_scope)
270
- return self.send(scope_cache_field :cached_votes_down, vote_scope)
271
- end
272
- get_down_votes(:vote_scope => vote_scope).count
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 weighted_total skip_cache = false, vote_scope = nil
276
- if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_total, vote_scope)
277
- return self.send(scope_cache_field :cached_weighted_total, vote_scope)
278
- end
279
- ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight)
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
- def weighted_score skip_cache = false, vote_scope = nil
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
- # voters
294
- def voted_on_by? voter
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
@@ -1,29 +1,27 @@
1
- require 'acts_as_votable/helpers/words'
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 ::ActiveRecord::VERSION::MAJOR < 4
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, :polymorphic => true
16
- belongs_to :voter, :polymorphic => true
16
+ belongs_to :votable, polymorphic: true
17
+ belongs_to :voter, polymorphic: true
17
18
 
18
- scope :up, lambda{ where(:vote_flag => true) }
19
- scope :down, lambda{ where(:vote_flag => false) }
20
- scope :for_type, lambda{ |klass| where(:votable_type => klass) }
21
- scope :by_type, lambda{ |klass| where(:voter_type => klass) }
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
- :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]
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, :class_name => 'ActsAsVotable::Vote', :as => :voter, :dependent => :destroy do
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 args
39
- args[:votable].vote_by args.merge({:voter => self})
37
+ def vote(args)
38
+ args[:votable].vote_by args.merge(voter: self)
40
39
  end
41
40
 
42
- def vote_up_for model=nil, args={}
43
- vote :votable => model, :vote_scope => args[:vote_scope], :vote => true
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 model=nil, args={}
47
- vote :votable => model, :vote_scope => args[:vote_scope], :vote => false
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 model, args={}
51
- model.unvote :voter => self, :vote_scope => args[:vote_scope]
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? votable, args={}
56
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
57
- :vote_scope => args[:vote_scope])
58
- votes.size > 0
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? votable, args={}
62
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
63
- :vote_scope => args[:vote_scope], :vote_flag => true)
64
- votes.size > 0
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? votable, args={}
68
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
69
- :vote_scope => args[:vote_scope], :vote_flag => false)
70
- votes.size > 0
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 votable, args={}
74
- vote = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
75
- :vote_scope => args[:vote_scope]).select(:vote_flag).last
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 extra_conditions = {}
79
+ def find_votes(extra_conditions = {})
81
80
  votes.where(extra_conditions)
82
81
  end
83
82
 
84
- def find_up_votes args={}
85
- find_votes :vote_flag => true, :vote_scope => args[:vote_scope]
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 args={}
89
- find_votes :vote_flag => false, :vote_scope => args[:vote_scope]
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 klass, extra_conditions = {}
93
- find_votes extra_conditions.merge({:votable_type => klass.name})
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 klass, args={}
97
- find_votes_for_class klass, :vote_flag => true, :vote_scope => args[:vote_scope]
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 klass, args={}
101
- find_votes_for_class klass, :vote_flag => false, :vote_scope => args[:vote_scope]
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 extra_conditions = {}
110
- options = extra_conditions.merge :voter_id => id, :voter_type => self.class.base_class.name
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 extra_conditions = {}
115
- find_voted_items extra_conditions.merge(:vote_flag => true)
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 extra_conditions = {}
119
- find_voted_items extra_conditions.merge(:vote_flag => false)
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 klass, extra_conditions = {}
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 klass
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 klass
129
+ def get_down_voted(klass)
131
130
  klass.joins(:votes_for).merge find_down_votes
132
131
  end
133
132
  end