acts_as_votable 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +10 -1
- data/Appraisals +6 -1
- data/README.md +38 -35
- data/gemfiles/rails_7_0.gemfile +8 -0
- data/lib/acts_as_votable/extenders/votable.rb +0 -1
- data/lib/acts_as_votable/extenders/voter.rb +0 -1
- data/lib/acts_as_votable/extenders.rb +11 -0
- data/lib/acts_as_votable/helpers.rb +9 -0
- data/lib/acts_as_votable/version.rb +1 -1
- data/lib/acts_as_votable/votable.rb +0 -1
- data/lib/acts_as_votable.rb +10 -4
- data/lib/generators/acts_as_votable/migration/migration_generator.rb +6 -0
- data/lib/generators/acts_as_votable/migration/templates/active_record/migration.erb +2 -2
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 716790eb29b78931fcc52052c68a260036354513ee02850f06fc4460812b2b0a
|
4
|
+
data.tar.gz: 06e8af46b169792a3f218666ef9ee73d41e781baae2c88a5ed0202aa151d9043
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 476384079c94b60160d19ececbc96aedd83a0b4cadbc7b17662cf630cd2cbcfaaed9c616a74a3e5e10a93c0ec692437628fe967c8712d1e39ee8282b3b210dc5
|
7
|
+
data.tar.gz: 84989f2e628bfb60a07e07696a903b383cc07f372ac9780c597dc56d7d64721f98fbf20d1d4f44106fc77463d9e52713254d624df5bea2da378ff76f66a7d633
|
data/.github/workflows/main.yml
CHANGED
@@ -25,6 +25,15 @@ jobs:
|
|
25
25
|
- gemfiles/rails_6_1.gemfile
|
26
26
|
continue-on-error:
|
27
27
|
- false
|
28
|
+
include:
|
29
|
+
- ruby: "3.0"
|
30
|
+
gemfile: gemfiles/rails_6_1.gemfile
|
31
|
+
os: ubuntu
|
32
|
+
continue-on-error: false
|
33
|
+
- ruby: 3.1
|
34
|
+
gemfile: gemfiles/rails_7_0.gemfile
|
35
|
+
os: ubuntu
|
36
|
+
continue-on-error: false
|
28
37
|
|
29
38
|
env:
|
30
39
|
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
@@ -41,4 +50,4 @@ jobs:
|
|
41
50
|
- name: Test
|
42
51
|
run: bundle exec rake spec
|
43
52
|
|
44
|
-
# use this
|
53
|
+
# use this
|
data/Appraisals
CHANGED
data/README.md
CHANGED
@@ -51,7 +51,7 @@ class Post < ApplicationRecord
|
|
51
51
|
acts_as_votable
|
52
52
|
end
|
53
53
|
|
54
|
-
@post = Post.new(:
|
54
|
+
@post = Post.new(name: 'my post!')
|
55
55
|
@post.save
|
56
56
|
|
57
57
|
@post.liked_by @user
|
@@ -66,9 +66,9 @@ more natural calls are the first few examples.
|
|
66
66
|
```ruby
|
67
67
|
@post.liked_by @user1
|
68
68
|
@post.downvote_from @user2
|
69
|
-
@post.vote_by :
|
70
|
-
@post.vote_by :
|
71
|
-
@post.vote_by :
|
69
|
+
@post.vote_by voter: @user3
|
70
|
+
@post.vote_by voter: @user4, vote: 'bad'
|
71
|
+
@post.vote_by voter: @user5, vote: 'like'
|
72
72
|
```
|
73
73
|
|
74
74
|
By default all votes are positive, so `@user3` has cast a 'good' vote for `@post`.
|
@@ -86,21 +86,21 @@ Revisiting the previous example of code.
|
|
86
86
|
```ruby
|
87
87
|
# positive votes
|
88
88
|
@post.liked_by @user1
|
89
|
-
@post.vote_by :
|
90
|
-
@post.vote_by :
|
89
|
+
@post.vote_by voter: @user3
|
90
|
+
@post.vote_by voter: @user5, vote: 'like'
|
91
91
|
|
92
92
|
# negative votes
|
93
93
|
@post.downvote_from @user2
|
94
|
-
@post.vote_by :
|
94
|
+
@post.vote_by voter: @user2, vote: 'bad'
|
95
95
|
|
96
96
|
# tally them up!
|
97
97
|
@post.votes_for.size # => 5
|
98
|
-
@post.weighted_total => 5
|
98
|
+
@post.weighted_total # => 5
|
99
99
|
@post.get_likes.size # => 3
|
100
100
|
@post.get_upvotes.size # => 3
|
101
101
|
@post.get_dislikes.size # => 2
|
102
102
|
@post.get_downvotes.size # => 2
|
103
|
-
@post.weighted_score => 1
|
103
|
+
@post.weighted_score # => 1
|
104
104
|
```
|
105
105
|
|
106
106
|
Active Record scopes are provided to make life easier.
|
@@ -142,29 +142,29 @@ You can add a scope to your vote
|
|
142
142
|
|
143
143
|
```ruby
|
144
144
|
# positive votes
|
145
|
-
@post.liked_by @user1, :
|
146
|
-
@post.vote_by :
|
147
|
-
@post.vote_by :
|
145
|
+
@post.liked_by @user1, vote_scope: 'rank'
|
146
|
+
@post.vote_by voter: @user3, vote_scope: 'rank'
|
147
|
+
@post.vote_by voter: @user5, vote: 'like', vote_scope: 'rank'
|
148
148
|
|
149
149
|
# negative votes
|
150
|
-
@post.downvote_from @user2, :
|
151
|
-
@post.vote_by :
|
150
|
+
@post.downvote_from @user2, vote_scope: 'rank'
|
151
|
+
@post.vote_by voter: @user2, vote: 'bad', vote_scope: 'rank'
|
152
152
|
|
153
153
|
# tally them up!
|
154
|
-
@post.find_votes_for(:
|
155
|
-
@post.get_likes(:
|
156
|
-
@post.get_upvotes(:
|
157
|
-
@post.get_dislikes(:
|
158
|
-
@post.get_downvotes(:
|
154
|
+
@post.find_votes_for(vote_scope: 'rank').size # => 5
|
155
|
+
@post.get_likes(vote_scope: 'rank').size # => 3
|
156
|
+
@post.get_upvotes(vote_scope: 'rank').size # => 3
|
157
|
+
@post.get_dislikes(vote_scope: 'rank').size # => 2
|
158
|
+
@post.get_downvotes(vote_scope: 'rank').size # => 2
|
159
159
|
|
160
160
|
# votable model can be voted under different scopes
|
161
161
|
# by the same user
|
162
|
-
@post.vote_by :
|
163
|
-
@post.vote_by :
|
162
|
+
@post.vote_by voter: @user1, vote_scope: 'week'
|
163
|
+
@post.vote_by voter: @user1, vote_scope: 'month'
|
164
164
|
|
165
165
|
@post.votes_for.size # => 2
|
166
|
-
@post.find_votes_for(:
|
167
|
-
@post.find_votes_for(:
|
166
|
+
@post.find_votes_for(vote_scope: 'week').size # => 1
|
167
|
+
@post.find_votes_for(vote_scope: 'month').size # => 1
|
168
168
|
```
|
169
169
|
|
170
170
|
### Adding weights to your votes
|
@@ -173,20 +173,20 @@ You can add weight to your vote. The default value is 1.
|
|
173
173
|
|
174
174
|
```ruby
|
175
175
|
# positive votes
|
176
|
-
@post.liked_by @user1, :
|
177
|
-
@post.vote_by :
|
178
|
-
@post.vote_by :
|
176
|
+
@post.liked_by @user1, vote_weight: 1
|
177
|
+
@post.vote_by voter: @user3, vote_weight: 2
|
178
|
+
@post.vote_by voter: @user5, vote: 'like', vote_scope: 'rank', vote_weight: 3
|
179
179
|
|
180
180
|
# negative votes
|
181
|
-
@post.downvote_from @user2, :
|
182
|
-
@post.vote_by :
|
181
|
+
@post.downvote_from @user2, vote_scope: 'rank', vote_weight: 1
|
182
|
+
@post.vote_by voter: @user2, vote: 'bad', vote_scope: 'rank', vote_weight: 3
|
183
183
|
|
184
184
|
# tally them up!
|
185
|
-
@post.find_votes_for(:
|
186
|
-
@post.get_likes(:
|
187
|
-
@post.get_upvotes(:
|
188
|
-
@post.get_dislikes(:
|
189
|
-
@post.get_downvotes(:
|
185
|
+
@post.find_votes_for(vote_scope: 'rank').sum(:vote_weight) # => 6
|
186
|
+
@post.get_likes(vote_scope: 'rank').sum(:vote_weight) # => 6
|
187
|
+
@post.get_upvotes(vote_scope: 'rank').sum(:vote_weight) # => 6
|
188
|
+
@post.get_dislikes(vote_scope: 'rank').sum(:vote_weight) # => 4
|
189
|
+
@post.get_downvotes(vote_scope: 'rank').sum(:vote_weight) # => 4
|
190
190
|
```
|
191
191
|
|
192
192
|
### The Voter
|
@@ -301,7 +301,7 @@ To permit duplicates entries of a same voter, use option duplicate. Also notice
|
|
301
301
|
will limit some other methods that didn't deal with multiples votes, in this case, the last vote will be considered.
|
302
302
|
|
303
303
|
```ruby
|
304
|
-
@hat.vote_by voter: @user, :
|
304
|
+
@hat.vote_by voter: @user, duplicate: true
|
305
305
|
```
|
306
306
|
|
307
307
|
## Caching
|
@@ -342,6 +342,9 @@ class AddCachedVotesToPosts < ActiveRecord::Migration
|
|
342
342
|
t.integer :cached_weighted_subscribe_score, default: 0
|
343
343
|
t.integer :cached_weighted_subscribe_total, default: 0
|
344
344
|
t.float :cached_weighted_subscribe_average, default: 0.0
|
345
|
+
|
346
|
+
# Uncomment this line to force caching of existing scoped votes
|
347
|
+
# Post.find_each { |p| p.update_cached_votes("subscribe") }
|
345
348
|
end
|
346
349
|
end
|
347
350
|
end
|
@@ -352,7 +355,7 @@ end
|
|
352
355
|
Order by average rating:
|
353
356
|
|
354
357
|
```ruby
|
355
|
-
Post.order(:
|
358
|
+
Post.order(cached_weighted_average: :desc)
|
356
359
|
```
|
357
360
|
|
358
361
|
Display average rating:
|
data/lib/acts_as_votable.rb
CHANGED
@@ -2,20 +2,26 @@
|
|
2
2
|
|
3
3
|
require "active_record"
|
4
4
|
require "active_support/inflector"
|
5
|
+
require "active_support/dependencies/autoload"
|
5
6
|
|
6
7
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
8
|
|
8
9
|
module ActsAsVotable
|
10
|
+
extend ActiveSupport::Autoload
|
11
|
+
|
12
|
+
autoload :Votable
|
13
|
+
autoload :Vote
|
14
|
+
autoload :Voter
|
15
|
+
autoload :Cacheable
|
16
|
+
autoload :Extenders
|
17
|
+
autoload :Helpers
|
18
|
+
|
9
19
|
if defined?(ActiveRecord::Base)
|
10
|
-
require "acts_as_votable/extenders/votable"
|
11
|
-
require "acts_as_votable/extenders/voter"
|
12
|
-
require "acts_as_votable/vote"
|
13
20
|
ActiveRecord::Base.extend ActsAsVotable::Extenders::Votable
|
14
21
|
ActiveRecord::Base.extend ActsAsVotable::Extenders::Voter
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
|
-
require "acts_as_votable/extenders/controller"
|
19
25
|
ActiveSupport.on_load(:action_controller) do
|
20
26
|
include ActsAsVotable::Extenders::Controller
|
21
27
|
end
|
@@ -38,6 +38,8 @@ module ActsAsVotable
|
|
38
38
|
"[4.2]"
|
39
39
|
elsif rails6?
|
40
40
|
"[6.0]"
|
41
|
+
elsif rails7?
|
42
|
+
"[7.0]"
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
@@ -48,5 +50,9 @@ module ActsAsVotable
|
|
48
50
|
def rails6?
|
49
51
|
Rails.version.start_with? "6"
|
50
52
|
end
|
53
|
+
|
54
|
+
def rails7?
|
55
|
+
Rails.version.start_with? "7"
|
56
|
+
end
|
51
57
|
end
|
52
58
|
end
|
@@ -2,8 +2,8 @@ class ActsAsVotableMigration < ActiveRecord::Migration<%= migration_version %>
|
|
2
2
|
def self.up
|
3
3
|
create_table :votes do |t|
|
4
4
|
|
5
|
-
t.references :votable, :
|
6
|
-
t.references :voter, :
|
5
|
+
t.references :votable, polymorphic: true
|
6
|
+
t.references :voter, polymorphic: true
|
7
7
|
|
8
8
|
t.boolean :vote_flag
|
9
9
|
t.string :vote_scope
|
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.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -118,11 +118,14 @@ files:
|
|
118
118
|
- gemfiles/rails_6.gemfile
|
119
119
|
- gemfiles/rails_6_1.gemfile
|
120
120
|
- gemfiles/rails_6_rc1.gemfile
|
121
|
+
- gemfiles/rails_7_0.gemfile
|
121
122
|
- lib/acts_as_votable.rb
|
122
123
|
- lib/acts_as_votable/cacheable.rb
|
124
|
+
- lib/acts_as_votable/extenders.rb
|
123
125
|
- lib/acts_as_votable/extenders/controller.rb
|
124
126
|
- lib/acts_as_votable/extenders/votable.rb
|
125
127
|
- lib/acts_as_votable/extenders/voter.rb
|
128
|
+
- lib/acts_as_votable/helpers.rb
|
126
129
|
- lib/acts_as_votable/helpers/words.rb
|
127
130
|
- lib/acts_as_votable/version.rb
|
128
131
|
- lib/acts_as_votable/votable.rb
|
@@ -152,7 +155,7 @@ homepage: http://rubygems.org/gems/acts_as_votable
|
|
152
155
|
licenses:
|
153
156
|
- MIT
|
154
157
|
metadata: {}
|
155
|
-
post_install_message:
|
158
|
+
post_install_message:
|
156
159
|
rdoc_options: []
|
157
160
|
require_paths:
|
158
161
|
- lib
|
@@ -167,8 +170,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
170
|
- !ruby/object:Gem::Version
|
168
171
|
version: '0'
|
169
172
|
requirements: []
|
170
|
-
|
171
|
-
|
173
|
+
rubyforge_project:
|
174
|
+
rubygems_version: 2.7.6
|
175
|
+
signing_key:
|
172
176
|
specification_version: 4
|
173
177
|
summary: Rails gem to allowing records to be votable
|
174
178
|
test_files:
|