audited-hp 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +32 -0
  4. data/.yardopts +3 -0
  5. data/Appraisals +22 -0
  6. data/CHANGELOG +153 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +19 -0
  9. data/README.md +299 -0
  10. data/Rakefile +18 -0
  11. data/gemfiles/rails40.gemfile +9 -0
  12. data/gemfiles/rails41.gemfile +8 -0
  13. data/gemfiles/rails42.gemfile +8 -0
  14. data/gemfiles/rails50.gemfile +8 -0
  15. data/lib/audited-rspec.rb +4 -0
  16. data/lib/audited.rb +29 -0
  17. data/lib/audited/audit.rb +149 -0
  18. data/lib/audited/auditor.rb +309 -0
  19. data/lib/audited/rspec_matchers.rb +177 -0
  20. data/lib/audited/sweeper.rb +60 -0
  21. data/lib/audited/version.rb +3 -0
  22. data/lib/generators/audited/install_generator.rb +20 -0
  23. data/lib/generators/audited/migration.rb +15 -0
  24. data/lib/generators/audited/templates/add_association_to_audits.rb +11 -0
  25. data/lib/generators/audited/templates/add_comment_to_audits.rb +9 -0
  26. data/lib/generators/audited/templates/add_remote_address_to_audits.rb +10 -0
  27. data/lib/generators/audited/templates/add_request_uuid_to_audits.rb +10 -0
  28. data/lib/generators/audited/templates/install.rb +30 -0
  29. data/lib/generators/audited/templates/rename_association_to_associated.rb +23 -0
  30. data/lib/generators/audited/templates/rename_changes_to_audited_changes.rb +9 -0
  31. data/lib/generators/audited/templates/rename_parent_to_association.rb +11 -0
  32. data/lib/generators/audited/upgrade_generator.rb +57 -0
  33. data/spec/audited/audit_spec.rb +199 -0
  34. data/spec/audited/auditor_spec.rb +607 -0
  35. data/spec/audited/sweeper_spec.rb +106 -0
  36. data/spec/audited_spec_helpers.rb +20 -0
  37. data/spec/rails_app/config/application.rb +8 -0
  38. data/spec/rails_app/config/database.yml +24 -0
  39. data/spec/rails_app/config/environment.rb +5 -0
  40. data/spec/rails_app/config/environments/development.rb +21 -0
  41. data/spec/rails_app/config/environments/production.rb +35 -0
  42. data/spec/rails_app/config/environments/test.rb +47 -0
  43. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  44. data/spec/rails_app/config/initializers/inflections.rb +2 -0
  45. data/spec/rails_app/config/initializers/secret_token.rb +3 -0
  46. data/spec/rails_app/config/routes.rb +3 -0
  47. data/spec/spec_helper.rb +21 -0
  48. data/spec/support/active_record/models.rb +99 -0
  49. data/spec/support/active_record/schema.rb +81 -0
  50. data/test/db/version_1.rb +17 -0
  51. data/test/db/version_2.rb +18 -0
  52. data/test/db/version_3.rb +19 -0
  53. data/test/db/version_4.rb +20 -0
  54. data/test/db/version_5.rb +18 -0
  55. data/test/db/version_6.rb +17 -0
  56. data/test/install_generator_test.rb +17 -0
  57. data/test/test_helper.rb +19 -0
  58. data/test/upgrade_generator_test.rb +77 -0
  59. metadata +229 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8180c7e9719f2de9261fb2b2353532a231c9883f
4
+ data.tar.gz: 10f2dd4a7f48f5527ee85ccd724c7fda9203fd27
5
+ SHA512:
6
+ metadata.gz: 2aacfc198a4243092803d9e3fdcffb4da68c5fa66508bb765a5b0cf4be84b8f6fed4243743221ba8b07b2821879cac3bd08044f3566f446027e2e3fc429c83ea
7
+ data.tar.gz: 79729ec7e575eadc4c8855d4b64e636176d453559569908832431708a3d3832321af96feba91c23fe5c3f7a6b627635d050204ddba1aa2533acc80435f6a99ef
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.iml
2
+ *.ipr
3
+ *.iws
4
+ *.log
5
+ *.swp
6
+ .bundle
7
+ .rakeTasks
8
+ .ruby-gemset
9
+ .ruby-version
10
+ .rvmrc
11
+ .yardoc
12
+ coverage/
13
+ doc/
14
+ Gemfile.lock
15
+ gemfiles/*.lock
16
+ pkg
17
+ tmp/*
18
+ audited_test.sqlite3.db
data/.travis.yml ADDED
@@ -0,0 +1,32 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1
5
+ - 2.2.4
6
+ - 2.3.1
7
+ - ruby-head
8
+ env:
9
+ - DB=SQLITE
10
+ - DB=POSTGRES
11
+ - DB=MYSQL
12
+ gemfile:
13
+ - gemfiles/rails40.gemfile
14
+ - gemfiles/rails41.gemfile
15
+ - gemfiles/rails42.gemfile
16
+ - gemfiles/rails50.gemfile
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ exclude:
21
+ - rvm: 2.1
22
+ gemfile: gemfiles/rails50.gemfile
23
+ fast_finish: true
24
+ branches:
25
+ only:
26
+ - master
27
+ sudo: false
28
+ notifications:
29
+ webhooks:
30
+ urls:
31
+ - http://buildlight.collectiveidea.com/
32
+ on_start: always
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --no-private
2
+ --title acts_as_audited
3
+ --exclude lib/generators
data/Appraisals ADDED
@@ -0,0 +1,22 @@
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
11
+
12
+ appraise 'rails42' do
13
+ gem 'rails', '~> 4.2.0'
14
+ gem 'protected_attributes'
15
+ end
16
+
17
+ appraise 'rails50' do
18
+ gem 'rails', '~> 5.0.0'
19
+
20
+ # The following needs to point to Github until the release of 0.1.3
21
+ gem 'rails-observers', github: 'rails/rails-observers', branch: 'master'
22
+ end
data/CHANGELOG ADDED
@@ -0,0 +1,153 @@
1
+ # Audited ChangeLog
2
+
3
+ ## Unreleased
4
+
5
+ Breaking changes
6
+
7
+ - None
8
+
9
+ Added
10
+
11
+ - None
12
+
13
+ Fixed
14
+
15
+ - None
16
+
17
+ ## 4.3.0 (2016-09-17)
18
+
19
+ Breaking changes
20
+
21
+ - None
22
+
23
+ Added
24
+
25
+ - Support singular arguments for options: `on` and `only`
26
+
27
+ Fixed
28
+
29
+ - Fix auditing instance attributes if "only" option specified
30
+ - Allow private / protected callback declarations
31
+ - Do not eagerly connect to database
32
+
33
+ ## 4.2.2 (2016-08-01)
34
+
35
+ - Correct auditing_enabled for STI models
36
+ - Properly set table name for mongomapper
37
+
38
+ ## 4.2.1 (2016-07-29)
39
+
40
+ - Fix bug when only: is a single field.
41
+ - update gemspec to use mongomapper 0.13
42
+ - sweeper need not run observer for mongomapper
43
+ - Make temporary disabling of auditing threadsafe
44
+ - Centralize `Audited.store` as thread safe variable store
45
+
46
+ ## 4.2.0 (2015-03-31)
47
+
48
+ Not yet documented.
49
+
50
+ ## 4.0.0 (2014-09-04)
51
+
52
+ Not yet documented.
53
+
54
+ ## 4.0.0.rc1 (2014-07-30)
55
+
56
+ Not yet documented.
57
+
58
+ ## 3.0.0 (2012-09-25)
59
+
60
+ Not yet documented.
61
+
62
+ ## 3.0.0.rc2 (2012-07-09)
63
+
64
+ Not yet documented.
65
+
66
+ ## 3.0.0.rc1 (2012-04-25)
67
+
68
+ Not yet documented.
69
+
70
+ ## 2012-04-10
71
+
72
+ - Add Audit scopes for creates, updates and destroys [chriswfx]
73
+
74
+ ## 2011-10-25
75
+
76
+ - Made ignored_attributes configurable [senny]
77
+
78
+ ## 2011-09-09
79
+
80
+ - Rails 3.x support
81
+ - Support for associated audits
82
+ - Support for remote IP address storage
83
+ - Plenty of bug fixes and refactoring
84
+ - [kennethkalmer, ineu, PatrickMa, jrozner, dwarburton, bsiggelkow, dgm]
85
+
86
+ ## 2009-01-27
87
+
88
+ - Store old and new values for updates, and store all attributes on destroy.
89
+ - Refactored revisioning methods to work as expected
90
+
91
+ ## 2008-10-10
92
+
93
+ - changed to make it work in development mode
94
+
95
+ ## 2008-09-24
96
+
97
+ - Add ability to record parent record of the record being audited [Kenneth Kalmer]
98
+
99
+ ## 2008-04-19
100
+
101
+ - refactored to make compatible with dirty tracking in edge rails
102
+ and to stop storing both old and new values in a single audit
103
+
104
+ ## 2008-04-18
105
+
106
+ - Fix NoMethodError when trying to access the :previous revision
107
+ on a model that doesn't have previous revisions [Alex Soto]
108
+
109
+ ## 2008-03-21
110
+
111
+ - added #changed_attributes to get access to the changes before a
112
+ save [Chris Parker]
113
+
114
+ ## 2007-12-16
115
+
116
+ - Added #revision_at for retrieving a revision from a specific
117
+ time [Jacob Atzen]
118
+
119
+ ## 2007-12-16
120
+
121
+ - Fix error when getting revision from audit with no changes
122
+ [Geoffrey Wiseman]
123
+
124
+ ## 2007-12-16
125
+
126
+ - Remove dependency on acts_as_list
127
+
128
+ ## 2007-06-17
129
+
130
+ - Added support getting previous revisions
131
+
132
+ ## 2006-11-17
133
+
134
+ - Replaced use of singleton User.current_user with cache sweeper
135
+ implementation for auditing the user that made the change
136
+
137
+ ## 2006-11-17
138
+
139
+ - added migration generator
140
+
141
+ ## 2006-08-14
142
+
143
+ - incorporated changes from Michael Schuerig to write_attribute
144
+ that saves the new value after every change and not just the
145
+ first, and performs proper type-casting before doing comparisons
146
+
147
+ ## 2006-08-14
148
+
149
+ - The "changes" are now saved as a serialized hash
150
+
151
+ ## 2006-07-21
152
+
153
+ - initial version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec name: "audited"
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2010 Brandon Keepers - Collective Idea
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,299 @@
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)
2
+ =======
3
+
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
+
6
+ Audited currently (4.x) works with Rails 5.0 and 4.2. It may work with 4.1 and 4.0, but this is not guaranteed.
7
+
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
+
10
+ ## Supported Rubies
11
+
12
+ Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions:
13
+
14
+ * 2.1.5
15
+ * 2.2.4
16
+ * 2.3.1
17
+
18
+ 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).
19
+
20
+ ## Supported ORMs
21
+
22
+ Audited is currently ActiveRecord-only. In a previous life, Audited worked with MongoMapper. Use the [4.2-stable branch](https://github.com/collectiveidea/audited/tree/4.2-stable) if you need MongoMapper.
23
+
24
+ ## Installation
25
+
26
+ Add the gem to your Gemfile:
27
+
28
+ ```ruby
29
+ gem "audited", "~> 4.3"
30
+ ```
31
+
32
+ If you are using rails 5.0, you would also need the following line in your Gemfile.
33
+ ```ruby
34
+ gem "rails-observers", github: 'rails/rails-observers'
35
+ ```
36
+
37
+ Then, from your Rails app directory, create the `audits` table:
38
+
39
+ ```bash
40
+ $ rails generate audited:install
41
+ $ rake db:migrate
42
+ ```
43
+
44
+ #### Upgrading
45
+
46
+ If you're already using Audited (or acts_as_audited), your `audits` table may require additional columns. After every upgrade, please run:
47
+
48
+ ```bash
49
+ $ rails generate audited:upgrade
50
+ $ rake db:migrate
51
+ ```
52
+
53
+ Upgrading will only make changes if changes are needed.
54
+
55
+
56
+ ## Usage
57
+
58
+ Simply call `audited` on your models:
59
+
60
+ ```ruby
61
+ class User < ActiveRecord::Base
62
+ audited
63
+ end
64
+ ```
65
+
66
+ By default, whenever a user is created, updated or destroyed, a new audit is created.
67
+
68
+ ```ruby
69
+ user = User.create!(name: "Steve")
70
+ user.audits.count # => 1
71
+ user.update_attributes!(name: "Ryan")
72
+ user.audits.count # => 2
73
+ user.destroy
74
+ user.audits.count # => 3
75
+ ```
76
+
77
+ Audits contain information regarding what action was taken on the model and what changes were made.
78
+
79
+ ```ruby
80
+ user.update_attributes!(name: "Ryan")
81
+ audit = user.audits.last
82
+ audit.action # => "update"
83
+ audit.audited_changes # => {"name"=>["Steve", "Ryan"]}
84
+ ```
85
+
86
+ You can get previous versions of a record by index or date, or list all
87
+ revisions.
88
+
89
+ ```ruby
90
+ user.revisions
91
+ user.revision(1)
92
+ user.revision_at(Date.parse("2016-01-01"))
93
+ ```
94
+
95
+ ### Specifying columns
96
+
97
+ By default, a new audit is created for any attribute changes. You can, however, limit the columns to be considered.
98
+
99
+ ```ruby
100
+ class User < ActiveRecord::Base
101
+ # All fields
102
+ # audited
103
+
104
+ # Single field
105
+ # audited only: :name
106
+
107
+ # Multiple fields
108
+ # audited only: [:name, :address]
109
+
110
+ # All except certain fields
111
+ # audited except: :password
112
+ end
113
+ ```
114
+
115
+ ### Specifying callbacks
116
+
117
+ By default, a new audit is created for any Create, Update or Destroy action. You can, however, limit the actions audited.
118
+
119
+ ```ruby
120
+ class User < ActiveRecord::Base
121
+ # All fields and actions
122
+ # audited
123
+
124
+ # Single field, only audit Update and Destroy (not Create)
125
+ # audited only: :name, on: [:update, :destroy]
126
+ end
127
+ ```
128
+
129
+ ### Comments
130
+
131
+ You can attach comments to each audit using an `audit_comment` attribute on your model.
132
+
133
+ ```ruby
134
+ user.update_attributes!(name: "Ryan", audit_comment: "Changing name, just because")
135
+ user.audits.last.comment # => "Changing name, just because"
136
+ ```
137
+
138
+ You can optionally add the `:comment_required` option to your `audited` call to require comments for all audits.
139
+
140
+ ```ruby
141
+ class User < ActiveRecord::Base
142
+ audited :comment_required => true
143
+ end
144
+ ```
145
+
146
+ ### Current User Tracking
147
+
148
+ 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.
149
+
150
+ ```ruby
151
+ class PostsController < ApplicationController
152
+ def create
153
+ current_user # => #<User name: "Steve">
154
+ @post = Post.create(params[:post])
155
+ @post.audits.last.user # => #<User name: "Steve">
156
+ end
157
+ end
158
+ ```
159
+
160
+ To use a method other than `current_user`, put the following in an initializer:
161
+
162
+ ```ruby
163
+ Audited.current_user_method = :authenticated_user
164
+ ```
165
+
166
+ Outside of a request, Audited can still record the user with the `as_user` method:
167
+
168
+ ```ruby
169
+ Audited::Audit.as_user(User.find(1)) do
170
+ post.update_attribute!(title: "Hello, world!")
171
+ end
172
+ post.audits.last.user # => #<User id: 1>
173
+ ```
174
+
175
+ #### Custom Auditor
176
+
177
+ You might need to use a custom auditor from time to time. It can be done by simply passing in a string:
178
+
179
+ ```ruby
180
+ class ApplicationController < ActionController::Base
181
+ def authenticated_user
182
+ if current_user
183
+ current_user
184
+ else
185
+ 'Elon Musk'
186
+ end
187
+ end
188
+ end
189
+ ```
190
+
191
+ ### Associated Audits
192
+
193
+ Sometimes it's useful to associate an audit with a model other than the one being changed. For instance, given the following models:
194
+
195
+ ```ruby
196
+ class User < ActiveRecord::Base
197
+ belongs_to :company
198
+ audited
199
+ end
200
+
201
+ class Company < ActiveRecord::Base
202
+ has_many :users
203
+ end
204
+ ```
205
+
206
+ Every change to a user is audited, but what if you want to grab all of the audits of users belonging to a particular company? You can add the `:associated_with` option to your `audited` call:
207
+
208
+ ```ruby
209
+ class User < ActiveRecord::Base
210
+ belongs_to :company
211
+ audited associated_with: :company
212
+ end
213
+
214
+ class Company < ActiveRecord::Base
215
+ has_many :users
216
+ has_associated_audits
217
+ end
218
+ ```
219
+
220
+ Now, when an audit is created for a user, that user's company is also saved alongside the audit. This makes it much easier (and faster) to access audits indirectly related to a company.
221
+
222
+ ```ruby
223
+ company = Company.create!(name: "Collective Idea")
224
+ user = company.users.create!(name: "Steve")
225
+ user.update_attribute!(name: "Steve Richert")
226
+ user.audits.last.associated # => #<Company name: "Collective Idea">
227
+ company.associated_audits.last.auditable # => #<User name: "Steve Richert">
228
+ ```
229
+
230
+ ### Disabling auditing
231
+
232
+ If you want to disable auditing temporarily doing certain tasks, there are a few
233
+ methods available.
234
+
235
+ To disable auditing on a save:
236
+
237
+ ```ruby
238
+ @user.save_without_auditing
239
+ ```
240
+
241
+ or:
242
+
243
+ ```ruby
244
+ @user.without_auditing do
245
+ @user.save
246
+ end
247
+ ```
248
+
249
+ To disable auditing on a column:
250
+
251
+ ```ruby
252
+ User.non_audited_columns = [:first_name, :last_name]
253
+ ```
254
+
255
+ To disable auditing on an entire model:
256
+
257
+ ```ruby
258
+ User.auditing_enabled = false
259
+ ```
260
+
261
+ ## Gotchas
262
+
263
+ ### Using attr_protected with Rails 4.x
264
+
265
+ 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.
266
+
267
+ First be sure to add `allow_mass_assignment: true` to your `audited` call; otherwise Audited will
268
+ interfere with `protected_attributes` and none of your `save` calls will work.
269
+
270
+ ```ruby
271
+ class User < ActiveRecord::Base
272
+ audited allow_mass_assignment: true
273
+ end
274
+ ```
275
+
276
+ Second, be sure to add `audit_ids` to the list of protected attributes to prevent data loss.
277
+
278
+ ```ruby
279
+ class User < ActiveRecord::Base
280
+ audited allow_mass_assignment: true
281
+ attr_protected :logins, :audit_ids
282
+ end
283
+ ```
284
+
285
+ ## Support
286
+
287
+ You can find documentation at: http://rdoc.info/github/collectiveidea/audited
288
+
289
+ Or join the [mailing list](http://groups.google.com/group/audited) to get help or offer suggestions.
290
+
291
+ ## Contributing
292
+
293
+ In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project. Here are a few ways _you_ can pitch in:
294
+
295
+ * Use prerelease versions of Audited.
296
+ * [Report bugs](https://github.com/collectiveidea/audited/issues).
297
+ * Fix bugs and submit [pull requests](http://github.com/collectiveidea/audited/pulls).
298
+ * Write, clarify or fix documentation.
299
+ * Refactor code.