# mongoid-letsrate Rating Gem

Provides the best way to add rating capabilites to your Rails application with jQuery Raty plugin.

## Repository

[![Build Status](https://secure.travis-ci.org/malagant/mongoid-letsrate.png)](http://travis-ci.org/malagant/mongoid-letsrate)
[![Dependency Status](https://gemnasium.com/malagant/mongoid-letsrate.png)](https://gemnasium.com/malagant/mongoid-letsrate)
[![Code Climate](https://codeclimate.com/github/malagant/mongoid-letsrate.png)](https://codeclimate.com/github/malagant/mongoid-letsrate)

Find it at [github.com/malagant/mongoid-letsrate](https://github.com/malagant/mongoid-letsrate)

## Instructions

### Install

Add the mongoid-letsrate gem into your Gemfile

```ruby
gem 'mongoid-letsrate'
```

### Generate

```
rails g mongoid_letsrate User
```

The generator takes one argument which is the name of your existing devise user model UserModelName. This is necessary to bind the user and rating data.
Also the generator copies necessary files (jquery.raty plugin files, star icons and javascripts)

Example:

Suppose you will have a devise user model which name is User. The devise generator and mongoid-letsrate generator should be like below

```
rails g devise:install
rails g devise user

rails g mongoid_letsrate user # => user is the model generated by devise
```

This generator will create Rate and RatingCache models and link them to your user model.

### Prepare

Suppose you have a car model

```
rails g model car name:string
```

You should add the letsrate_rateable function.

```ruby
class Car
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Letsrate

  letsrate_rateable
end
```

Then you need to add a call letsrate_rater in the user model.

```ruby
class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Letsrate

  letsrate_rater
end
```

### Using

There is a helper method which name is rating_for to add the star links. By default rating_for will display the average rating and accept the
new rating value from authenticated users.

```erb
<%# show.html.erb -> /cars/1 %>

Rating : <%= rating_for @car %>
```

If you need to change the star number, you should use star option like below.

```erb
Rating : <%= rating_for @car, :star => 10 %>
```

You can use the rating_for_user helper method to show the star rating for the user.

```erb
Rating : <%= rating_for_user @car, current_user, :star => 10 %>
```


## Feedback
If you find bugs please open a ticket at [https://github.com/malagant/mongoid-letsrate/issues](https://github.com/malagant/mongoid-letsrate/issues)