acts_as_votable 0.11.0 → 0.13.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +44 -0
- data/.gitignore +1 -0
- data/.ruby-version +1 -0
- data/Appraisals +12 -6
- data/{README.markdown → README.md} +47 -110
- data/acts_as_votable.gemspec +2 -4
- data/gemfiles/rails_4.gemfile +1 -1
- data/gemfiles/rails_5.gemfile +1 -1
- data/gemfiles/rails_5_1.gemfile +1 -1
- data/gemfiles/rails_5_2.gemfile +7 -0
- data/gemfiles/rails_6.gemfile +8 -0
- data/gemfiles/rails_6_1.gemfile +8 -0
- data/gemfiles/rails_6_rc1.gemfile +8 -0
- data/lib/acts_as_votable/cacheable.rb +15 -11
- data/lib/acts_as_votable/extenders/votable.rb +2 -2
- data/lib/acts_as_votable/version.rb +1 -1
- data/lib/acts_as_votable/votable.rb +41 -25
- data/lib/acts_as_votable/voter.rb +4 -4
- data/lib/generators/acts_as_votable/migration/migration_generator.rb +7 -1
- data/spec/factories/votable.rb +1 -1
- data/spec/factories/votable_cache.rb +1 -1
- data/spec/factories/votable_cache_update_attributes.rb +2 -2
- data/spec/factories/votable_cache_update_columns.rb +1 -1
- data/spec/factories/votable_child_of_sti_not_votable.rb +1 -1
- data/spec/factories/votable_child_of_sti_votable.rb +1 -1
- data/spec/factories/votable_voter.rb +1 -1
- data/spec/factories/vote.rb +1 -1
- data/spec/factories/voter.rb +1 -1
- data/spec/shared_example/votable_model.rb +40 -4
- data/spec/spec_helper.rb +3 -3
- data/spec/support/factory_bot.rb +9 -0
- metadata +36 -17
- data/.travis.yml +0 -14
- data/gemfiles/rails_4.gemfile.lock +0 -159
- data/gemfiles/rails_5.gemfile.lock +0 -166
- data/gemfiles/rails_5_1.gemfile.lock +0 -166
- data/spec/support/factory_girl.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
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
|
@@ -0,0 +1,44 @@
|
|
1
|
+
name: CI
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches:
|
5
|
+
- master
|
6
|
+
pull_request:
|
7
|
+
branches:
|
8
|
+
- master
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
tests:
|
12
|
+
strategy:
|
13
|
+
fail-fast: false
|
14
|
+
matrix:
|
15
|
+
os:
|
16
|
+
- ubuntu
|
17
|
+
ruby:
|
18
|
+
- 2.5
|
19
|
+
- 2.6
|
20
|
+
- 2.7
|
21
|
+
gemfile:
|
22
|
+
- gemfiles/rails_5_1.gemfile
|
23
|
+
- gemfiles/rails_5_2.gemfile
|
24
|
+
- gemfiles/rails_6.gemfile
|
25
|
+
- gemfiles/rails_6_1.gemfile
|
26
|
+
continue-on-error:
|
27
|
+
- false
|
28
|
+
|
29
|
+
env:
|
30
|
+
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
31
|
+
runs-on: ${{ matrix.os }}-latest
|
32
|
+
continue-on-error: ${{ matrix.continue-on-error }}
|
33
|
+
steps:
|
34
|
+
- name: Checkout
|
35
|
+
uses: actions/checkout@v2
|
36
|
+
- name: Setup Ruby
|
37
|
+
uses: ruby/setup-ruby@v1
|
38
|
+
with:
|
39
|
+
ruby-version: ${{ matrix.ruby }}
|
40
|
+
bundler-cache: true
|
41
|
+
- name: Test
|
42
|
+
run: bundle exec rake spec
|
43
|
+
|
44
|
+
# use this
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.3
|
data/Appraisals
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
appraise "rails-
|
4
|
-
gem "rails", "
|
3
|
+
appraise "rails-5-1" do
|
4
|
+
gem "rails", "5.1.7"
|
5
5
|
end
|
6
6
|
|
7
|
-
appraise "rails-5" do
|
8
|
-
gem "rails", "5.
|
7
|
+
appraise "rails-5-2" do
|
8
|
+
gem "rails", "5.2.4"
|
9
9
|
end
|
10
10
|
|
11
|
-
appraise "rails-
|
12
|
-
gem "rails", "
|
11
|
+
appraise "rails-6" do
|
12
|
+
gem "rails", "6.0.3"
|
13
|
+
gem "sqlite3", "~> 1.4"
|
13
14
|
end
|
15
|
+
|
16
|
+
appraise "rails-6-1" do
|
17
|
+
gem "rails", "6.1.0"
|
18
|
+
gem "sqlite3", "~> 1.4"
|
19
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# Acts As Votable (aka Acts As Likeable)
|
2
2
|
|
3
|
-
|
4
|
-
[![Code Climate](https://codeclimate.com/github/ryanto/acts_as_votable.png)](https://codeclimate.com/github/ryanto/acts_as_votable)
|
3
|
+
![Build status](https://github.com/ryanto/acts_as_votable/workflows/CI/badge.svg)
|
5
4
|
|
6
5
|
Acts As Votable is a Ruby Gem specifically written for Rails/ActiveRecord models.
|
7
6
|
The main goals of this gem are:
|
@@ -16,16 +15,15 @@ The main goals of this gem are:
|
|
16
15
|
|
17
16
|
### Supported Ruby and Rails versions
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
* Rails 5.0, 5.1
|
18
|
+
- Ruby >= 2.5.0
|
19
|
+
- Rails >= 5.1
|
22
20
|
|
23
21
|
### Install
|
24
22
|
|
25
23
|
Just add the following to your Gemfile to install the latest release.
|
26
24
|
|
27
25
|
```ruby
|
28
|
-
gem 'acts_as_votable'
|
26
|
+
gem 'acts_as_votable'
|
29
27
|
```
|
30
28
|
|
31
29
|
And follow that up with a ``bundle install``.
|
@@ -35,8 +33,10 @@ And follow that up with a ``bundle install``.
|
|
35
33
|
Acts As Votable uses a votes table to store all voting information. To
|
36
34
|
generate and run the migration just use.
|
37
35
|
|
38
|
-
|
39
|
-
|
36
|
+
```bash
|
37
|
+
rails generate acts_as_votable:migration
|
38
|
+
rails db:migrate
|
39
|
+
```
|
40
40
|
|
41
41
|
You will get a performance increase by adding in cached columns to your model's
|
42
42
|
tables. You will have to do this manually through your own migrations. See the
|
@@ -47,7 +47,7 @@ caching section of this document for more information.
|
|
47
47
|
### Votable Models
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
class Post <
|
50
|
+
class Post < ApplicationRecord
|
51
51
|
acts_as_votable
|
52
52
|
end
|
53
53
|
|
@@ -77,7 +77,6 @@ By default all votes are positive, so `@user3` has cast a 'good' vote for `@post
|
|
77
77
|
|
78
78
|
`@user2` and `@user4` on the other had has voted against `@post`.
|
79
79
|
|
80
|
-
|
81
80
|
Just about any word works for casting a vote in favor or against post. Up/Down,
|
82
81
|
Like/Dislike, Positive/Negative... the list goes on-and-on. Boolean flags `true` and
|
83
82
|
`false` are also applicable.
|
@@ -167,6 +166,7 @@ You can add a scope to your vote
|
|
167
166
|
@post.find_votes_for(:vote_scope => 'week').size # => 1
|
168
167
|
@post.find_votes_for(:vote_scope => 'month').size # => 1
|
169
168
|
```
|
169
|
+
|
170
170
|
### Adding weights to your votes
|
171
171
|
|
172
172
|
You can add weight to your vote. The default value is 1.
|
@@ -194,15 +194,15 @@ You can add weight to your vote. The default value is 1.
|
|
194
194
|
You can have your voters `acts_as_voter` to provide some reserve functionality.
|
195
195
|
|
196
196
|
```ruby
|
197
|
-
class User <
|
197
|
+
class User < ApplicationRecord
|
198
198
|
acts_as_voter
|
199
199
|
end
|
200
200
|
|
201
201
|
@user.likes @article
|
202
202
|
|
203
|
-
@article.
|
204
|
-
@article.
|
205
|
-
@article.
|
203
|
+
@article.votes_for.size # => 1
|
204
|
+
@article.get_likes.size # => 1
|
205
|
+
@article.get_dislikes.size # => 0
|
206
206
|
```
|
207
207
|
|
208
208
|
To check if a voter has voted on a model, you can use ``voted_for?``. You can
|
@@ -275,8 +275,8 @@ because `@user` has already voted for `@shoe`.
|
|
275
275
|
@user.likes @shoe
|
276
276
|
@user.likes @shoe
|
277
277
|
|
278
|
-
@shoe.
|
279
|
-
@shoe.
|
278
|
+
@shoe.votes_for.size # => 1
|
279
|
+
@shoe.get_likes.size # => 1
|
280
280
|
```
|
281
281
|
|
282
282
|
To check if a vote counted, or registered, use `vote_registered?` on your model
|
@@ -292,9 +292,9 @@ after voting. For example:
|
|
292
292
|
@hat.disliked_by @user
|
293
293
|
@hat.vote_registered? # => true, because user changed their vote
|
294
294
|
|
295
|
-
@hat.
|
296
|
-
@hat.
|
297
|
-
@hat.
|
295
|
+
@hat.votes_for.size # => 1
|
296
|
+
@hat.get_positives.size # => 0
|
297
|
+
@hat.get_negatives.size # => 1
|
298
298
|
```
|
299
299
|
|
300
300
|
To permit duplicates entries of a same voter, use option duplicate. Also notice that this
|
@@ -312,35 +312,20 @@ to speed up @post we would use the following migration:
|
|
312
312
|
|
313
313
|
```ruby
|
314
314
|
class AddCachedVotesToPosts < ActiveRecord::Migration
|
315
|
-
def
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
add_index :posts, :cached_votes_up
|
326
|
-
add_index :posts, :cached_votes_down
|
327
|
-
add_index :posts, :cached_weighted_score
|
328
|
-
add_index :posts, :cached_weighted_total
|
329
|
-
add_index :posts, :cached_weighted_average
|
315
|
+
def change
|
316
|
+
change_table :posts do |t|
|
317
|
+
t.integer :cached_votes_total, default: 0
|
318
|
+
t.integer :cached_votes_score, default: 0
|
319
|
+
t.integer :cached_votes_up, default: 0
|
320
|
+
t.integer :cached_votes_down, default: 0
|
321
|
+
t.integer :cached_weighted_score, default: 0
|
322
|
+
t.integer :cached_weighted_total, default: 0
|
323
|
+
t.float :cached_weighted_average, default: 0.0
|
324
|
+
end
|
330
325
|
|
331
326
|
# Uncomment this line to force caching of existing votes
|
332
327
|
# Post.find_each(&:update_cached_votes)
|
333
328
|
end
|
334
|
-
|
335
|
-
def self.down
|
336
|
-
remove_column :posts, :cached_votes_total
|
337
|
-
remove_column :posts, :cached_votes_score
|
338
|
-
remove_column :posts, :cached_votes_up
|
339
|
-
remove_column :posts, :cached_votes_down
|
340
|
-
remove_column :posts, :cached_weighted_score
|
341
|
-
remove_column :posts, :cached_weighted_total
|
342
|
-
remove_column :posts, :cached_weighted_average
|
343
|
-
end
|
344
329
|
end
|
345
330
|
```
|
346
331
|
|
@@ -348,36 +333,21 @@ If you have a scope for your vote, let's say `subscribe`, your migration will be
|
|
348
333
|
|
349
334
|
```ruby
|
350
335
|
class AddCachedVotesToPosts < ActiveRecord::Migration
|
351
|
-
def
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
add_index :posts, :cached_scoped_subscribe_votes_up
|
362
|
-
add_index :posts, :cached_scoped_subscribe_votes_down
|
363
|
-
add_index :posts, :cached_weighted_subscribe_score
|
364
|
-
add_index :posts, :cached_weighted_subscribe_total
|
365
|
-
add_index :posts, :cached_weighted_subscribe_average
|
366
|
-
end
|
367
|
-
|
368
|
-
def self.down
|
369
|
-
remove_column :posts, :cached_scoped_subscribe_votes_total
|
370
|
-
remove_column :posts, :cached_scoped_subscribe_votes_score
|
371
|
-
remove_column :posts, :cached_scoped_subscribe_votes_up
|
372
|
-
remove_column :posts, :cached_scoped_subscribe_votes_down
|
373
|
-
remove_column :posts, :cached_weighted_subscribe_score
|
374
|
-
remove_column :posts, :cached_weighted_subscribe_total
|
375
|
-
remove_column :posts, :cached_weighted_subscribe_average
|
336
|
+
def change
|
337
|
+
change_table :posts do |t|
|
338
|
+
t.integer :cached_scoped_subscribe_votes_total, default: 0
|
339
|
+
t.integer :cached_scoped_subscribe_votes_score, default: 0
|
340
|
+
t.integer :cached_scoped_subscribe_votes_up, default: 0
|
341
|
+
t.integer :cached_scoped_subscribe_votes_down, default: 0
|
342
|
+
t.integer :cached_weighted_subscribe_score, default: 0
|
343
|
+
t.integer :cached_weighted_subscribe_total, default: 0
|
344
|
+
t.float :cached_weighted_subscribe_average, default: 0.0
|
345
|
+
end
|
376
346
|
end
|
377
347
|
end
|
378
348
|
```
|
379
349
|
|
380
|
-
`cached_weighted_average` can be helpful for a rating system, e.g.:
|
350
|
+
`cached_weighted_average` can be helpful for a rating system, e.g.:
|
381
351
|
|
382
352
|
Order by average rating:
|
383
353
|
|
@@ -391,63 +361,30 @@ Display average rating:
|
|
391
361
|
<%= post.weighted_average.round(2) %> / 5
|
392
362
|
<!-- 3.5 / 5 -->
|
393
363
|
```
|
394
|
-
|
364
|
+
|
365
|
+
## Votable model's `updated_at`
|
395
366
|
|
396
367
|
You can control whether `updated_at` column of votable model will be touched or
|
397
368
|
not by passing `cacheable_strategy` option to `acts_as_votable` method.
|
398
369
|
|
399
|
-
By default, `
|
370
|
+
By default, `update` strategy is used. Pass `:update_columns` as
|
400
371
|
`cacheable_strategy` if you don't want to touch model's `updated_at` column.
|
372
|
+
|
401
373
|
```ruby
|
402
|
-
class Post <
|
374
|
+
class Post < ApplicationRecord
|
403
375
|
acts_as_votable cacheable_strategy: :update_columns
|
404
376
|
end
|
405
377
|
```
|
406
378
|
|
407
|
-
NOTE: this option does not work for ActiveRecord < 3.1
|
408
|
-
|
409
|
-
|
410
379
|
## Testing
|
411
380
|
|
412
381
|
All tests follow the RSpec format and are located in the spec directory.
|
413
382
|
They can be run with:
|
414
383
|
|
415
|
-
```
|
384
|
+
```bash
|
416
385
|
rake spec
|
417
386
|
```
|
418
387
|
|
419
|
-
## Changes
|
420
|
-
|
421
|
-
### Fixes for votable voter model
|
422
|
-
|
423
|
-
In version 0.8.0, there are bugs for a model that is both votable and voter.
|
424
|
-
Some name-conflicting methods are renamed:
|
425
|
-
+ Renamed Votable.votes to votes_for
|
426
|
-
+ Renamed Votable.vote to vote_by,
|
427
|
-
+ Removed Votable.vote_by alias (was an alias for :vote_up)
|
428
|
-
+ Renamed Votable.unvote_for to unvote_by
|
429
|
-
+ Renamed Votable.find_votes to find_votes_for
|
430
|
-
+ Renamed Votable.up_votes to get_upvotes
|
431
|
-
+ and its aliases :get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes
|
432
|
-
+ Renamed Votable.down_votes to get_downvotes
|
433
|
-
+ and its aliases :get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
|
434
|
-
|
435
|
-
|
436
388
|
## License
|
437
389
|
|
438
|
-
Acts as votable is released under the [MIT
|
439
|
-
License](http://www.opensource.org/licenses/MIT).
|
440
|
-
|
441
|
-
## Next steps
|
442
|
-
|
443
|
-
- Pass in a block of options when creating acts_as. Allow for things
|
444
|
-
like disabling the aliasing
|
445
|
-
|
446
|
-
- Smarter language syntax. Example: `@user.likes` will return all of the votables
|
447
|
-
that the user likes, while `@user.likes @model` will cast a vote for @model by
|
448
|
-
@user.
|
449
|
-
|
450
|
-
- The aliased methods are referred to by using the terms 'up/down' and/or
|
451
|
-
'true/false'. Need to come up with guidelines for naming these methods.
|
452
|
-
|
453
|
-
- 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).
|
data/acts_as_votable.gemspec
CHANGED
@@ -15,17 +15,15 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.description = "Rails gem to allowing records to be votable"
|
16
16
|
s.license = "MIT"
|
17
17
|
|
18
|
-
s.rubyforge_project = "acts_as_votable"
|
19
|
-
|
20
18
|
s.files = `git ls-files`.split("\n")
|
21
19
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
20
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
23
21
|
s.require_paths = ["lib"]
|
24
22
|
|
25
23
|
s.add_development_dependency "rspec", "~> 3.6"
|
26
|
-
s.add_development_dependency "sqlite3", "~> 1.3"
|
24
|
+
s.add_development_dependency "sqlite3", "~> 1.3.6"
|
27
25
|
s.add_development_dependency "rubocop", "~> 0.49.1"
|
28
26
|
s.add_development_dependency "simplecov", "~> 0.15.0"
|
29
27
|
s.add_development_dependency "appraisal", "~> 2.2"
|
30
|
-
s.add_development_dependency "
|
28
|
+
s.add_development_dependency "factory_bot", "~> 4.8"
|
31
29
|
end
|
data/gemfiles/rails_4.gemfile
CHANGED
data/gemfiles/rails_5.gemfile
CHANGED
data/gemfiles/rails_5_1.gemfile
CHANGED