acts_as_rated

The ultimate rating system for ActiveRecord models. Highly flexible and
configurable, while easy to use with the defaults. Supports 3 different
ways to manage the statistics, and creates all the needed associations for
easy access to everything.

Comes complete with the needed migrations code to make it easy to add to
any project.

NOTE: It uses some advanced SQL constructs that might not be
supported by all servers. It was tested on Postgres only. If you have
patches/fixes for other databases, please send them and I will add them to
the plugin. UPDATE: Thanks to work done by Tiago Serafim it now
passes all but one tests on MySQL. And this test fails due to strangeness
in the avg() function in MySQL, according to Tiago.

Features

Rate any model

Optionally add fields to the rated objects to optimize speed

Optionally add an external rating statistics table with a record for each
rated model

Can work with the added fields, external table or just using direct SQL
count/avg calls

Use any model as the rater (defaults to User)

Limit the range of the ratings

Average, total and number of ratings

Find objects by ratings or rating ranges

Find objects by rater

Check if an object is rated by a specific rater. (Added by Tiago Serafim,
thanks!)

Migration

The file test/fixtures/migrations/001_add_rating_tables.rb shows
examples of all types of migration options.

See also the detailed documentation for the acts_as_rated method
on how to declare it, and the rest of the documentation for how to generate
the migration columns/files and how to use it.

classAddRatingTables<ActiveRecord::Migrationdefself.upActiveRecord::Base.create_ratings_table# Movies table has the columns for the ratings added while it's createdcreate_table(:movies)do|t|

t.column :title, :text Movie.generate_ratings_columns t

end# Cars table has the columns for the ratings added, but after the fact, using ALTER TABLE calls.# Usually used if the model already exist and we want to add the ratings after the factcreate_table(:cars)do|t|

t.column :title, :text

endCar.add_ratings_columnsenddefself.down# Remove the columns we addedCar.remove_ratings_columnsdrop_table:moviesrescuenildrop_table:carsrescuenilActiveRecord::Base.drop_ratings_tableendend

Testing the plugin

The plugin comes with a full set of tests, both for migrations and for the
code itself. The framework was taken from the acts_as_versioned plugin,
allowing it to run stand-alone in the test directory.

run the tests:

raketest

In order for testing to work, you need to create a database (default name
is acts_as_rated_plugin_test) and edit test/database.yml to make sure the
login and password are correct. You can also change there the name of the
database.

Testing defaults to postgresql, to change it set the environment variable
DB to the driver you want to use: