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 +4 -4
- data/Appraisals +0 -4
- data/README.md +12 -43
- data/lib/acts_as_votable/cacheable.rb +15 -11
- data/lib/acts_as_votable/version.rb +1 -1
- data/lib/acts_as_votable/votable.rb +5 -6
- data/lib/acts_as_votable/voter.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d9154268f7a15b8f66a0dcf909ce0dffd9e675c97559924c653c3ba90f4cf14
|
4
|
+
data.tar.gz: 3cf9b07d912de5732d8867ad1fb16394244857ce4fb150682154c5e4a6b3e715
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c1c7c024425ec4e0a22432e820f6d47598d0b0adaef43930c37f9ea9fb9d7c832e831e60252b8545679d12125b9f4912e8cc14356ad34e32c26237ddfa0422c
|
7
|
+
data.tar.gz: f36cbcdc97e3f03470227dc669ad9f992f09e8d2e7ff66c5f0bd986ace5677dddcbf1552e6ee69e18148628ad70e5d5cd55357f4048f6f7fb503eca0a5c5a13f
|
data/Appraisals
CHANGED
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
|
-
|
19
|
-
|
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
|
-
|
37
|
-
|
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
|
-
|
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
|
-
|
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
|
|
@@ -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: :
|
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
|
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.
|
115
|
-
update_cached_votes
|
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
|
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: :
|
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
|