February 29th, 2008
ActsAsReadable - Drop-in 'mark as read' functionality
When writing an application there’s a number of times where it can be very useful to know whether or not a user has seen or accessed a piece of information. I recently had to write a solution to such a need and have wrapped up the result in a plugin for your enjoyment.
ActsAsReadable allows you to create a generic relationship of items which can be marked as ‘read’ by users. This is useful for forums or any other kind of situation where you might need to know whether or not a user has seen a particular model.
Installation
To install the plugin just install from the GitHub repository:
git clone git://github.com/mbleigh/acts-as-readable.git vendor/plugins/acts_as_readable
You will need the readings table to use this plugin. A generator has been included, simply type
script/generate acts_as_readable_migration
to get the standard migration created for you.
Example
class Post < ActiveRecord::Base
acts_as_readable
end
bob = User.find_by_name("bob")
bob.readings # => []
Post.find_unread_by(bob) # => [<Post 1>,<Post 2>,<Post 3>...]
Post.find_read_by(bob) # => []
Post.find(1).read_by?(bob) # => false
Post.find(1).read_by!(bob) # => <Reading 1>
Post.find(1).read_by?(bob) # => true
Post.find(1).users_who_read # => [<User bob>]
Post.find_unread_by(bob) # => [<Post 2>,<Post 3>...]
Post.find_read_by(bob) # => [<Post 1>]
bob.readings # => [<Reading 1>]
And that’s all there is to it! It’s not an incredibly complex set of features, but I find it to be a pretty useful one. If you have any questions or issues, please feel free to post them on the public Trac
May 28th, 2008 at 08:27 AM
Hello misterbleight !
Just one word to say that your plugin is nice and useful. And my 2cts of help: the server may crash during launch because it fails adding acts_as_readable methods into User saying that “method x can’t be found”. I think it’s because in the natural order acts_as_readable comes before has_attachement or restful_auth for example, so to correct that just do something like that in your environement.rb : config.plugins = [ :all, :acts_as_readable ]