Giter Site home page Giter Site logo

rating_star's Introduction

RatingStar ★★★★☆

This gem is in early alpha. No guarantee, it will install or work at all, at the moment.

This extension adds easy and straight-forward rating functionality to your Rails app. It also comes with pre-defined views, for the ratings to be displayed as a set of stars.

Setup

Installation

# Gemfile
gem 'rating_star', git: 'https://github.com/michaelgrohn/rating_star.git'
$ bundle

Migration

$ rails generate ratings
$ rake db:migrate

Quickstart

# app/models/movie.rb
is_rateable
@movie = Movie.find_by( title: 'The Dark Knight' )
@movie.ratings.create( value: 0.9 )
# app/views/movies/show.html.haml
# ⋮
= render @movie.rating
# ⋮

This will render something like this on your webpage:

Usage

Models and Controllers

Specify attributions, to easily reference the correspondending model later (not mandatory):

# app/models/movie.rb
is_rateable by: :users
# app/models/book.rb
is_rateable by: [ :users, :readers,  ]
# app/models/user.rb
is_rater for: [ :movies, :books ]

Accessing .rating on a model gives you the average rating, based on all votes. The ratings .value is the actual rating value and ranges form 0..1. .vote_count is the count of given ratings. If it is greater than 1, the rating represents an average rating.

@movie.rating.vote_count
#=> 1
@movie.rating.value
#=> 0.9

@movie.ratings.create( value: 0.8 )
@movie.rating.vote_count
#=> 2
@movie.rating.value
#=> 0.85

Ways to access your models:

@movie.ratings
#=> a collection of all ratings
@movie.rating
#=> the average rating (a new object)

@user = User.first
@rating = @movie.ratings.create( value: 1.0, rater: @user )

@rating.rater
#=> the object that gave this rating (nil if not specified)
@rating.item
#=> the object that is being rated

# For the following accessors, you must specifiy is_rateable by: :users, is_rater for: [ :movies, :books ] etc.
@movie.rating_users
#=> a collection of all the users that gave this movie a rating
@user.rated_movies
#=> a collection of all movies, that this user has rated
@user.rated_books
#=> a collection of all books, that this user has rated
@user.rated_books( min: 0.5, max: 0.8 )
#=> a collection of all books, that this user has rated with a rating between 0.5 and 0.8

Views

Simply render the rating as stars:

= render @movie.rating # 5 stars default
= render @movie.rating, scale: 4 # 4 stars system
= render @movie.rating, scale: 11 # ours goes up to 11! 

Enable rating by clicking on the stars:

= render @movie.rating, rateable: true
= render @movie.rating, rateable: true, rater: current_user

Show additional information under the stars:

= render @movie.rating, with_vote_count: true
= render @movie.rating, with_value: true # adds "4.5 out of 5"

Contributing

  1. Fork it ( https://github.com/[my-github-username]/rating_star/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

rating_star's People

Contributors

michaelgrohn avatar

Watchers

 avatar

rating_star's Issues

Add configuration

  • Pick rater model name (rater should be polymorphic!)
  • Defaul Display Options
    • with_votes (==> @rating.vote_count!)
    • votes.parenthesize
    • with_value
    • value.numerator
    • value.seperator
    • value.denominator
    • star_count
    • color
    • different display styles
    • half-stars for display?
    • half-stars for input?
    • templating engine: haml vs. erb & scss vs. sass (get from conig)
    • schema.org
    • uncertainty

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.