3. 1. Introduction
Audited (previously acts_as_audited) is an
ORM extension that logs all changes to your
models. Audited also allows you to record who
made those changes, save comments and
associate models related to the changes. Audited
works with Rails 3.
Supported Rubies: 1.8.7, 1.9.2, 1.9.3 ...
Supported ORMs
5. 3. Usage
- Add “audited” into Model file.
class User < ActiveRecord::Base
audited
End
6. Specifying columns
class User < ActiveRecord::Base
# All fields
# audited
# Single field
# audited only: :name
# Multiple fields
# audited only: [:name, :address]
# All except certain fields
# audited except: :password
end
7. Comments
user.update_attributes!(:name => "Ryan",
:audit_comment => "Changing name, just
because")
You can optionally add the :comment_required option to
your audited call to require comments for all audits.
class User < ActiveRecord::Base
audited :comment_required => true
end
8. Current User Tracking
s PostsController < ApplicationController
f create
urrent_user # => #<User name: "Steve">
@post = Post.create(params[:post])
@post.audits.last.user # => #<User name: "Steve">
d
use a method other than current_user, put the following in an intializer:
dited.current_user_method = :authenticated_user
tside of a request, Audited can still record the user with the as_user metho
dit.as_user(User.find(1)) do
ost.update_attribute!(:title => "Hello, world!")
d
st.audits.last.user # => #<User id: 1>
9. Associated Audits
class User < ActiveRecord::Base
belongs_to :company
audited :associated_with => :company
end
class Company < ActiveRecord::Base
has_many :users
has_associated_audits
end
10. Accessible Attributes
class User < ActiveRecord::Base
audited :allow_mass_assignment => true
end
class User < ActiveRecord::Base
audited :allow_mass_assignment => true
end