audited 4.6.0 → 4.10.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of audited might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.rubocop.yml +25 -0
- data/.travis.yml +35 -21
- data/Appraisals +29 -12
- data/CHANGELOG.md +108 -0
- data/README.md +125 -39
- data/gemfiles/rails42.gemfile +3 -0
- data/gemfiles/rails50.gemfile +3 -0
- data/gemfiles/rails51.gemfile +3 -0
- data/gemfiles/rails52.gemfile +4 -2
- data/gemfiles/rails60.gemfile +10 -0
- data/gemfiles/rails61.gemfile +10 -0
- data/lib/audited.rb +2 -1
- data/lib/audited/audit.rb +31 -25
- data/lib/audited/auditor.rb +199 -39
- data/lib/audited/rspec_matchers.rb +70 -21
- data/lib/audited/version.rb +1 -1
- data/lib/generators/audited/templates/add_version_to_auditable_index.rb +21 -0
- data/lib/generators/audited/templates/install.rb +1 -1
- data/lib/generators/audited/upgrade_generator.rb +4 -0
- data/spec/audited/audit_spec.rb +88 -21
- data/spec/audited/auditor_spec.rb +450 -57
- data/spec/audited/rspec_matchers_spec.rb +69 -0
- data/spec/audited/sweeper_spec.rb +15 -6
- data/spec/audited_spec_helpers.rb +16 -2
- data/spec/rails_app/app/assets/config/manifest.js +1 -0
- data/spec/rails_app/app/controllers/application_controller.rb +2 -0
- data/spec/rails_app/config/application.rb +5 -0
- data/spec/rails_app/config/database.yml +1 -0
- data/spec/spec_helper.rb +4 -1
- data/spec/support/active_record/models.rb +50 -3
- data/spec/support/active_record/schema.rb +4 -2
- data/test/db/version_6.rb +2 -0
- data/test/test_helper.rb +1 -2
- data/test/upgrade_generator_test.rb +10 -0
- metadata +60 -22
- data/gemfiles/rails40.gemfile +0 -9
- data/gemfiles/rails41.gemfile +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e774f1f27e84aaa6f3ad9160eaba9b0e5eda0df9202cb5acaf405de13071c4d3
|
4
|
+
data.tar.gz: affeec219575b7032c73f03d69d839aeb7ad81ab7c53e2e636bea0243d66ac8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df852876685671788eeec0cd604a5771ed5bd096d466c10f1b9798774681c03749b2280bf4794d0cef707080c4f2e22464adbb90ac39f077132a0d3630c85c8c
|
7
|
+
data.tar.gz: bf555f179f6c33d4493aa984c1163e1ae2bcf7985b1ff7fecbe52012b3d2140c364098478aca57817c8991747a4009e4b184e63227621dc752cd8fe5ccf8a516
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.3
|
4
|
+
Exclude:
|
5
|
+
- lib/generators/audited/templates/**/*
|
6
|
+
- vendor/bundle/**/*
|
7
|
+
- gemfiles/vendor/bundle/**/*
|
8
|
+
|
9
|
+
Bundler/OrderedGems:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Gemspec/OrderedDependencies:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Layout:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Naming:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style:
|
25
|
+
Enabled: false
|
data/.travis.yml
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
language: ruby
|
2
2
|
cache: bundler
|
3
3
|
rvm:
|
4
|
-
- 2.
|
5
|
-
- 2.
|
6
|
-
- 2.
|
7
|
-
- 2.
|
4
|
+
- 2.3.7
|
5
|
+
- 2.4.4
|
6
|
+
- 2.5.1
|
7
|
+
- 2.6.3
|
8
8
|
- ruby-head
|
9
9
|
env:
|
10
10
|
- DB=SQLITE
|
@@ -12,36 +12,50 @@ env:
|
|
12
12
|
- DB=MYSQL
|
13
13
|
addons:
|
14
14
|
postgresql: "9.4"
|
15
|
+
services:
|
16
|
+
- mysql
|
17
|
+
before_install:
|
18
|
+
# https://github.com/travis-ci/travis-ci/issues/8978
|
19
|
+
- "travis_retry gem update --system"
|
20
|
+
# Rails 4.2 has a bundler 1.x requirement
|
21
|
+
- if [ $BUNDLE_GEMFILE = $PWD/gemfiles/rails42.gemfile ]; then
|
22
|
+
travis_retry gem install -v '1.17.3' bundler;
|
23
|
+
bundle _1.17.3_ install;
|
24
|
+
else
|
25
|
+
travis_retry gem install bundler;
|
26
|
+
fi
|
15
27
|
gemfile:
|
16
|
-
- gemfiles/rails40.gemfile
|
17
|
-
- gemfiles/rails41.gemfile
|
18
28
|
- gemfiles/rails42.gemfile
|
19
29
|
- gemfiles/rails50.gemfile
|
20
30
|
- gemfiles/rails51.gemfile
|
21
31
|
- gemfiles/rails52.gemfile
|
32
|
+
- gemfiles/rails60.gemfile
|
33
|
+
- gemfiles/rails61.gemfile
|
22
34
|
matrix:
|
23
|
-
|
24
|
-
- rvm:
|
35
|
+
include:
|
36
|
+
- rvm: 2.6.3
|
37
|
+
script: bundle exec rubocop --parallel
|
38
|
+
env: DB=rubocop # make travis build display nicer
|
25
39
|
exclude:
|
26
|
-
- rvm: 2.
|
27
|
-
gemfile: gemfiles/
|
28
|
-
- rvm: 2.
|
29
|
-
gemfile: gemfiles/
|
30
|
-
- rvm: 2.
|
31
|
-
gemfile: gemfiles/
|
32
|
-
- rvm: 2.4.
|
33
|
-
gemfile: gemfiles/
|
34
|
-
- rvm: 2.
|
35
|
-
gemfile: gemfiles/
|
40
|
+
- rvm: 2.3.7
|
41
|
+
gemfile: gemfiles/rails61.gemfile
|
42
|
+
- rvm: 2.4.4
|
43
|
+
gemfile: gemfiles/rails61.gemfile
|
44
|
+
- rvm: 2.3.7
|
45
|
+
gemfile: gemfiles/rails60.gemfile
|
46
|
+
- rvm: 2.4.4
|
47
|
+
gemfile: gemfiles/rails60.gemfile
|
48
|
+
- rvm: 2.6.3
|
49
|
+
gemfile: gemfiles/rails42.gemfile
|
36
50
|
- rvm: ruby-head
|
37
|
-
gemfile: gemfiles/
|
51
|
+
gemfile: gemfiles/rails42.gemfile
|
52
|
+
allow_failures:
|
38
53
|
- rvm: ruby-head
|
39
|
-
gemfile: gemfiles/rails41.gemfile
|
40
54
|
fast_finish: true
|
41
55
|
branches:
|
42
56
|
only:
|
43
57
|
- master
|
44
|
-
|
58
|
+
- /.*-stable$/
|
45
59
|
notifications:
|
46
60
|
webhooks:
|
47
61
|
urls:
|
data/Appraisals
CHANGED
@@ -1,28 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
gem 'protected_attributes'
|
4
|
-
gem 'test-unit'
|
5
|
-
end
|
6
|
-
|
7
|
-
appraise 'rails41' do
|
8
|
-
gem 'rails', '~> 4.1.0'
|
9
|
-
gem 'protected_attributes'
|
10
|
-
end
|
1
|
+
# Include DB adapters matching the version requirements in
|
2
|
+
# rails/activerecord/lib/active_record/connection_adapters/*adapter.rb
|
11
3
|
|
12
4
|
appraise 'rails42' do
|
13
5
|
gem 'rails', '~> 4.2.0'
|
14
6
|
gem 'protected_attributes'
|
7
|
+
gem "mysql2", ">= 0.3.13", "< 0.6.0"
|
8
|
+
gem "pg", "~> 0.15"
|
9
|
+
gem "sqlite3", "~> 1.3.6"
|
15
10
|
end
|
16
11
|
|
17
12
|
appraise 'rails50' do
|
18
13
|
gem 'rails', '~> 5.0.0'
|
14
|
+
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
15
|
+
gem "pg", ">= 0.18", "< 2.0"
|
16
|
+
gem "sqlite3", "~> 1.3.6"
|
19
17
|
end
|
20
18
|
|
21
19
|
appraise 'rails51' do
|
22
20
|
gem 'rails', '~> 5.1.4'
|
21
|
+
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
22
|
+
gem "pg", ">= 0.18", "< 2.0"
|
23
|
+
gem "sqlite3", "~> 1.3.6"
|
23
24
|
end
|
24
25
|
|
25
26
|
appraise 'rails52' do
|
26
|
-
gem 'rails', '>= 5.2.0
|
27
|
-
gem
|
27
|
+
gem 'rails', '>= 5.2.0', '< 5.3'
|
28
|
+
gem "mysql2", ">= 0.4.4", "< 0.6.0"
|
29
|
+
gem "pg", ">= 0.18", "< 2.0"
|
30
|
+
gem "sqlite3", "~> 1.3.6"
|
31
|
+
end
|
32
|
+
|
33
|
+
appraise 'rails60' do
|
34
|
+
gem 'rails', '>= 6.0.0', '< 6.1'
|
35
|
+
gem "mysql2", ">= 0.4.4"
|
36
|
+
gem "pg", ">= 0.18", "< 2.0"
|
37
|
+
gem "sqlite3", "~> 1.4"
|
38
|
+
end
|
39
|
+
|
40
|
+
appraise 'rails61' do
|
41
|
+
gem 'rails', '>= 6.1.0', '< 6.2'
|
42
|
+
gem "mysql2", ">= 0.4.4"
|
43
|
+
gem "pg", ">= 1.1", "< 2.0"
|
44
|
+
gem "sqlite3", "~> 1.4"
|
28
45
|
end
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,82 @@
|
|
2
2
|
|
3
3
|
## Unreleased
|
4
4
|
|
5
|
+
## 4.10.0 (2021-01-07)
|
6
|
+
|
7
|
+
Added
|
8
|
+
|
9
|
+
- Add redacted option
|
10
|
+
[#485](https://github.com/collectiveidea/audited/pull/485)
|
11
|
+
- Rails 6.1. support
|
12
|
+
[#554](https://github.com/collectiveidea/audited/pull/554)
|
13
|
+
[#559](https://github.com/collectiveidea/audited/pull/559)
|
14
|
+
|
15
|
+
Improved
|
16
|
+
|
17
|
+
- Avoid extra query on first audit version
|
18
|
+
[#513](https://github.com/collectiveidea/audited/pull/513)
|
19
|
+
|
20
|
+
|
21
|
+
## 4.9.0 (2019-07-17)
|
22
|
+
|
23
|
+
Breaking changes
|
24
|
+
|
25
|
+
- removed block support for `Audit.reconstruct_attributes`
|
26
|
+
[#437](https://github.com/collectiveidea/audited/pull/437)
|
27
|
+
- removed `audited_columns`, `non_audited_columns`, `auditing_enabled=` instance methods,
|
28
|
+
use class methods instead
|
29
|
+
[#424](https://github.com/collectiveidea/audited/pull/424)
|
30
|
+
- removed rails 4.1 and 4.0 support
|
31
|
+
[#431](https://github.com/collectiveidea/audited/pull/431)
|
32
|
+
|
33
|
+
Added
|
34
|
+
|
35
|
+
- Add `with_auditing` methods to enable temporarily
|
36
|
+
[#502](https://github.com/collectiveidea/audited/pull/502)
|
37
|
+
- Add `update_with_comment_only` option to control audit creation with only comments
|
38
|
+
[#327](https://github.com/collectiveidea/audited/pull/327)
|
39
|
+
- Support for Rails 6.0 and Ruby 2.6
|
40
|
+
[#494](https://github.com/collectiveidea/audited/pull/494)
|
41
|
+
|
42
|
+
Changed
|
43
|
+
|
44
|
+
- None
|
45
|
+
|
46
|
+
Fixed
|
47
|
+
|
48
|
+
- Ensure enum changes are stored consistently
|
49
|
+
[#429](https://github.com/collectiveidea/audited/pull/429)
|
50
|
+
|
51
|
+
## 4.8.0 (2018-08-19)
|
52
|
+
|
53
|
+
Breaking changes
|
54
|
+
|
55
|
+
- None
|
56
|
+
|
57
|
+
Added
|
58
|
+
|
59
|
+
- Add ability to globally disable auditing
|
60
|
+
[#426](https://github.com/collectiveidea/audited/pull/426)
|
61
|
+
- Add `own_and_associated_audits` method to auditable models
|
62
|
+
[#428](https://github.com/collectiveidea/audited/pull/428)
|
63
|
+
- Ability to nest `as_user` within itself
|
64
|
+
[#450](https://github.com/collectiveidea/audited/pull/450)
|
65
|
+
- Private methods can now be used for conditional auditing
|
66
|
+
[#454](https://github.com/collectiveidea/audited/pull/454)
|
67
|
+
|
68
|
+
Changed
|
69
|
+
|
70
|
+
- Add version to `auditable_index`
|
71
|
+
[#427](https://github.com/collectiveidea/audited/pull/427)
|
72
|
+
- Rename audited resource revision `version` attribute to `audit_version` and deprecate `version` attribute
|
73
|
+
[#443](https://github.com/collectiveidea/audited/pull/443)
|
74
|
+
|
75
|
+
Fixed
|
76
|
+
|
77
|
+
- None
|
78
|
+
|
79
|
+
## 4.7.1 (2018-04-10)
|
80
|
+
|
5
81
|
Breaking changes
|
6
82
|
|
7
83
|
- None
|
@@ -16,8 +92,40 @@ Changed
|
|
16
92
|
|
17
93
|
Fixed
|
18
94
|
|
95
|
+
- Allow use with Rails 5.2 final
|
96
|
+
|
97
|
+
## 4.7.0 (2018-03-14)
|
98
|
+
|
99
|
+
Breaking changes
|
100
|
+
|
19
101
|
- None
|
20
102
|
|
103
|
+
Added
|
104
|
+
|
105
|
+
- Add `inverse_of: auditable` definition to audit relation
|
106
|
+
[#413](https://github.com/collectiveidea/audited/pull/413)
|
107
|
+
- Add functionality to conditionally audit models
|
108
|
+
[#414](https://github.com/collectiveidea/audited/pull/414)
|
109
|
+
- Allow limiting number of audits stored
|
110
|
+
[#405](https://github.com/collectiveidea/audited/pull/405)
|
111
|
+
|
112
|
+
Changed
|
113
|
+
|
114
|
+
- Reduced db calls in `#revisions` method
|
115
|
+
[#402](https://github.com/collectiveidea/audited/pull/402)
|
116
|
+
[#403](https://github.com/collectiveidea/audited/pull/403)
|
117
|
+
- Update supported Ruby and Rails versions
|
118
|
+
[#404](https://github.com/collectiveidea/audited/pull/404)
|
119
|
+
[#409](https://github.com/collectiveidea/audited/pull/409)
|
120
|
+
[#415](https://github.com/collectiveidea/audited/pull/415)
|
121
|
+
[#416](https://github.com/collectiveidea/audited/pull/416)
|
122
|
+
|
123
|
+
Fixed
|
124
|
+
|
125
|
+
- Ensure that `on` and `except` options jive with `comment_required: true`
|
126
|
+
[#419](https://github.com/collectiveidea/audited/pull/419)
|
127
|
+
- Fix RSpec matchers
|
128
|
+
[#420](https://github.com/collectiveidea/audited/pull/420)
|
21
129
|
|
22
130
|
## 4.6.0 (2018-01-10)
|
23
131
|
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.svg)](http://travis-ci.org/collectiveidea/audited) [![
|
1
|
+
Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.svg)](http://travis-ci.org/collectiveidea/audited) [![Code Climate](https://codeclimate.com/github/collectiveidea/audited.svg)](https://codeclimate.com/github/collectiveidea/audited) [![Security](https://hakiri.io/github/collectiveidea/audited/master.svg)](https://hakiri.io/github/collectiveidea/audited/master)
|
2
2
|
=======
|
3
3
|
|
4
4
|
**Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited can also record who made those changes, save comments and associate models related to the changes.
|
5
5
|
|
6
|
-
Audited currently (4.x) works with Rails
|
6
|
+
Audited currently (4.x) works with Rails 6.1, Rails 6.0, 5.2, 5.1, 5.0 and 4.2.
|
7
7
|
|
8
8
|
For Rails 3, use gem version 3.0 or see the [3.0-stable branch](https://github.com/collectiveidea/audited/tree/3.0-stable).
|
9
9
|
|
@@ -11,10 +11,10 @@ For Rails 3, use gem version 3.0 or see the [3.0-stable branch](https://github.c
|
|
11
11
|
|
12
12
|
Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions:
|
13
13
|
|
14
|
-
* 2.
|
15
|
-
* 2.
|
16
|
-
* 2.
|
17
|
-
* 2.
|
14
|
+
* 2.3.7
|
15
|
+
* 2.4.4
|
16
|
+
* 2.5.1
|
17
|
+
* 2.6.3
|
18
18
|
|
19
19
|
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).
|
20
20
|
|
@@ -27,7 +27,7 @@ Audited is currently ActiveRecord-only. In a previous life, Audited worked with
|
|
27
27
|
Add the gem to your Gemfile:
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
gem "audited", "~> 4.
|
30
|
+
gem "audited", "~> 4.9"
|
31
31
|
```
|
32
32
|
|
33
33
|
Then, from your Rails app directory, create the `audits` table:
|
@@ -37,7 +37,9 @@ $ rails generate audited:install
|
|
37
37
|
$ rake db:migrate
|
38
38
|
```
|
39
39
|
|
40
|
-
If you're using PostgreSQL, then you can use `rails generate audited:install --audited-changes-column-type jsonb` (or `json`) to store audit changes natively with
|
40
|
+
By default changes are stored in YAML format. If you're using PostgreSQL, then you can use `rails generate audited:install --audited-changes-column-type jsonb` (or `json` for MySQL 5.7+ and Rails 5+) to store audit changes natively with database JSON column types.
|
41
|
+
|
42
|
+
If you're using something other than integer primary keys (e.g. UUID) for your User model, then you can use `rails generate audited:install --audited-user-id-column-type uuid` to customize the `audits` table `user_id` column type.
|
41
43
|
|
42
44
|
#### Upgrading
|
43
45
|
|
@@ -66,7 +68,7 @@ By default, whenever a user is created, updated or destroyed, a new audit is cre
|
|
66
68
|
```ruby
|
67
69
|
user = User.create!(name: "Steve")
|
68
70
|
user.audits.count # => 1
|
69
|
-
user.
|
71
|
+
user.update!(name: "Ryan")
|
70
72
|
user.audits.count # => 2
|
71
73
|
user.destroy
|
72
74
|
user.audits.count # => 3
|
@@ -75,7 +77,7 @@ user.audits.count # => 3
|
|
75
77
|
Audits contain information regarding what action was taken on the model and what changes were made.
|
76
78
|
|
77
79
|
```ruby
|
78
|
-
user.
|
80
|
+
user.update!(name: "Ryan")
|
79
81
|
audit = user.audits.last
|
80
82
|
audit.action # => "update"
|
81
83
|
audit.audited_changes # => {"name"=>["Steve", "Ryan"]}
|
@@ -129,7 +131,7 @@ end
|
|
129
131
|
You can attach comments to each audit using an `audit_comment` attribute on your model.
|
130
132
|
|
131
133
|
```ruby
|
132
|
-
user.
|
134
|
+
user.update!(name: "Ryan", audit_comment: "Changing name, just because")
|
133
135
|
user.audits.last.comment # => "Changing name, just because"
|
134
136
|
```
|
135
137
|
|
@@ -141,6 +143,41 @@ class User < ActiveRecord::Base
|
|
141
143
|
end
|
142
144
|
```
|
143
145
|
|
146
|
+
You can update an audit only if audit_comment is present. You can optionally add the `:update_with_comment_only` option set to `false` to your `audited` call to turn this behavior off for all audits.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class User < ActiveRecord::Base
|
150
|
+
audited :update_with_comment_only => false
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
### Limiting stored audits
|
155
|
+
|
156
|
+
You can limit the number of audits stored for your model. To configure limiting for all audited models, put the following in an initializer:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
Audited.max_audits = 10 # keep only 10 latest audits
|
160
|
+
```
|
161
|
+
|
162
|
+
or customize per model:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
class User < ActiveRecord::Base
|
166
|
+
audited max_audits: 2
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
Whenever an object is updated or destroyed, extra audits are combined with newer ones and the old ones are destroyed.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
user = User.create!(name: "Steve")
|
174
|
+
user.audits.count # => 1
|
175
|
+
user.update!(name: "Ryan")
|
176
|
+
user.audits.count # => 2
|
177
|
+
user.destroy
|
178
|
+
user.audits.count # => 2
|
179
|
+
```
|
180
|
+
|
144
181
|
### Current User Tracking
|
145
182
|
|
146
183
|
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.
|
@@ -165,16 +202,16 @@ Outside of a request, Audited can still record the user with the `as_user` metho
|
|
165
202
|
|
166
203
|
```ruby
|
167
204
|
Audited.audit_class.as_user(User.find(1)) do
|
168
|
-
post.
|
205
|
+
post.update!(title: "Hello, world!")
|
169
206
|
end
|
170
207
|
post.audits.last.user # => #<User id: 1>
|
171
208
|
```
|
172
209
|
|
173
210
|
The standard Audited install assumes your User model has an integer primary key type. If this isn't true (e.g. you're using UUID primary keys), you'll need to create a migration to update the `audits` table `user_id` column type. (See Installation above for generator flags if you'd like to regenerate the install migration.)
|
174
211
|
|
175
|
-
#### Custom
|
212
|
+
#### Custom Audit User
|
176
213
|
|
177
|
-
You might need to use a custom auditor from time to time.
|
214
|
+
You might need to use a custom auditor from time to time. This can be done by simply passing in a string:
|
178
215
|
|
179
216
|
```ruby
|
180
217
|
class ApplicationController < ActionController::Base
|
@@ -188,6 +225,25 @@ class ApplicationController < ActionController::Base
|
|
188
225
|
end
|
189
226
|
```
|
190
227
|
|
228
|
+
`as_user` also accepts a string, which can be useful for auditing updates made in a CLI environment:
|
229
|
+
|
230
|
+
```rb
|
231
|
+
Audited.audit_class.as_user("console-user-#{ENV['SSH_USER']}") do
|
232
|
+
post.update_attributes!(title: "Hello, world!")
|
233
|
+
end
|
234
|
+
post.audits.last.user # => 'console-user-username'
|
235
|
+
```
|
236
|
+
|
237
|
+
If you want to set a specific user as the auditor of the commands in a CLI environment, whether that is a string or an ActiveRecord object, you can use the following command:
|
238
|
+
|
239
|
+
```rb
|
240
|
+
Audited.store[:audited_user] = "username"
|
241
|
+
|
242
|
+
# or
|
243
|
+
|
244
|
+
Audited.store[:audited_user] = User.find(1)
|
245
|
+
```
|
246
|
+
|
191
247
|
### Associated Audits
|
192
248
|
|
193
249
|
Sometimes it's useful to associate an audit with a model other than the one being changed. For instance, given the following models:
|
@@ -222,11 +278,42 @@ Now, when an audit is created for a user, that user's company is also saved alon
|
|
222
278
|
```ruby
|
223
279
|
company = Company.create!(name: "Collective Idea")
|
224
280
|
user = company.users.create!(name: "Steve")
|
225
|
-
user.
|
281
|
+
user.update!(name: "Steve Richert")
|
226
282
|
user.audits.last.associated # => #<Company name: "Collective Idea">
|
227
283
|
company.associated_audits.last.auditable # => #<User name: "Steve Richert">
|
228
284
|
```
|
229
285
|
|
286
|
+
You can access records' own audits and associated audits in one go:
|
287
|
+
```ruby
|
288
|
+
company.own_and_associated_audits
|
289
|
+
```
|
290
|
+
|
291
|
+
### Conditional auditing
|
292
|
+
|
293
|
+
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.
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
class User < ActiveRecord::Base
|
297
|
+
audited if: :active?
|
298
|
+
|
299
|
+
private
|
300
|
+
|
301
|
+
def active?
|
302
|
+
last_login > 6.months.ago
|
303
|
+
end
|
304
|
+
end
|
305
|
+
```
|
306
|
+
|
307
|
+
Just like in ActiveModel, you can use an inline Proc in your conditions:
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
class User < ActiveRecord::Base
|
311
|
+
audited unless: Proc.new { |u| u.ninja? }
|
312
|
+
end
|
313
|
+
```
|
314
|
+
|
315
|
+
In the above case, the user will only be audited when `User#ninja` is `false`.
|
316
|
+
|
230
317
|
### Disabling auditing
|
231
318
|
|
232
319
|
If you want to disable auditing temporarily doing certain tasks, there are a few
|
@@ -258,6 +345,29 @@ To disable auditing on an entire model:
|
|
258
345
|
User.auditing_enabled = false
|
259
346
|
```
|
260
347
|
|
348
|
+
To disable auditing on all models:
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
Audited.auditing_enabled = false
|
352
|
+
```
|
353
|
+
|
354
|
+
If you have auditing disabled by default on your model you can enable auditing
|
355
|
+
temporarily.
|
356
|
+
|
357
|
+
```ruby
|
358
|
+
User.auditing_enabled = false
|
359
|
+
@user.save_with_auditing
|
360
|
+
```
|
361
|
+
|
362
|
+
or:
|
363
|
+
|
364
|
+
```ruby
|
365
|
+
User.auditing_enabled = false
|
366
|
+
@user.with_auditing do
|
367
|
+
@user.save
|
368
|
+
end
|
369
|
+
```
|
370
|
+
|
261
371
|
### Custom `Audit` model
|
262
372
|
|
263
373
|
If you want to extend or modify the audit model, create a new class that
|
@@ -278,30 +388,6 @@ Audited.config do |config|
|
|
278
388
|
end
|
279
389
|
```
|
280
390
|
|
281
|
-
## Gotchas
|
282
|
-
|
283
|
-
### Using attr_protected with Rails 4.x
|
284
|
-
|
285
|
-
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.
|
286
|
-
|
287
|
-
First be sure to add `allow_mass_assignment: true` to your `audited` call; otherwise Audited will
|
288
|
-
interfere with `protected_attributes` and none of your `save` calls will work.
|
289
|
-
|
290
|
-
```ruby
|
291
|
-
class User < ActiveRecord::Base
|
292
|
-
audited allow_mass_assignment: true
|
293
|
-
end
|
294
|
-
```
|
295
|
-
|
296
|
-
Second, be sure to add `audit_ids` to the list of protected attributes to prevent data loss.
|
297
|
-
|
298
|
-
```ruby
|
299
|
-
class User < ActiveRecord::Base
|
300
|
-
audited allow_mass_assignment: true
|
301
|
-
attr_protected :logins, :audit_ids
|
302
|
-
end
|
303
|
-
```
|
304
|
-
|
305
391
|
## Support
|
306
392
|
|
307
393
|
You can find documentation at: http://rdoc.info/github/collectiveidea/audited
|