views:

209

answers:

1

Hi guys,

I am using the acts_as_audited gem with my application. (Excellent gem to keep track of changes of model objects)

I have been asked to support associating a text comment with each Audit record (similar functionality to svn commit). And I am stumped on how to accomplish this.

For example. Lets say I have an address form, and the user updates City and State, upon hitting save I ask him to provide a Comment. I would like that comment information associated with the audit record that is created.

Make sense?

Thanks for your help,

Jonathan

+4  A: 

I thought this would be a useful thing. So I forked and patched the plugin myself. Who knows if and when it will be accepted upstream.

Install it as a plugin:

rails_root$ script/plugin install git://github.com/EmFi/acts_as_audited.git

Usage doesn't really change from vanilla acts_as_audited.

acts_as_audited takes an extra option now. :require_comment, which if true, blocks creation, updating, or destruction of an audited model unless a comment is supplied.

To add a comment to an audit use model.audit_comment= "My Comment" before create/update/destroy.

audit_comment can also be mass assigned making it simple to add a comment field to any form.

Before you can use my gem/plugin you'll need to update the audit table to contain a comment column. Sorry, but you're on your own for that, I haven't gotten around to writing a generator for it yet. If you don't mind wiping the audit table or don't have one yet, you can run script/generate audited_migration add_audits_table (after dropping an existing audit table of course)

With my gem/plugin all that would need to change using your address example adding an audit_comment field to your form.

<%form_for @address do |f| %>
  ... standard address fields
  <%= f.label_for :audit_comment %>
  <%= f.text_field :audit_comment %>
<% end %>

EDIT: Now has a complete test suite.

EmFi
Amazing!!!! Talk about going above and beyond. Thanks a bunch, that worked perfectly
Jonathan
You're welcome. I realized about halfway through a response that I'd already done most of the work. It seemed like a useful enough thing that it only made sense to make it public.
EmFi