README.md in audited-4.6.0 vs README.md in audited-4.7.0

- old
+ new

@@ -10,13 +10,14 @@ ## Supported Rubies Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions: * 2.1.10 -* 2.2.8 -* 2.3.5 -* 2.4.2 +* 2.2.9 +* 2.3.6 +* 2.4.3 +* 2.5.0 Audited may work just fine with a Ruby version not listed above, but we can't guarantee that it will. If you'd like to maintain a Ruby that isn't listed, please let us know with a [pull request](https://github.com/collectiveidea/audited/pulls). ## Supported ORMs @@ -25,11 +26,11 @@ ## Installation Add the gem to your Gemfile: ```ruby -gem "audited", "~> 4.6" +gem "audited", "~> 4.7" ``` Then, from your Rails app directory, create the `audits` table: ```bash @@ -139,10 +140,37 @@ class User < ActiveRecord::Base audited :comment_required => true end ``` +### Limiting stored audits + +You can limit the number of audits stored for your model. To configure limiting for all audited models, put the following in an initializer: + +```ruby +Audited.max_audits = 10 # keep only 10 latest audits +``` + +or customize per model: + +```ruby +class User < ActiveRecord::Base + audited max_audits: 2 +end +``` + +Whenever an object is updated or destroyed, extra audits are combined with newer ones and the old ones are destroyed. + +```ruby +user = User.create!(name: "Steve") +user.audits.count # => 1 +user.update_attributes!(name: "Ryan") +user.audits.count # => 2 +user.destroy +user.audits.count # => 2 +``` + ### Current User Tracking If you're using Audited in a Rails application, all audited changes made within a request will automatically be attributed to the current user. By default, Audited uses the `current_user` method in your controller. ```ruby @@ -225,10 +253,36 @@ user.update_attribute!(name: "Steve Richert") user.audits.last.associated # => #<Company name: "Collective Idea"> company.associated_audits.last.auditable # => #<User name: "Steve Richert"> ``` +### Conditional auditing + +If you want to audit only under specific conditions, you can provide conditional options (similar to ActiveModel callbacks) that will ensure your model is only audited for these conditions. + +```ruby +class User < ActiveRecord::Base + audited if: :active? + + private + + def active? + last_login > 6.months.ago + end +end +``` + +Just like in ActiveModel, you can use an inline Proc in your conditions: + +```ruby +class User < ActiveRecord::Base + audited unless: Proc.new { |u| u.ninja? } +end +``` + +In the above case, the user will only be audited when `User#ninja` is `false`. + ### Disabling auditing If you want to disable auditing temporarily doing certain tasks, there are a few methods available. @@ -273,33 +327,9 @@ ```ruby # config/initializers/audited.rb Audited.config do |config| config.audit_class = CustomAudit -end -``` - -## Gotchas - -### Using attr_protected with Rails 4.x - -If you're using the `protected_attributes` gem with Rails 4.0, 4.1 or 4.2 (the gem isn't supported in Rails 5.0 or higher), you'll have to take an extra couple of steps to get `audited` working. - -First be sure to add `allow_mass_assignment: true` to your `audited` call; otherwise Audited will -interfere with `protected_attributes` and none of your `save` calls will work. - -```ruby -class User < ActiveRecord::Base - audited allow_mass_assignment: true -end -``` - -Second, be sure to add `audit_ids` to the list of protected attributes to prevent data loss. - -```ruby -class User < ActiveRecord::Base - audited allow_mass_assignment: true - attr_protected :logins, :audit_ids end ``` ## Support