acts_as_votable 0.13.0 → 0.13.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1843c2e0892abc19c5ecc63dd48e38fe8d48e1cdb7affc3c90d67b0419725e1
4
- data.tar.gz: b05a72a3a7310c362233e9d54301a695a03fb72c9fb6068b2fde8e58d1e238bb
3
+ metadata.gz: 1d9154268f7a15b8f66a0dcf909ce0dffd9e675c97559924c653c3ba90f4cf14
4
+ data.tar.gz: 3cf9b07d912de5732d8867ad1fb16394244857ce4fb150682154c5e4a6b3e715
5
5
  SHA512:
6
- metadata.gz: a125832f11dd75fa540f90791b5bdc092b92ade7c2a34218947fa1621aae21487eb38d8664636d1113885bb878927805a10f0db5edb786c48a8339832859bd68
7
- data.tar.gz: 23bb31cc323d1173f982b2d12895b834e26b1ede7a6293cc1343538140f2fe3f7f9c0c89daf4de4ea67d72a69b4b4bbca2c4ec53555500ec965f26ca8fb1f475
6
+ metadata.gz: 3c1c7c024425ec4e0a22432e820f6d47598d0b0adaef43930c37f9ea9fb9d7c832e831e60252b8545679d12125b9f4912e8cc14356ad34e32c26237ddfa0422c
7
+ data.tar.gz: f36cbcdc97e3f03470227dc669ad9f992f09e8d2e7ff66c5f0bd986ace5677dddcbf1552e6ee69e18148628ad70e5d5cd55357f4048f6f7fb503eca0a5c5a13f
data/Appraisals CHANGED
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- appraise "rails-4" do
4
- gem "rails", "4.2.11"
5
- end
6
-
7
3
  appraise "rails-5-1" do
8
4
  gem "rails", "5.1.7"
9
5
  end
data/README.md CHANGED
@@ -15,8 +15,8 @@ The main goals of this gem are:
15
15
 
16
16
  ### Supported Ruby and Rails versions
17
17
 
18
- * Ruby >= 2.3.0
19
- * Rails >= 4
18
+ - Ruby >= 2.5.0
19
+ - Rails >= 5.1
20
20
 
21
21
  ### Install
22
22
 
@@ -33,8 +33,10 @@ And follow that up with a ``bundle install``.
33
33
  Acts As Votable uses a votes table to store all voting information. To
34
34
  generate and run the migration just use.
35
35
 
36
- rails generate acts_as_votable:migration
37
- rails db:migrate
36
+ ```bash
37
+ rails generate acts_as_votable:migration
38
+ rails db:migrate
39
+ ```
38
40
 
39
41
  You will get a performance increase by adding in cached columns to your model's
40
42
  tables. You will have to do this manually through your own migrations. See the
@@ -75,7 +77,6 @@ By default all votes are positive, so `@user3` has cast a 'good' vote for `@post
75
77
 
76
78
  `@user2` and `@user4` on the other had has voted against `@post`.
77
79
 
78
-
79
80
  Just about any word works for casting a vote in favor or against post. Up/Down,
80
81
  Like/Dislike, Positive/Negative... the list goes on-and-on. Boolean flags `true` and
81
82
  `false` are also applicable.
@@ -165,6 +166,7 @@ You can add a scope to your vote
165
166
  @post.find_votes_for(:vote_scope => 'week').size # => 1
166
167
  @post.find_votes_for(:vote_scope => 'month').size # => 1
167
168
  ```
169
+
168
170
  ### Adding weights to your votes
169
171
 
170
172
  You can add weight to your vote. The default value is 1.
@@ -359,63 +361,30 @@ Display average rating:
359
361
  <%= post.weighted_average.round(2) %> / 5
360
362
  <!-- 3.5 / 5 -->
361
363
  ```
362
- ## updated_at at Votable model
364
+
365
+ ## Votable model's `updated_at`
363
366
 
364
367
  You can control whether `updated_at` column of votable model will be touched or
365
368
  not by passing `cacheable_strategy` option to `acts_as_votable` method.
366
369
 
367
370
  By default, `update` strategy is used. Pass `:update_columns` as
368
371
  `cacheable_strategy` if you don't want to touch model's `updated_at` column.
372
+
369
373
  ```ruby
370
374
  class Post < ApplicationRecord
371
375
  acts_as_votable cacheable_strategy: :update_columns
372
376
  end
373
377
  ```
374
378
 
375
- NOTE: this option does not work for ActiveRecord < 3.1
376
-
377
-
378
379
  ## Testing
379
380
 
380
381
  All tests follow the RSpec format and are located in the spec directory.
381
382
  They can be run with:
382
383
 
383
- ```
384
+ ```bash
384
385
  rake spec
385
386
  ```
386
387
 
387
- ## Changes
388
-
389
- ### Fixes for votable voter model
390
-
391
- In version 0.8.0, there are bugs for a model that is both votable and voter.
392
- Some name-conflicting methods are renamed:
393
- + Renamed Votable.votes to votes_for
394
- + Renamed Votable.vote to vote_by,
395
- + Removed Votable.vote_by alias (was an alias for :vote_up)
396
- + Renamed Votable.unvote_for to unvote_by
397
- + Renamed Votable.find_votes to find_votes_for
398
- + Renamed Votable.up_votes to get_upvotes
399
- + and its aliases :get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes
400
- + Renamed Votable.down_votes to get_downvotes
401
- + and its aliases :get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
402
-
403
-
404
388
  ## License
405
389
 
406
- Acts as votable is released under the [MIT
407
- License](http://www.opensource.org/licenses/MIT).
408
-
409
- ## Next steps
410
-
411
- - Pass in a block of options when creating acts_as. Allow for things
412
- like disabling the aliasing
413
-
414
- - Smarter language syntax. Example: `@user.likes` will return all of the votables
415
- that the user likes, while `@user.likes @model` will cast a vote for @model by
416
- @user.
417
-
418
- - The aliased methods are referred to by using the terms 'up/down' and/or
419
- 'true/false'. Need to come up with guidelines for naming these methods.
420
-
421
- - Create more aliases. Specifically for counting votes and finding votes.
390
+ Acts as votable is released under the [MIT License](http://www.opensource.org/licenses/MIT).
@@ -40,10 +40,6 @@ module ActsAsVotable
40
40
  def update_cached_votes(vote_scope = nil)
41
41
  updates = {}
42
42
 
43
- if self.respond_to?(:cached_votes_total=)
44
- updates[:cached_votes_total] = count_votes_total(true)
45
- end
46
-
47
43
  if self.respond_to?(:cached_votes_up=)
48
44
  updates[:cached_votes_up] = count_votes_up(true)
49
45
  end
@@ -52,6 +48,13 @@ module ActsAsVotable
52
48
  updates[:cached_votes_down] = count_votes_down(true)
53
49
  end
54
50
 
51
+ if self.respond_to?(:cached_votes_total=)
52
+ updates[:cached_votes_total] = (
53
+ (updates[:cached_votes_up] || count_votes_up(true)) +
54
+ (updates[:cached_votes_down] || count_votes_down(true))
55
+ )
56
+ end
57
+
55
58
  if self.respond_to?(:cached_votes_score=)
56
59
  updates[:cached_votes_score] = (
57
60
  (updates[:cached_votes_up] || count_votes_up(true)) -
@@ -72,10 +75,6 @@ module ActsAsVotable
72
75
  end
73
76
 
74
77
  if vote_scope
75
- if self.respond_to?(scope_cache_field :cached_votes_total=, vote_scope)
76
- updates[scope_cache_field :cached_votes_total, vote_scope] = count_votes_total(true, vote_scope)
77
- end
78
-
79
78
  if self.respond_to?(scope_cache_field :cached_votes_up=, vote_scope)
80
79
  updates[scope_cache_field :cached_votes_up, vote_scope] = count_votes_up(true, vote_scope)
81
80
  end
@@ -84,6 +83,13 @@ module ActsAsVotable
84
83
  updates[scope_cache_field :cached_votes_down, vote_scope] = count_votes_down(true, vote_scope)
85
84
  end
86
85
 
86
+ if self.respond_to?(scope_cache_field :cached_votes_total=, vote_scope)
87
+ updates[scope_cache_field :cached_votes_total, vote_scope] = (
88
+ (updates[scope_cache_field :cached_votes_up, vote_scope] || count_votes_up(true, vote_scope)) +
89
+ (updates[scope_cache_field :cached_votes_down, vote_scope] || count_votes_down(true, vote_scope))
90
+ )
91
+ end
92
+
87
93
  if self.respond_to?(scope_cache_field :cached_weighted_total=, vote_scope)
88
94
  updates[scope_cache_field :cached_weighted_total, vote_scope] = weighted_total(true, vote_scope)
89
95
  end
@@ -136,9 +142,7 @@ module ActsAsVotable
136
142
 
137
143
  def weighted_total(skip_cache = false, vote_scope = nil)
138
144
  from_cache(skip_cache, :cached_weighted_total, vote_scope) do
139
- ups = get_up_votes(vote_scope: vote_scope).sum(:vote_weight)
140
- downs = get_down_votes(vote_scope: vote_scope).sum(:vote_weight)
141
- ups + downs
145
+ find_votes_for(scope_or_empty_hash(vote_scope)).sum(:vote_weight)
142
146
  end
143
147
  end
144
148
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsVotable
4
- VERSION = "0.13.0"
4
+ VERSION = "0.13.1"
5
5
  end
@@ -35,7 +35,7 @@ module ActsAsVotable
35
35
  }
36
36
 
37
37
  base.class_eval do
38
- 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: :delete_all do
39
39
  def voters
40
40
  includes(:voter).map(&:voter)
41
41
  end
@@ -74,7 +74,7 @@ module ActsAsVotable
74
74
  # find the vote
75
75
  votes = find_votes_by(options[:voter], options[:vote_scope])
76
76
 
77
- if votes.empty? || options[:duplicate]
77
+ if options[:duplicate] || !votes.exists?
78
78
  # this voter has never voted
79
79
  vote = ActsAsVotable::Vote.new(
80
80
  votable: self,
@@ -109,12 +109,11 @@ module ActsAsVotable
109
109
  return false if args[:voter].nil?
110
110
  votes = find_votes_by(args[:voter], args[:vote_scope])
111
111
 
112
- return true if votes.empty?
113
112
  ActiveRecord::Base.transaction do
114
- votes.each(&:destroy)
115
- update_cached_votes args[:vote_scope]
113
+ deleted_count = votes.delete_all
114
+ update_cached_votes(args[:vote_scope]) if deleted_count > 0
116
115
  end
117
- self.vote_registered = false if votes_for.empty?
116
+ self.vote_registered = false
118
117
  return true
119
118
  end
120
119
 
@@ -18,7 +18,7 @@ module ActsAsVotable
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: :delete_all do
22
22
  def votables
23
23
  includes(:votable).map(&:votable)
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_votable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan