forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 4
Auditing via paper_trail (change history)
dickeytk edited this page Oct 20, 2011
·
14 revisions
paper_trail is a gem used for auditing and versioning of your models. We can easily use it with active_admin to show a list of recently modified items in the admin screen.
Example:
-
Install PaperTrail as a gem via your
Gemfile
:gem 'paper_trail'
-
Generate a migration which will add a
versions
table to your database:rails generate paper_trail:install
-
Run the migration:
rake db:migrate
-
Add
user_for_paper_trail
to controller so paper_trail knows which user updated the item. (optional)
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# ...
protected
def user_for_paper_trail
admin_user_signed_in? ? current_admin_user : 'Unknown user'
end
# ...
end
- Add
has_paper_trail
to the models you want to track:
# app/models/post.rb
class Post < ActiveRecord::Base
has_paper_trail
# ...
end
- Display the versions table on the dashboard
# app/admin/dashboard.rb
section "Recently updated content" do
table_for Version.order('id desc').limit(20) do
column "Item" do |v| v.item end
column "Type" do |v| v.item_type.underscore.humanize end
column "Modified at" do |v| v.created_at.to_s :long end
column "Admin" do |v| link_to AdminUser.find(v.whodunnit), admin_admin_path(v.whodunnit) end
end
end
To link the item
column to your model in active_admin, I added the following method to my model:
# app/models/post.rb
class Post < ActiveRecord::Base
include Rails.application.routes.url_helpers # neeeded for _path helpers to work in models
has_paper_trail
# ...
def admin_permalink
admin_post_path(self)
end
# ...
end
Then in app/admin/dashboard.rb
:
column "Item" do |v| link_to v.item, v.item.admin_permalink end
TODO