rating/README.md

412 lines
9.5 KiB
Markdown
Raw Normal View History

2017-10-30 01:44:23 +00:00
# Rating
[![Build Status](https://travis-ci.org/wbotelhos/rating.svg)](https://travis-ci.org/wbotelhos/rating)
[![Gem Version](https://badge.fury.io/rb/rating.svg)](https://badge.fury.io/rb/rating)
2018-01-16 03:25:46 +00:00
[![Maintainability](https://api.codeclimate.com/v1/badges/cc5efe8b06bc1d5e9e8a/maintainability)](https://codeclimate.com/github/wbotelhos/rating/maintainability)
2018-02-14 17:45:00 +00:00
[![Patreon](https://img.shields.io/badge/donate-%3C3-brightgreen.svg)](https://www.patreon.com/wbotelhos)
2017-10-30 01:44:23 +00:00
2017-11-02 18:55:46 +00:00
A true Bayesian rating system with scope and cache enabled.
2017-10-30 01:44:23 +00:00
## JS Rating?
This is **Raty**: https://github.com/wbotelhos/raty :star2:
## Description
Rating uses the know as "True Bayesian Estimate" inspired on [IMDb rating](http://www.imdb.com/help/show_leaf?votestopfaq) with the following formula:
```
(WR) = (v ÷ (v + m)) × R + (m ÷ (v + m)) × C
```
**IMDb Implementation:**
`WR`: weighted rating
`R`: average for the movie (mean) = (Rating)
`v`: number of votes for the movie = (votes)
`m`: minimum votes required to be listed in the Top 250
`C`: the mean vote across the whole report
**Rating Implementation:**
`WR`: weighted rating
2017-10-31 01:45:01 +00:00
`R`: average for the resource
2017-10-30 01:44:23 +00:00
2017-10-31 01:45:01 +00:00
`v`: number of votes for the resource
2017-10-30 01:44:23 +00:00
`m`: average of the number of votes
2017-10-31 01:45:01 +00:00
`C`: the average rating based on all resources
2017-10-30 01:44:23 +00:00
## Install
Add the following code on your `Gemfile` and run `bundle install`:
```ruby
gem 'rating'
```
Run the following task to create a Rating migration:
```bash
rails g rating:install
```
Then execute the migrations to create the to create tables `rating_rates` and `rating_ratings`:
```bash
rake db:migrate
```
## Usage
Just add the callback `rating` to your model:
```ruby
2018-01-16 03:14:09 +00:00
class Author < ApplicationRecord
2017-10-30 01:44:23 +00:00
rating
end
```
2017-11-02 18:55:46 +00:00
Now this model can vote or receive votes.
2017-10-30 01:44:23 +00:00
### rate
You can vote on some resource:
```ruby
2018-01-16 03:14:09 +00:00
author = Author.last
2017-10-30 01:44:23 +00:00
resource = Article.last
2018-01-26 19:46:10 +00:00
author.rate resource, 3
2017-10-30 01:44:23 +00:00
```
### rating
A voted resource exposes a cached data about it state:
```ruby
resource = Article.last
resource.rating
```
It will return a `Rating` object that keeps:
`average`: the normal mean of votes;
`estimate`: the true Bayesian estimate mean value (you should use this over average);
`sum`: the sum of votes for this resource;
`total`: the total of votes for this resource.
### rate_for
You can retrieve the rate of some author gave to some resource:
```ruby
2018-01-16 03:14:09 +00:00
author = Author.last
2017-10-30 01:44:23 +00:00
resource = Article.last
author.rate_for resource
```
It will return a `Rate` object that keeps:
`author`: the author of vote;
`resource`: the resource that received the vote;
`value`: the value of the vote.
### rated?
Maybe you want just to know if some author already rated some resource and receive `true` or `false`:
```ruby
2018-01-16 03:14:09 +00:00
author = Author.last
2017-10-30 01:44:23 +00:00
resource = Article.last
author.rated? resource
```
### rates
2019-11-27 16:00:11 +00:00
All rating received.
2017-10-30 01:44:23 +00:00
```ruby
2019-11-27 16:00:11 +00:00
Article.first.rates
2017-10-30 01:44:23 +00:00
```
It will return a collection of `Rate` object.
### rated
2019-11-27 16:00:11 +00:00
All rating given.
2017-10-30 01:44:23 +00:00
```ruby
2019-11-27 16:00:11 +00:00
Author.first.rated
2017-10-30 01:44:23 +00:00
```
It will return a collection of `Rate` object.
### order_by_rating
You can list resource ordered by rating data:
```ruby
Article.order_by_rating
```
It will return a collection of resource ordered by `estimate desc` as default.
The order column and direction can be changed:
```ruby
Article.order_by_rating :average, :asc
```
It will return a collection of resource ordered by `Rating` table data.
2017-11-02 18:55:46 +00:00
### Scope
All methods support scope query, since you may want to vote on items of a resource instead the resource itself.
Let's say an article belongs to one or more categories and you want to vote on some categories of this article.
```ruby
category_1 = Category.first
category_2 = Category.second
2018-01-16 03:14:09 +00:00
author = Author.last
2017-11-02 18:55:46 +00:00
resource = Article.last
```
In this situation you should scope the vote of article with some category:
**rate**
```ruby
2018-01-30 19:19:52 +00:00
author.rate resource, 3, scope: category_1
author.rate resource, 5, scope: category_2
2017-11-02 18:55:46 +00:00
```
2018-01-16 03:14:09 +00:00
Now `resource` has a rating for `category_1` and another one for `category_2`.
2017-11-02 18:55:46 +00:00
**rating**
2018-01-16 03:14:09 +00:00
Recovering the rating values for resource, we have:
2017-11-02 18:55:46 +00:00
```ruby
2018-01-16 03:14:09 +00:00
resource.rating
2017-11-02 18:55:46 +00:00
# nil
```
But using the scope to make the right query:
```ruby
2018-01-16 03:14:09 +00:00
resource.rating scope: category_1
2017-11-02 18:55:46 +00:00
# { average: 3, estimate: 3, sum: 3, total: 1 }
2018-01-16 03:14:09 +00:00
resource.rating scope: category_2
2017-11-02 18:55:46 +00:00
# { average: 5, estimate: 5, sum: 5, total: 1 }
```
**rated**
On the same way you can find your rates with a scoped query:
```ruby
2018-01-16 03:14:09 +00:00
author.rated scope: category_1
2017-11-02 18:55:46 +00:00
# { value: 3, scopeable: category_1 }
```
**rates**
The resource still have the power to consult its rates:
```ruby
article.rates scope: category_1
# { value: 3, scopeable: category_1 }
article.rates scope: category_2
# { value: 3, scopeable: category_2 }
```
**order_by_rating**
To order the rating you do the same thing:
```ruby
Article.order_by_rating scope: category_1
```
2018-03-01 21:34:17 +00:00
### Extra Scopes
Maybe you need to use more than one scope to make a rate, so you can use the `extra_scopes` options.
This feature is enable **only** to restrict the rate, the rating calculation will **ignore** it.
Example situation: I have a Profile (resource) that belongs to some Category (scope) and the Client (author) will rate
this Profile based on each Lead (extra scope) this Profile made. The Client can vote just one time on each lead, but many
times to that Profile. The Profile has a rating score based on all leads made on that Category.
```ruby
scope = Category.first
author = Client.last
resource = Profile.last
lead = Lead.last
author.rate resource, 5, extra_scopes: { lead_id: lead.id }, scope: scope
```
* The extra scopes fields is not present into gem, so you cannot use `{ lead: lead }`, for example.
2018-03-02 18:17:26 +00:00
All methods listed on [Scope](#scope) session allows `extra_scopes` as additional condition too.
2017-11-02 18:55:46 +00:00
### Records
Maybe you want to recover all records with or without scope, so you can add the suffix `_records` on relations:
```ruby
category_1 = Category.first
category_2 = Category.second
2018-01-16 03:14:09 +00:00
author = Author.last
2017-11-02 18:55:46 +00:00
resource = Article.last
author.rate resource, 1
2018-01-30 19:19:52 +00:00
author.rate resource, 3, scope: category_1
author.rate resource, 5, scope: category_2
2017-11-02 18:55:46 +00:00
author.rating_records
# { average: 1, estimate: 1, scopeable: nil , sum: 1, total: 1 },
# { average: 3, estimate: 3, scopeable: category_1, sum: 3, total: 1 },
# { average: 5, estimate: 5, scopeable: category_2, sum: 5, total: 1 }
2018-01-16 03:14:09 +00:00
author.rated_records
2017-11-02 18:55:46 +00:00
# { value: 1 }, { value: 3, scopeable: category_1 }, { value: 5, scopeable: category_2 }
article.rates_records
# { value: 1 }, { value: 3, scopeable: category_1 }, { value: 5, scopeable: category_2 }
```
2017-11-03 02:02:23 +00:00
### As
If you have a model that will only be able to rate but not to receive a rate, configure it as `author`.
2018-01-16 03:14:09 +00:00
An author model still can be rated, but won't genarate a Rating record with all values as zero to warm up the cache.
2017-11-03 02:02:23 +00:00
```ruby
rating as: :author
```
2018-01-26 19:46:25 +00:00
### Metadata
Maybe you want include a `comment` together your rating or even a `fingerprint` field to make your rating more secure.
So, first you will need to add more fields to the `Rating::Rate` table:
```ruby
class AddCommentAndFingerprintOnRatingRates < ActiveRecord::Migration
def change
add_column :rating_rates, :comment, :text
add_reference :rating_rates, :fingerprint, foreign_key: true, index: true, null: false
end
end
```
As you can seed, we can add any kind of field we want. Now we just provide this values when we make the rate:
```ruby
author = Author.last
resource = Article.last
comment = 'This is a very nice rating. s2'
fingerprint = Fingerprint.new(ip: '127.0.0.1')
author.rate resource, 3, metadata: { comment: comment, fingerprint: fingerprint }
```
Now you can have this data into your model normally:
```ruby
author = Author.last
rate = author.rates.last
rate.comment # 'This is a very nice rating. s2'
rate.fingerprint # <Fingerprint id:...>
rate.value # 3
```
2018-02-14 17:38:04 +00:00
### Scoping
If you need to warm up a record with scope, you need to setup the `scoping` relation.
```ruby
class Resource < ApplicationRecord
voting scoping: :categories
end
```
Now, when a resource is created, the cache will be generated for each related `category` as `scopeable`.
### Table Name
You can choose the table where Rating will write the data via YAML config.
You should just to provide a `config/rating.yml` file with the following content:
```yml
rating:
2018-03-02 18:17:26 +00:00
rate_table: reviews
rating_table: review_ratings
2018-02-14 17:38:04 +00:00
```
Now the rates will be written on `reviews` table over `rating_rates` and calculation will be on `review_ratings` over `rating_ratings`.
You can change one table o both of them.
2018-03-02 18:17:26 +00:00
### Validations
#### Rate Uniqueness
Since you can to use [Extra Scopes](#extra_scopes) to restrict rates and the original model `Rating::Rate` is inside gem, you can configure the uniqueness validation, from outside, to include this extra scopes.
```yml
rating:
validations:
rate:
uniqueness:
case_sensitive: false
scope:
- author_type
- resource_id
- resource_type
- scopeable_id
- scopeable_type
- scope_1
- scope_2
```
2018-03-05 23:34:10 +00:00
### Unscoped Rating
All rating values are grouped by the scope, but you can disable it and group all of them.
```ruby
rating unscoped_rating: true
author = User.last
resource = Article.last
scope = Category.last
author.rate resource, 1, scope: scope
author.rate resource, 2, scope: scope
author.rate resource, 3
```
Now the `sum` will be `6` and the `total` will be `3` because all rating will be calculated into just one rating record ignoring the `scopeable` object. The rating record is always saved on the record with `scopeable` as `nil`.
2018-06-14 16:44:34 +00:00
### References
2017-10-30 01:44:23 +00:00
2018-06-14 16:44:34 +00:00
- [Evan Miller](http://www.evanmiller.org/ranking-items-with-star-ratings.html)