jasherai / acts-as-rated Goto Github PK
View Code? Open in Web Editor NEWgit svn clone of acts-as-rated
Home Page: http://acts-as-rated.rubyforge.org
License: MIT License
git svn clone of acts-as-rated
Home Page: http://acts-as-rated.rubyforge.org
License: MIT License
= 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. <em>NOTE:</em> 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. <em>UPDATE:</em> 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!) * Extensively tested == Basic Details Install * script/plugin install svn://rubyforge.org/var/svn/acts-as-rated/trunk/acts_as_rated * gem install - <b>coming soon</b> Rubyforge project * http://rubyforge.org/projects/acts-as-rated RDocs * http://acts-as-rated.rubyforge.org Subversion * svn://rubyforge.org/var/svn/acts-as-rated Agile Web Development directory * http://www.agilewebdevelopment.com/plugins/acts_as_rated My blog with some comments about the plugin * http://devblog.famundo.com Work done as part of Famundo development * http://www.famundo.com Contact me at * [email protected] == Changes * V3 - Added improved find_by_rating as proposed by Ian McIntosh * V2 - Passing MySQL tests, check if rated by a specific rater as proposed by Tiago Serafim == TODO * Test with more databases * Test with other versions of Rails (tested against 1.2.1) * Add view helpers for easy display and entering of the ratings == Example of usage: === Simple rating system Look at the file <tt>test/rating_test.rb</tt> for many usage examples covering all variations of the plugin. class Book < ActiveRecord::Base acts_as_rated end bill = User.find_by_name 'bill' jill = User.find_by_name 'jill' catch22 = Book.find_by_title 'Catch 22' hobbit = Book.find_by_title 'Hobbit' catch22.rate 5, bill hobbit.rate 3, bill catch22.rate 1, jill hobbit.rate 5, jill hobbit.rating_average # => 4 hobbit.rated_total # => 8 hobbit.rated_count # => 2 hobbit.unrate bill hobbit.rating_average # => 5 hobbit.rated_total # => 5 hobbit.rated_count # => 1 bks = Book.find_by_rating 5 # => [hobbit] bks = Book.find_by_rating 1..5 # => [catch22, hobbit] usr = Book.find_rated_by jill # => [catch22, hobbit] === Migration The file <tt>test/fixtures/migrations/001_add_rating_tables.rb</tt> shows examples of all types of migration options. See also the detailed documentation for the <tt>acts_as_rated</tt> 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. class AddRatingTables < ActiveRecord::Migration def self.up ActiveRecord::Base.create_ratings_table # Movies table has the columns for the ratings added while it's created create_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 fact create_table(:cars) do |t| t.column :title, :text end Car.add_ratings_columns end def self.down # Remove the columns we added Car.remove_ratings_columns drop_table :movies rescue nil drop_table :cars rescue nil ActiveRecord::Base.drop_ratings_table end end == 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: rake test 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: env DB='mysql' rake test
I'm getting the following error:
irb(main):009:0> Article.find_rated_by(User.first)
NoMethodError: undefined method `class_name_of_active_record_descendant' for ActiveRecord::Base:Class
from C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/base.rb:1016:in `method_missing'
from C:/Users/rob/web/insite-rails-cms/vendor/plugins/jasherai-acts-as-rated/lib/acts_as_rated.rb:376:in `send'
from C:/Users/rob/web/insite-rails-cms/vendor/plugins/jasherai-acts-as-rated/lib/acts_as_rated.rb:376:in `find_rated_by'
from (irb):9
I can fix the error by changing line 376 in acts_as_rated.rb from:
rated_class = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
to:
rated_class = ActiveRecord::Base.send(:class_of_active_record_descendant, self).to_s
DEPRECATION WARNING: class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first. (called from acts_as_rated at /.../vendor/plugins/acts_as_rated/lib/acts_as_rated.rb:121)
Not quite sure how to fix this
undefined method `write_inheritable_attribute' for #Class:0xb3546574
activerecord (3.2.1) lib/active_record/dynamic_matchers.rb:50:in method_missing' vendor/plugins/acts_as_rated/lib/acts_as_rated.rb:116:in
acts_as_rated'
app/models/input.rb:5
Rewriting those two lines (116) as
class_attribute :acts_as_rated_options
self.acts_as_rated_options=
{ :rating_range => options[:rating_range],
:rating_class => rating_class,
:stats_class => stats_class,
:rater_class => rater_class }
Seems to fix it
It uses class_name in two places which is no longer available in ActiveRecord, however simply replacing class_name with base_class fixes it and it works fine under Rails3. Will issue a pull request for the fix
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.