Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dashboard in Rails

Let's say I had an app that was an address book. I'd like to have a page dedicated to a "dashboard". On this page, I'd like to have a running list of the events that happen within the app itself.

Event examples could be:

  1. A user adds a contact.
  2. A user deletes a contact.
  3. A user updates a contact.

What would be the best way to create this type of functionality? Originally I felt that I could do some creative database calls with existing data, but I wouldn't be able to deal with events that deleted data, like when a contact is deleted.

So now I'm thinking it would have to be a separate table that simply stored the events as they occurred. Would this be how most sites accomplish this?

I could go throughout my app, and each time a CRUD operation is performed I could create a new item in the table detailing what happened, but that doesn't seem very DRY.

I supposed my question would be - what's the best way to create the dashboard functionality within an already existing application such as an address book?

Any guidance would be greatly appreciated.

like image 892
Steve Avatar asked Feb 26 '10 02:02

Steve


2 Answers

The easiest way to do this is to user Observers in addition to a "logger" table in your database.

Logger
  id
  model_name
  model_id
  message

This way you can set up an Observer for all models that you want to log, and do something like this:

after_delete(contact)
  Logger.create({:model_name => contact.class.to_s,
                 :model_id => contact.id,
                 :message => "Contact was deleted at #{Time.now}"})
end

Now you can log any event in a way you deem fit. Another great addition to this kind of structure is to implement "Logical Deletes", which means you never really delete a record from the table, you simple give it a flag so that it no longer shows up in regular result sets. There's a plugin that does this called acts_as_paranoid.

If you implement both things above, the dashboard can log all important actions, and if you ever need to see what happened or view the data of those events, it's all in the system and can be accessed via the Console (or controllers, if you set them up).

like image 126
Mike Trpcic Avatar answered Oct 01 '22 00:10

Mike Trpcic


You may want to check out Timeline Fu: http://github.com/jamesgolick/timeline_fu:

class Post < ActiveRecord::Base
  belongs_to :author, :class_name => 'Person'
  fires :new_post, :on    => :create,
                 :actor => :author
end
like image 34
Gordon Isnor Avatar answered Sep 30 '22 22:09

Gordon Isnor