acts_as_votable 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b8402806ab189425b71b1d24d2081123e6bffc1
4
- data.tar.gz: 48de253feb1e0df3ef42cd798faaaf8f91f36b47
3
+ metadata.gz: c2beddd83fe6f3062fdfb1599953b34d942f6ac8
4
+ data.tar.gz: 8bfb3b0bb95722935fb3eb315da58c619f583921
5
5
  SHA512:
6
- metadata.gz: 7189eeb6505d6394bb090d58618d23ee13e36e6512f0416a15bf984c489e0775952680b748cadc054bd4948260184ac4a457ef6dee3b34f279561245adb2213e
7
- data.tar.gz: 0fd78166628e72e3c77b8b5c8b8ba0028aa3069afb217816324c470ce6cc0c5a809431fcf47a800e440bbcb49020778e54f6f082698321357ded582081f488b0
6
+ metadata.gz: cbe9cba192df3d698531c14a10f8340b78642cd51288b582b064384ea48931345900c74a499598b541ca2662230d262be6acc0f26d2db9a3763539140ec08683
7
+ data.tar.gz: 03948b37befab6d39cd621b63d6e32cb2404b4850f2d5b2e2bece1674bcbaa13bcc4aff7e023f30aeff3330c809fdf9eb28b7d28d5ae6d6685c287c5167a90c1
@@ -25,7 +25,7 @@ The main goals of this gem are:
25
25
  Just add the following to your Gemfile.
26
26
 
27
27
  ```ruby
28
- gem 'acts_as_votable', '~> 0.9.0'
28
+ gem 'acts_as_votable', '~> 0.10.0'
29
29
  ```
30
30
 
31
31
  And follow that up with a ``bundle install``.
@@ -316,11 +316,13 @@ class AddCachedVotesToPosts < ActiveRecord::Migration
316
316
  add_column :posts, :cached_votes_up, :integer, :default => 0
317
317
  add_column :posts, :cached_votes_down, :integer, :default => 0
318
318
  add_column :posts, :cached_weighted_score, :integer, :default => 0
319
+ add_column :posts, :cached_weighted_total, :integer, :default => 0
319
320
  add_index :posts, :cached_votes_total
320
321
  add_index :posts, :cached_votes_score
321
322
  add_index :posts, :cached_votes_up
322
323
  add_index :posts, :cached_votes_down
323
324
  add_index :posts, :cached_weighted_score
325
+ add_index :posts, :cached_weighted_total
324
326
 
325
327
  # Uncomment this line to force caching of existing votes
326
328
  # Post.find_each(&:update_cached_votes)
@@ -331,7 +333,8 @@ class AddCachedVotesToPosts < ActiveRecord::Migration
331
333
  remove_column :posts, :cached_votes_score
332
334
  remove_column :posts, :cached_votes_up
333
335
  remove_column :posts, :cached_votes_down
334
- remove_column :posts, :cached_weighted_score
336
+ remove_column :posts, :cached_weighted_score
337
+ remove_column :posts, :cached_weighted_total
335
338
  end
336
339
  end
337
340
  ```
@@ -1,3 +1,3 @@
1
1
  module ActsAsVotable
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -79,7 +79,7 @@ module ActsAsVotable
79
79
  _votes_ = find_votes_for({
80
80
  :voter_id => options[:voter].id,
81
81
  :vote_scope => options[:vote_scope],
82
- :voter_type => options[:voter].class.name
82
+ :voter_type => options[:voter].class.base_class.name
83
83
  })
84
84
 
85
85
  if _votes_.count == 0 or options[:duplicate]
@@ -114,7 +114,7 @@ module ActsAsVotable
114
114
 
115
115
  def unvote args = {}
116
116
  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.name)
117
+ _votes_ = find_votes_for(:voter_id => args[:voter].id, :vote_scope => args[:vote_scope], :voter_type => args[:voter].class.base_class.name)
118
118
 
119
119
  return true if _votes_.size == 0
120
120
  _votes_.each(&:destroy)
@@ -155,6 +155,10 @@ module ActsAsVotable
155
155
  "cached_scoped_#{vote_scope}_votes_score="
156
156
  when :cached_votes_score
157
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="
158
162
  when :cached_weighted_score
159
163
  "cached_weighted_#{vote_scope}_score"
160
164
  when :cached_weighted_score=
@@ -186,6 +190,10 @@ module ActsAsVotable
186
190
  )
187
191
  end
188
192
 
193
+ if self.respond_to?(:cached_weighted_total=)
194
+ updates[:cached_weighted_total] = weighted_total(true)
195
+ end
196
+
189
197
  if self.respond_to?(:cached_weighted_score=)
190
198
  updates[:cached_weighted_score] = weighted_score(true)
191
199
  end
@@ -203,6 +211,10 @@ module ActsAsVotable
203
211
  updates[scope_cache_field :cached_votes_down, vote_scope] = count_votes_down(true, vote_scope)
204
212
  end
205
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
+
206
218
  if self.respond_to?(scope_cache_field :cached_weighted_score=, vote_scope)
207
219
  updates[scope_cache_field :cached_weighted_score, vote_scope] = weighted_score(true, vote_scope)
208
220
  end
@@ -260,6 +272,15 @@ module ActsAsVotable
260
272
  get_down_votes(:vote_scope => vote_scope).count
261
273
  end
262
274
 
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
282
+ end
283
+
263
284
  def weighted_score skip_cache = false, vote_scope = nil
264
285
  if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_score, vote_scope)
265
286
  return self.send(scope_cache_field :cached_weighted_score, vote_scope)
@@ -271,7 +292,7 @@ module ActsAsVotable
271
292
 
272
293
  # voters
273
294
  def voted_on_by? voter
274
- votes = find_votes_for :voter_id => voter.id, :voter_type => voter.class.name
295
+ votes = find_votes_for :voter_id => voter.id, :voter_type => voter.class.base_class.name
275
296
  votes.count > 0
276
297
  end
277
298
 
@@ -53,25 +53,25 @@ module ActsAsVotable
53
53
 
54
54
  # results
55
55
  def voted_on? votable, args={}
56
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
56
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
57
57
  :vote_scope => args[:vote_scope])
58
58
  votes.size > 0
59
59
  end
60
60
 
61
61
  def voted_up_on? votable, args={}
62
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
62
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
63
63
  :vote_scope => args[:vote_scope], :vote_flag => true)
64
64
  votes.size > 0
65
65
  end
66
66
 
67
67
  def voted_down_on? votable, args={}
68
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
68
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
69
69
  :vote_scope => args[:vote_scope], :vote_flag => false)
70
70
  votes.size > 0
71
71
  end
72
72
 
73
73
  def voted_as_when_voting_on votable, args={}
74
- vote = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
74
+ vote = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
75
75
  :vote_scope => args[:vote_scope]).select(:vote_flag).last
76
76
  return nil unless vote
77
77
  return vote.vote_flag
@@ -107,7 +107,7 @@ module ActsAsVotable
107
107
  end
108
108
 
109
109
  def find_voted_items extra_conditions = {}
110
- options = extra_conditions.merge :voter_id => id, :voter_type => self.class.name
110
+ options = extra_conditions.merge :voter_id => id, :voter_type => self.class.base_class.name
111
111
  include_objects.where(options).collect(&:votable)
112
112
  end
113
113
 
@@ -206,6 +206,28 @@ shared_examples "a votable_model" do
206
206
  votable_cache.cached_votes_score.should == 0
207
207
  end
208
208
 
209
+ it "should update cached weighted total if there is a weighted total column" do
210
+ votable_cache.cached_weighted_total = 50
211
+ votable_cache.vote_by :voter => voter
212
+ votable_cache.cached_weighted_total.should == 1
213
+ votable_cache.vote_by :voter => voter2, :vote => 'false'
214
+ votable_cache.cached_weighted_total.should == 2
215
+ end
216
+
217
+ it "should update cached weighted total votes_for when a vote up is removed" do
218
+ votable_cache.vote_by :voter => voter, :vote => 'true', :vote_weight => 3
219
+ votable_cache.cached_weighted_total.should == 3
220
+ votable_cache.unvote :voter => voter
221
+ votable_cache.cached_weighted_total.should == 0
222
+ end
223
+
224
+ it "should update cached weighted total votes_for when a vote down is removed" do
225
+ votable_cache.vote_by :voter => voter, :vote => 'false', :vote_weight => 4
226
+ votable_cache.cached_weighted_total.should == 4
227
+ votable_cache.unvote :voter => voter
228
+ votable_cache.cached_weighted_total.should == 0
229
+ end
230
+
209
231
  it "should update cached weighted score if there is a weighted score column" do
210
232
  votable_cache.cached_weighted_score = 50
211
233
  votable_cache.vote_by :voter => voter
@@ -273,6 +295,12 @@ shared_examples "a votable_model" do
273
295
  votable_cache.count_votes_down.should == 50
274
296
  end
275
297
 
298
+ it "should select from cached weighted total if there is a weighted total column" do
299
+ votable_cache.vote_by :voter => voter, :vote => 'false'
300
+ votable_cache.cached_weighted_total = 50
301
+ votable_cache.weighted_total.should == 50
302
+ end
303
+
276
304
  it "should select from cached weighted score if there is a weighted score column" do
277
305
  votable_cache.vote_by :voter => voter, :vote => 'false'
278
306
  votable_cache.cached_weighted_score = 50
@@ -59,12 +59,14 @@ ActiveRecord::Schema.define(:version => 1) do
59
59
  t.integer :cached_votes_score
60
60
  t.integer :cached_votes_up
61
61
  t.integer :cached_votes_down
62
+ t.integer :cached_weighted_total
62
63
  t.integer :cached_weighted_score
63
64
 
64
65
  t.integer :cached_scoped_test_votes_total
65
66
  t.integer :cached_scoped_test_votes_score
66
67
  t.integer :cached_scoped_test_votes_up
67
68
  t.integer :cached_scoped_test_votes_down
69
+ t.integer :cached_scoped_weighted_total
68
70
  t.integer :cached_scoped_weighted_score
69
71
  end
70
72
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_votable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-27 00:00:00.000000000 Z
11
+ date: 2014-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec