You are here: Browse Railsplugins Acts As Viewed
= acts_as_viewed
The ultimate viewing system for ActiveRecord models. This plugin allow to keep the view count of objects. The views are only counted one time by IP address or User. The viewer class can be changed from User to whatever you want. Highly flexible and configurable, while easy to use with the defaults. 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: the code are based in the excellent acts_as_rated plugin.
Features=== Simple rating system
class Movie < ActiveRecord::Base
acts_as_viewed
end
In a controller:
bill = User.find_by_name 'bill'
batman = Movie.find_by_title 'Batman'
toystory = Movie.find_by_title 'Toy Story'
batman.view request.remote_ip, bill
toystory.view request.remote_ip, bill
batman.view_count # => 1
=== Migration See also the detailed documentation for the acts_as_viewed 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 AddViewingTables < ActiveRecord::Migration
def self.up
ActiveRecord::Base.create_viewings_table
end
def self.down
# Remove the columns we added
Movie.remove_viewings_columns
end
drop_table :movies rescue nil
ActiveRecord::Base.drop_viewings_table
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly