All Projects → wbotelhos → rating

wbotelhos / rating

Licence: MIT license
⭐ A true Bayesian rating system with scope and cache enabled

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to rating

Laravel Love
Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
Stars: ✭ 822 (+1577.55%)
Mutual labels:  rating, vote, rate
Raty
🌟 Raty - A Star Rating Plugin
Stars: ✭ 2,292 (+4577.55%)
Mutual labels:  rating, vote, star-rating
rater-js
Star rating widget for the browser. Unlimited number of stars. No dependencies. No Jquery required.
Stars: ✭ 66 (+34.69%)
Mutual labels:  rating, vote, star-rating
Smart App Rate
An Android library that encourages users to rate the app on the Google Play.
Stars: ✭ 609 (+1142.86%)
Mutual labels:  rating, rate
react-star-ratings
A customizable svg star rating component for selecting x stars or visualizing x stars
Stars: ✭ 128 (+161.22%)
Mutual labels:  rating, star-rating
react-native-rating-element
A simple rating library for react native supporting: decimal points, direction aware icons, custom icon set from Ionicons, custom images and record rating given by users.
Stars: ✭ 19 (-61.22%)
Mutual labels:  rating, star-rating
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (+93.88%)
Mutual labels:  rating, rate
Laravel Reactions
Laravel reactions package for implementing reactions (eg: like, dislike, love, emotion, etc) on Eloquent models.
Stars: ✭ 58 (+18.37%)
Mutual labels:  rating, rate
Androidrate
AndroidRate is a library to help you promote your Android app by prompting users to rate the app after using it for a few days.
Stars: ✭ 117 (+138.78%)
Mutual labels:  rating, rate
yii2-vote
Provides voting for any model 👍 👎
Stars: ✭ 70 (+42.86%)
Mutual labels:  rating, vote
Material-Smart-Rating
😍⭐⭐Material Smart Rating App - An Android library that encourages users to rate the app on Google Play.⭐⭐😘
Stars: ✭ 30 (-38.78%)
Mutual labels:  rating, rate
Laravel Acquaintances
This package gives Eloquent models the ability to manage friendships (with groups), followships along with Likes, favorites..etc .
Stars: ✭ 449 (+816.33%)
Mutual labels:  vote, rate
Starrate
swift电商五星评价,星星评分控件,支持自定义数量、拖拽、间隔、设置最小星星数等操作
Stars: ✭ 85 (+73.47%)
Mutual labels:  rating, rate
Ngx Bar Rating
Angular Bar Rating
Stars: ✭ 146 (+197.96%)
Mutual labels:  rating, rate
Laravel-rating
Laravel package that allows you to rate, like & dislike and vote(+1,-1) your models with a simple and clear ways
Stars: ✭ 204 (+316.33%)
Mutual labels:  rating, vote
react-ratings-declarative
A customizable rating component for selecting x widgets or visualizing x widgets
Stars: ✭ 41 (-16.33%)
Mutual labels:  rating, star-rating
average-rating
Calculate average score and rating based on Wilson Score Equation
Stars: ✭ 28 (-42.86%)
Mutual labels:  rating
crypto-monitor
Monitor the crypto currency rate
Stars: ✭ 71 (+44.9%)
Mutual labels:  rate
vue-star-rating
⭐ A simple star rating component for vue apps
Stars: ✭ 30 (-38.78%)
Mutual labels:  star-rating
arbolito
A currency conversion api for the minimalist developer
Stars: ✭ 50 (+2.04%)
Mutual labels:  rate

Rating

CI Gem Version Maintainability codecov Sponsor

A true Bayesian rating system with scope and cache enabled.

JS Rating?

This is Raty: https://github.com/wbotelhos/raty 🌟

Description

Rating uses the know as "True Bayesian Estimate" inspired on IMDb rating 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

R: average for the resource

v: number of votes for the resource

m: average of the number of votes

C: the average rating based on all resources

Install

Add the following code on your Gemfile and run bundle install:

gem 'rating'

Run the following task to create the Rating migration:

rails g rating:install

Then execute the migrations to create the to create tables rating_rates and rating_ratings:

rake db:migrate

Usage

Just add the callback rating to your model:

class Author < ApplicationRecord
  rating
end

Now this model can vote or receive votes.

rate

You can vote on some resource:

author   = Author.last
resource = Article.last

author.rate resource, 3

rating

A voted resource exposes a cached data about it state:

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:

author   = Author.last
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:

author   = Author.last
resource = Article.last

author.rated? resource

rates

All rating received.

Article.first.rates

It will return a collection of Rate object.

rated

All rating given.

Author.first.rated

It will return a collection of Rate object.

order_by_rating

You can list resource ordered by rating data:

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:

Article.order_by_rating :average, :asc

It will return a collection of resource ordered by Rating table data.

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.

category_1 = Category.first
category_2 = Category.second
author     = Author.last
resource   = Article.last

In this situation you should scope the vote of article with some category:

rate

author.rate resource, 3, scope: category_1
author.rate resource, 5, scope: category_2

Now resource has a rating for category_1 and another one for category_2.

rating

Recovering the rating values for resource, we have:

resource.rating
# nil

But using the scope to make the right query:

resource.rating scope: category_1
# { average: 3, estimate: 3, sum: 3, total: 1 }

resource.rating scope: category_2
# { average: 5, estimate: 5, sum: 5, total: 1 }

rated

On the same way you can find your rates with a scoped query:

author.rated scope: category_1
# { value: 3, scopeable: category_1 }

rates

The resource still have the power to consult its rates:

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:

Article.order_by_rating scope: category_1

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.

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.

All methods listed on Scope session allows extra_scopes as additional condition too.

Records

Maybe you want to recover all records with or without scope, so you can add the suffix _records on relations:

category_1 = Category.first
category_2 = Category.second
author     = Author.last
resource   = Article.last

author.rate resource, 1
author.rate resource, 3, scope: category_1
author.rate resource, 5, scope: category_2

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 }

author.rated_records
# { 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 }

As

If you have a model that will only be able to rate but not to receive a rate, configure it as author. An author model still can be rated, but won't genarate a Rating record with all values as zero to warm up the cache.

rating as: :author

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:

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:

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:

author = Author.last
rate   = author.rates.last

rate.comment     # 'This is a very nice rating. s2'
rate.fingerprint # <Fingerprint id:...>
rate.value       # 3

Scoping

If you need to warm up a record with scope, you need to setup the scoping relation.

class Resource < ApplicationRecord
  rating 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:

rating:
  rate_table: reviews
  rating_table: review_ratings

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.

Validations

Rate Uniqueness

Since you can to use 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.

rating:
  validations:
    rate:
      uniqueness:
        case_sensitive: false

        scope:
          - author_type
          - resource_id
          - resource_type
          - scopeable_id
          - scopeable_type
          - scope_1
          - scope_2

Unscoped Rating

All rating values are grouped by its own scope, but you can disable it and group all of them together.

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.

where

The where option can be used to filter the Rating::Rate records used to create the final Rating::Rating. You can filter only approved rates, for exemplo:

rating where: 'approved = true'

author   = User.last
resource = Article.last

author.rate resource, 1, extra_scope: { approved: false }
author.rate resource, 5, extra_scope: { approved: true }

As you can see, now, only the rate with value 5 will be included on the final rating.

Specs

To run all specs, run the following rakes:

bundle exec rake spec
bundle exec rake spec_config
bundle exec rake spec_config_with_extra_scopes

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].