acts_as_toucher

You can get more information on Bosko's blog.

0. License:

  "THE BEER-WARE LICENSE" (Revision 42):
   wrote this file. As long as you retain this notice
  you can do whatever you want with this stuff. If we meet some day, and you
  think this stuff is worth it, you can buy me a beer in return.
    -- Bosko Milekic 

1. Problem:

  You have two models in your domain and one interacts with the other in
  various ways.  You want to have a record of certain types of interactions
  that one model has with the other.

  For example, you have a User model and a File model.  You want a record
  of when a particular User views or edits a File.

2. Solution:

  Use acts_as_toucher (this plugin).

3. HowTo:

  First, create and run a Rails migration resembling the following:

class ActsAsRecentlyTouchedStructure < ActiveRecord::Migration
  def self.up
    create_table :touch_records do |t|
      t.column :created_at, :datetime
      t.column :touched_id, :integer
      t.column :touched_type, :string
      t.column :toucher_id, :integer
      t.column :toucher_type, :string
    end
  end

  def self.down
    drop_table :touch_records
  end
end

  Then, suppose we are talking about the above described User/File scenario.
  In such a scenario, you can do something like this from within your User
  class:

class User < ActiveRecord::Base
  acts_as_toucher :on => :file
end

  Then you can do this:

@user = User.find(1)
@file = File.find(1)
@file2 = File.find(2)
@user.touch_file(@file)
@user.touch_file(@file2)
@user.touch_file(@file)
@my_ten_recently_touched_files = @user.touched_files(10)

  It's also possible to specify an array/list for the :on option to
  acts_as_toucher.  For instance:

class User < ActiveRecord::Base
  acts_as_toucher :on => [ :file, :directory ]
end

  And so on...

4. Download

Grab 0.2 here.

5. Questions? Comments? Patch Submissions?

  I'd love to hear about how you're using this, and especially if you have
  changes.  Email: bosko.milekic at gmail.com