audited 4.7.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: 6a2d5a695157822691d7e8a980742c9dab27769c
4
- data.tar.gz: 41262a1baa392f65fd172c6971985ca43ab53e92
2
+ SHA256:
3
+ metadata.gz: e774f1f27e84aaa6f3ad9160eaba9b0e5eda0df9202cb5acaf405de13071c4d3
4
+ data.tar.gz: affeec219575b7032c73f03d69d839aeb7ad81ab7c53e2e636bea0243d66ac8f
5
5
  SHA512:
6
- metadata.gz: f4d0ebf2b9683b96d7954efb7e2ddab5c21e3c0cea29d9d7388334a9f9215f8729b337c850f60abf9b9084773158609d32e99ab580aec244393dc79d41846fad
7
- data.tar.gz: 32a48ffa57d8ebd24ece8714b4c26c8c0a0af4ed7021f165a625a619c058c71749510e20417e9a15629385e72b999409b39186e050af10f5cf73ad36760b25b5
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
- gem 'rails', '>= 5.2.0.rc1', '< 5.3'
27
- gem 'mysql2', '~> 0.4.4'
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,7 +92,7 @@ Changed
16
92
 
17
93
  Fixed
18
94
 
19
- - None
95
+ - Allow use with Rails 5.2 final
20
96
 
21
97
  ## 4.7.0 (2018-03-14)
22
98
 
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.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_attribute!(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,11 +278,16 @@ 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
  ```
257
285
 
286
+ You can access records' own audits and associated audits in one go:
287
+ ```ruby
288
+ company.own_and_associated_audits
289
+ ```
290
+
258
291
  ### Conditional auditing
259
292
 
260
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.
@@ -312,6 +345,29 @@ To disable auditing on an entire model:
312
345
  User.auditing_enabled = false
313
346
  ```
314
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
+
315
371
  ### Custom `Audit` model
316
372
 
317
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: "../"
@@ -2,7 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", ">= 5.2.0.rc1", "< 5.3"
6
- gem "mysql2", "~> 0.4.4"
5
+ gem "rails", ">= 5.2.0", "< 5.3"
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
 
@@ -65,7 +69,7 @@ module Audited
65
69
  def revision
66
70
  clazz = auditable_type.constantize
67
71
  (clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
68
- self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge(version: version))
72
+ self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge(audit_version: version))
69
73
  end
70
74
  end
71
75
 
@@ -88,20 +92,18 @@ module Audited
88
92
 
89
93
  # Allows user to undo changes
90
94
  def undo
91
- model = self.auditable_type.constantize
92
- if action == 'create'
95
+ case action
96
+ when 'create'
93
97
  # destroys a newly created record
94
- model.find(auditable_id).destroy!
95
- elsif action == 'destroy'
98
+ auditable.destroy!
99
+ when 'destroy'
96
100
  # creates a new record with the destroyed record attributes
97
- model.create(audited_changes)
98
- else
101
+ auditable_type.constantize.create!(audited_changes)
102
+ when 'update'
99
103
  # changes back attributes
100
- audited_object = model.find(auditable_id)
101
- self.audited_changes.each do |k, v|
102
- audited_object[k] = v[0]
103
- end
104
- audited_object.save
104
+ auditable.update!(audited_changes.transform_values(&:first))
105
+ else
106
+ raise StandardError, "invalid action given #{action}"
105
107
  end
106
108
  end
107
109
 
@@ -132,21 +134,20 @@ module Audited
132
134
  # All audits made during the block called will be recorded as made
133
135
  # by +user+. This method is hopefully threadsafe, making it ideal
134
136
  # for background operations that require audit information.
135
- def self.as_user(user, &block)
137
+ def self.as_user(user)
138
+ last_audited_user = ::Audited.store[:audited_user]
136
139
  ::Audited.store[:audited_user] = user
137
140
  yield
138
141
  ensure
139
- ::Audited.store[:audited_user] = nil
142
+ ::Audited.store[:audited_user] = last_audited_user
140
143
  end
141
144
 
142
145
  # @private
143
146
  def self.reconstruct_attributes(audits)
144
- attributes = {}
145
- result = audits.collect do |audit|
146
- attributes.merge!(audit.new_attributes)[:version] = audit.version
147
- yield attributes if block_given?
148
- end
149
- block_given? ? result : attributes
147
+ audits.each_with_object({}) do |audit, all|
148
+ all.merge!(audit.new_attributes)
149
+ all[:audit_version] = audit.version
150
+ end
150
151
  end
151
152
 
152
153
  # @private
@@ -164,15 +165,20 @@ module Audited
164
165
  end
165
166
 
166
167
  # use created_at as timestamp cache key
167
- def self.collection_cache_key(collection = all, timestamp_column = :created_at)
168
+ def self.collection_cache_key(collection = all, *)
168
169
  super(collection, :created_at)
169
170
  end
170
171
 
171
172
  private
172
173
 
173
174
  def set_version_number
174
- max = self.class.auditable_finder(auditable_id, auditable_type).descending.first.try(:version) || 0
175
- 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
176
182
  end
177
183
 
178
184
  def set_audit_user