audited 4.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bd000e9aee82f6d9bc8f26875cf255276e1c03c4
4
- data.tar.gz: 71228c6fd0026f14f1c79ffc092fa212c680ec12
2
+ SHA256:
3
+ metadata.gz: e774f1f27e84aaa6f3ad9160eaba9b0e5eda0df9202cb5acaf405de13071c4d3
4
+ data.tar.gz: affeec219575b7032c73f03d69d839aeb7ad81ab7c53e2e636bea0243d66ac8f
5
5
  SHA512:
6
- metadata.gz: 85c69841d137bc726c684589a1b4ec6e2352fa3f1751cd20c38431f66dd67c459766c842f7582cda9cabfadd235d3c1b2b37548408a23118d14b194ac45e6f1b
7
- data.tar.gz: 1c1a0854e666f9255972563fe2c32bb708fd1f4ad271144d1062a765c22a40d929a8bc2301278ae30bb162d214b19ea24046c35478f10c56af2748ef7205582a
6
+ metadata.gz: df852876685671788eeec0cd604a5771ed5bd096d466c10f1b9798774681c03749b2280bf4794d0cef707080c4f2e22464adbb90ac39f077132a0d3630c85c8c
7
+ data.tar.gz: bf555f179f6c33d4493aa984c1163e1ae2bcf7985b1ff7fecbe52012b3d2140c364098478aca57817c8991747a4009e4b184e63227621dc752cd8fe5ccf8a516
data/.gitignore CHANGED
@@ -9,7 +9,6 @@
9
9
  .ruby-version
10
10
  .rvmrc
11
11
  .yardoc
12
- coverage/
13
12
  doc/
14
13
  Gemfile.lock
15
14
  gemfiles/*.lock
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,11 +1,10 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
- - 2.1
5
- - 2.2.9
6
- - 2.3.6
7
- - 2.4.3
8
- - 2.5.0
4
+ - 2.3.7
5
+ - 2.4.4
6
+ - 2.5.1
7
+ - 2.6.3
9
8
  - ruby-head
10
9
  env:
11
10
  - DB=SQLITE
@@ -13,44 +12,50 @@ env:
13
12
  - DB=MYSQL
14
13
  addons:
15
14
  postgresql: "9.4"
15
+ services:
16
+ - mysql
16
17
  before_install:
17
18
  # https://github.com/travis-ci/travis-ci/issues/8978
18
19
  - "travis_retry gem update --system"
19
- - "travis_retry gem install bundler"
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
20
27
  gemfile:
21
- - gemfiles/rails40.gemfile
22
- - gemfiles/rails41.gemfile
23
28
  - gemfiles/rails42.gemfile
24
29
  - gemfiles/rails50.gemfile
25
30
  - gemfiles/rails51.gemfile
26
31
  - gemfiles/rails52.gemfile
32
+ - gemfiles/rails60.gemfile
33
+ - gemfiles/rails61.gemfile
27
34
  matrix:
28
- allow_failures:
29
- - rvm: ruby-head
35
+ include:
36
+ - rvm: 2.6.3
37
+ script: bundle exec rubocop --parallel
38
+ env: DB=rubocop # make travis build display nicer
30
39
  exclude:
31
- - rvm: 2.1
32
- gemfile: gemfiles/rails50.gemfile
33
- - rvm: 2.1
34
- gemfile: gemfiles/rails51.gemfile
35
- - rvm: 2.1
36
- gemfile: gemfiles/rails52.gemfile
37
- - rvm: 2.4.3
38
- gemfile: gemfiles/rails40.gemfile
39
- - rvm: 2.4.3
40
- gemfile: gemfiles/rails41.gemfile
41
- - rvm: 2.5.0
42
- gemfile: gemfiles/rails40.gemfile
43
- - rvm: 2.5.0
44
- gemfile: gemfiles/rails41.gemfile
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
45
50
  - rvm: ruby-head
46
- gemfile: gemfiles/rails40.gemfile
51
+ gemfile: gemfiles/rails42.gemfile
52
+ allow_failures:
47
53
  - rvm: ruby-head
48
- gemfile: gemfiles/rails41.gemfile
49
54
  fast_finish: true
50
55
  branches:
51
56
  only:
52
57
  - master
53
- sudo: false
58
+ - /.*-stable$/
54
59
  notifications:
55
60
  webhooks:
56
61
  urls:
data/Appraisals CHANGED
@@ -1,28 +1,45 @@
1
- appraise 'rails40' do
2
- gem 'rails', '~> 4.0.0'
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
27
  gem 'rails', '>= 5.2.0', '< 5.3'
27
- gem 'mysql2', '~> 0.4.4'
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
@@ -1,5 +1,53 @@
1
1
  # Audited ChangeLog
2
2
 
3
+ ## Unreleased
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
+
3
51
  ## 4.8.0 (2018-08-19)
4
52
 
5
53
  Breaking changes
@@ -26,7 +74,7 @@ Changed
26
74
 
27
75
  Fixed
28
76
 
29
- - None
77
+ - None
30
78
 
31
79
  ## 4.7.1 (2018-04-10)
32
80
 
@@ -40,8 +88,7 @@ Added
40
88
 
41
89
  Changed
42
90
 
43
- - Change/deprecate `version` attribute to `audit_version` attribute
44
- [#443](https://github.com/collectiveidea/audited/pull/443)
91
+ - None
45
92
 
46
93
  Fixed
47
94
 
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) [![Dependency Status](https://gemnasium.com/collectiveidea/audited.svg)](https://gemnasium.com/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)
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 5.2, 5.1, 5.0 and 4.2. It may work with 4.1 and 4.0, but this is not guaranteed.
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,11 +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.1.10
15
- * 2.2.9
16
- * 2.3.6
17
- * 2.4.3
18
- * 2.5.0
14
+ * 2.3.7
15
+ * 2.4.4
16
+ * 2.5.1
17
+ * 2.6.3
19
18
 
20
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).
21
20
 
@@ -28,7 +27,7 @@ Audited is currently ActiveRecord-only. In a previous life, Audited worked with
28
27
  Add the gem to your Gemfile:
29
28
 
30
29
  ```ruby
31
- gem "audited", "~> 4.7"
30
+ gem "audited", "~> 4.9"
32
31
  ```
33
32
 
34
33
  Then, from your Rails app directory, create the `audits` table:
@@ -38,7 +37,9 @@ $ rails generate audited:install
38
37
  $ rake db:migrate
39
38
  ```
40
39
 
41
- 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 its JSON column types. 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.
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.
42
43
 
43
44
  #### Upgrading
44
45
 
@@ -67,7 +68,7 @@ By default, whenever a user is created, updated or destroyed, a new audit is cre
67
68
  ```ruby
68
69
  user = User.create!(name: "Steve")
69
70
  user.audits.count # => 1
70
- user.update_attributes!(name: "Ryan")
71
+ user.update!(name: "Ryan")
71
72
  user.audits.count # => 2
72
73
  user.destroy
73
74
  user.audits.count # => 3
@@ -76,7 +77,7 @@ user.audits.count # => 3
76
77
  Audits contain information regarding what action was taken on the model and what changes were made.
77
78
 
78
79
  ```ruby
79
- user.update_attributes!(name: "Ryan")
80
+ user.update!(name: "Ryan")
80
81
  audit = user.audits.last
81
82
  audit.action # => "update"
82
83
  audit.audited_changes # => {"name"=>["Steve", "Ryan"]}
@@ -130,7 +131,7 @@ end
130
131
  You can attach comments to each audit using an `audit_comment` attribute on your model.
131
132
 
132
133
  ```ruby
133
- user.update_attributes!(name: "Ryan", audit_comment: "Changing name, just because")
134
+ user.update!(name: "Ryan", audit_comment: "Changing name, just because")
134
135
  user.audits.last.comment # => "Changing name, just because"
135
136
  ```
136
137
 
@@ -142,6 +143,14 @@ class User < ActiveRecord::Base
142
143
  end
143
144
  ```
144
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
+
145
154
  ### Limiting stored audits
146
155
 
147
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:
@@ -163,7 +172,7 @@ Whenever an object is updated or destroyed, extra audits are combined with newer
163
172
  ```ruby
164
173
  user = User.create!(name: "Steve")
165
174
  user.audits.count # => 1
166
- user.update_attributes!(name: "Ryan")
175
+ user.update!(name: "Ryan")
167
176
  user.audits.count # => 2
168
177
  user.destroy
169
178
  user.audits.count # => 2
@@ -193,16 +202,16 @@ Outside of a request, Audited can still record the user with the `as_user` metho
193
202
 
194
203
  ```ruby
195
204
  Audited.audit_class.as_user(User.find(1)) do
196
- post.update_attributes!(title: "Hello, world!")
205
+ post.update!(title: "Hello, world!")
197
206
  end
198
207
  post.audits.last.user # => #<User id: 1>
199
208
  ```
200
209
 
201
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.)
202
211
 
203
- #### Custom Auditor
212
+ #### Custom Audit User
204
213
 
205
- You might need to use a custom auditor from time to time. It can be done by simply passing in a string:
214
+ You might need to use a custom auditor from time to time. This can be done by simply passing in a string:
206
215
 
207
216
  ```ruby
208
217
  class ApplicationController < ActionController::Base
@@ -216,6 +225,25 @@ class ApplicationController < ActionController::Base
216
225
  end
217
226
  ```
218
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
+
219
247
  ### Associated Audits
220
248
 
221
249
  Sometimes it's useful to associate an audit with a model other than the one being changed. For instance, given the following models:
@@ -250,7 +278,7 @@ Now, when an audit is created for a user, that user's company is also saved alon
250
278
  ```ruby
251
279
  company = Company.create!(name: "Collective Idea")
252
280
  user = company.users.create!(name: "Steve")
253
- user.update_attribute!(name: "Steve Richert")
281
+ user.update!(name: "Steve Richert")
254
282
  user.audits.last.associated # => #<Company name: "Collective Idea">
255
283
  company.associated_audits.last.auditable # => #<User name: "Steve Richert">
256
284
  ```
@@ -323,6 +351,23 @@ To disable auditing on all models:
323
351
  Audited.auditing_enabled = false
324
352
  ```
325
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
+
326
371
  ### Custom `Audit` model
327
372
 
328
373
  If you want to extend or modify the audit model, create a new class that
@@ -4,5 +4,8 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 4.2.0"
6
6
  gem "protected_attributes"
7
+ gem "mysql2", ">= 0.3.13", "< 0.6.0"
8
+ gem "pg", "~> 0.15"
9
+ gem "sqlite3", "~> 1.3.6"
7
10
 
8
11
  gemspec name: "audited", path: "../"
@@ -3,5 +3,8 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 5.0.0"
6
+ gem "mysql2", ">= 0.3.18", "< 0.6.0"
7
+ gem "pg", ">= 0.18", "< 2.0"
8
+ gem "sqlite3", "~> 1.3.6"
6
9
 
7
10
  gemspec name: "audited", path: "../"
@@ -3,5 +3,8 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 5.1.4"
6
+ gem "mysql2", ">= 0.3.18", "< 0.6.0"
7
+ gem "pg", ">= 0.18", "< 2.0"
8
+ gem "sqlite3", "~> 1.3.6"
6
9
 
7
10
  gemspec name: "audited", path: "../"
@@ -3,6 +3,8 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", ">= 5.2.0", "< 5.3"
6
- gem "mysql2", "~> 0.4.4"
6
+ gem "mysql2", ">= 0.4.4", "< 0.6.0"
7
+ gem "pg", ">= 0.18", "< 2.0"
8
+ gem "sqlite3", "~> 1.3.6"
7
9
 
8
10
  gemspec name: "audited", path: "../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", ">= 6.0.0", "< 6.1"
6
+ gem "mysql2", ">= 0.4.4"
7
+ gem "pg", ">= 0.18", "< 2.0"
8
+ gem "sqlite3", "~> 1.4"
9
+
10
+ gemspec name: "audited", path: "../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", ">= 6.1.0", "< 6.2"
6
+ gem "mysql2", ">= 0.4.4"
7
+ gem "pg", ">= 1.1", "< 2.0"
8
+ gem "sqlite3", "~> 1.4"
9
+
10
+ gemspec name: "audited", path: "../"
data/lib/audited/audit.rb CHANGED
@@ -16,7 +16,7 @@ module Audited
16
16
  class YAMLIfTextColumnType
17
17
  class << self
18
18
  def load(obj)
19
- if Audited.audit_class.columns_hash["audited_changes"].type.to_s == "text"
19
+ if text_column?
20
20
  ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
21
21
  else
22
22
  obj
@@ -24,12 +24,16 @@ module Audited
24
24
  end
25
25
 
26
26
  def dump(obj)
27
- if Audited.audit_class.columns_hash["audited_changes"].type.to_s == "text"
27
+ if text_column?
28
28
  ActiveRecord::Coders::YAMLColumn.new(Object).dump(obj)
29
29
  else
30
30
  obj
31
31
  end
32
32
  end
33
+
34
+ def text_column?
35
+ Audited.audit_class.columns_hash["audited_changes"].type.to_s == "text"
36
+ end
33
37
  end
34
38
  end
35
39
 
@@ -97,7 +101,7 @@ module Audited
97
101
  auditable_type.constantize.create!(audited_changes)
98
102
  when 'update'
99
103
  # changes back attributes
100
- auditable.update_attributes!(audited_changes.transform_values(&:first))
104
+ auditable.update!(audited_changes.transform_values(&:first))
101
105
  else
102
106
  raise StandardError, "invalid action given #{action}"
103
107
  end
@@ -130,8 +134,8 @@ module Audited
130
134
  # All audits made during the block called will be recorded as made
131
135
  # by +user+. This method is hopefully threadsafe, making it ideal
132
136
  # for background operations that require audit information.
133
- def self.as_user(user, &block)
134
- last_audited_user = ::Audited.store[:audited_user]
137
+ def self.as_user(user)
138
+ last_audited_user = ::Audited.store[:audited_user]
135
139
  ::Audited.store[:audited_user] = user
136
140
  yield
137
141
  ensure
@@ -140,13 +144,11 @@ module Audited
140
144
 
141
145
  # @private
142
146
  def self.reconstruct_attributes(audits)
143
- attributes = {}
144
- result = audits.collect do |audit|
145
- attributes.merge!(audit.new_attributes)[:audit_version] = audit.version
146
- yield attributes if block_given?
147
- end
148
- block_given? ? result : attributes
149
- end
147
+ audits.each_with_object({}) do |audit, all|
148
+ all.merge!(audit.new_attributes)
149
+ all[:audit_version] = audit.version
150
+ end
151
+ end
150
152
 
151
153
  # @private
152
154
  def self.assign_revision_attributes(record, attributes)
@@ -163,15 +165,20 @@ module Audited
163
165
  end
164
166
 
165
167
  # use created_at as timestamp cache key
166
- def self.collection_cache_key(collection = all, timestamp_column = :created_at)
168
+ def self.collection_cache_key(collection = all, *)
167
169
  super(collection, :created_at)
168
170
  end
169
171
 
170
172
  private
171
173
 
172
174
  def set_version_number
173
- max = self.class.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0
174
- self.version = max + 1
175
+ if action == 'create'
176
+ self.version = 1
177
+ else
178
+ collection = Rails::VERSION::MAJOR == 6 ? self.class.unscoped : self.class
179
+ max = collection.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0
180
+ self.version = max + 1
181
+ end
175
182
  end
176
183
 
177
184
  def set_audit_user