0

background: I am building a web app using Sinatra and ActiveRecord and I am keen to take advantage of acts_as_audited (as per https://github.com/collectiveidea/acts_as_audited). The docs for acts_as_audited assume I'll be using Rails and so assume I'll use Rails to generate the necessary migrations. I've not found any examples of using acts_as_audited with Sinatra.

So my question: Can someone point me at an example of using Sinatra and ActiveRecord with acts_as_audited?

Dave Sag
  • 13,266
  • 14
  • 86
  • 134

1 Answers1

2

I have been able to get this to work using the Audit.as_user method. Using this method lets you audit records as if the change were made by the user object you pass in.

Here is a simple example.

# This is my User model, I want to audit email address changes to it.
class User < ActiveRecord::Base
  acts_as_audited
  # user has :email attribute
  ...
end

# This is what I would call in my Sinatra code.
# user is an instance of my User class
...
Audit.as_user(user) do
  user.audit_comment = "updating email from sinatra"
  user.update_attribute(:email, 'foo@bar.com')
end
...

A more complex example...

# Now I have a User model and a Comments model and I 
# want to audit when I create a comment from Sinatra
class User < ActiveRecord::Base
  has_many :comments
  acts_as_audited
  ...
end

class Comment < ActiveRecord::Base
  belongs_to :user
  acts_as_audited
  # has a :body attribute
  ...
end

# This is what I would call in my Sinatra code.
# Again, user is an instance of my User class
...
Audit.as_user(user) do
  user.comments.create(
    :body => "Body of Comment", 
    :audit_comment => "Creating Comment from Sinatra"
  )
end
Ben Evans
  • 98
  • 1
  • 5
  • Thanks Ben. What Migrations would I need to add to my tables to support this? – Dave Sag Nov 06 '11 at 01:53
  • Sorry about the delay... for reference, the **audit_comment** is stored in the audits table, so once you have run `rails g acts_as_audited:install` and `rake db:migrate` for **acts_as_audited**, you should be good to go. To get the migration code that `rails g acts_as_audited:install` makes, take a look at the github repo: https://github.com/collectiveidea/acts_as_audited/blob/master/lib/generators/acts_as_audited/templates/install.rb – Ben Evans Jan 12 '12 at 00:44
  • so I'd need to install rails first I guess. – Dave Sag Jan 16 '12 at 02:47
  • My particular Sinatra project is using ActiveRecord to interact with models, and is tied to the same db backend as a Rails application, but you can see the actual model migration code in the git repo I linked above and create the appropriate tables however you choose. Since you said you are using ActiveRecord with your Sinatra app, I assumed you have the ability to run migrations from your app. The migration I reference is just the default migration needed to use acts_as_audited at all, so if you have that table created, then you should be good to go. – Ben Evans Jan 16 '12 at 23:17
  • cool. I will give that a go when I dive back into that project next week. cheers and thanks. – Dave Sag Jan 17 '12 at 05:02