开发者

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开发者_如何学Python'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.


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).


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


I've created similar functionality in the past using acts_as_audited.

This helps you track changes to your models, which you can then present to the user.

It basically just tracks the events in a separate table, as you suggested.


You can use Observers in order to handle the events.

Then just store event with the information needed in the database from those Observers.

Here is a quick link to get you started.


user paper_trail plugin, it is awesome!. We modified it though, it is used for all our audit system for complicated release process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜