audited 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +13 -0
  3. data/.yardopts +3 -0
  4. data/Appraisals +11 -0
  5. data/CHANGELOG +34 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE +19 -0
  8. data/README.md +206 -0
  9. data/Rakefile +24 -0
  10. data/audited-activerecord.gemspec +19 -0
  11. data/audited-mongo_mapper.gemspec +19 -0
  12. data/audited.gemspec +25 -0
  13. data/gemfiles/rails30.gemfile +7 -0
  14. data/gemfiles/rails31.gemfile +7 -0
  15. data/gemfiles/rails32.gemfile +7 -0
  16. data/lib/audited.rb +11 -0
  17. data/lib/audited/audit.rb +105 -0
  18. data/lib/audited/auditor.rb +272 -0
  19. data/lib/audited/sweeper.rb +45 -0
  20. data/spec/audited_spec_helpers.rb +31 -0
  21. data/spec/rails_app/config/application.rb +5 -0
  22. data/spec/rails_app/config/database.yml +24 -0
  23. data/spec/rails_app/config/environment.rb +5 -0
  24. data/spec/rails_app/config/environments/development.rb +19 -0
  25. data/spec/rails_app/config/environments/production.rb +33 -0
  26. data/spec/rails_app/config/environments/test.rb +33 -0
  27. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  28. data/spec/rails_app/config/initializers/inflections.rb +2 -0
  29. data/spec/rails_app/config/initializers/secret_token.rb +2 -0
  30. data/spec/rails_app/config/routes.rb +6 -0
  31. data/spec/spec_helper.rb +23 -0
  32. data/spec/support/active_record/models.rb +84 -0
  33. data/spec/support/active_record/schema.rb +54 -0
  34. data/spec/support/mongo_mapper/connection.rb +4 -0
  35. data/spec/support/mongo_mapper/models.rb +174 -0
  36. data/test/db/version_1.rb +17 -0
  37. data/test/db/version_2.rb +18 -0
  38. data/test/db/version_3.rb +19 -0
  39. data/test/db/version_4.rb +20 -0
  40. data/test/db/version_5.rb +18 -0
  41. data/test/install_generator_test.rb +17 -0
  42. data/test/test_helper.rb +19 -0
  43. data/test/upgrade_generator_test.rb +65 -0
  44. metadata +220 -0
@@ -0,0 +1,11 @@
1
+ *.log
2
+ *.swp
3
+ .bundle
4
+ .rvmrc
5
+ .yardoc
6
+ coverage/
7
+ doc/
8
+ Gemfile.lock
9
+ gemfiles/*.lock
10
+ pkg
11
+ tmp/*
@@ -0,0 +1,13 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ruby-head
6
+ branches:
7
+ only:
8
+ - master
9
+ - v3
10
+ gemfile:
11
+ - gemfiles/rails30.gemfile
12
+ - gemfiles/rails31.gemfile
13
+ - gemfiles/rails32.gemfile
@@ -0,0 +1,3 @@
1
+ --no-private
2
+ --title acts_as_audited
3
+ --exclude lib/generators
@@ -0,0 +1,11 @@
1
+ appraise 'rails30' do
2
+ gem 'rails', '~> 3.0.0'
3
+ end
4
+
5
+ appraise 'rails31' do
6
+ gem 'rails', '~> 3.1.0'
7
+ end
8
+
9
+ appraise 'rails32' do
10
+ gem 'rails', '~> 3.2.0'
11
+ end
@@ -0,0 +1,34 @@
1
+ Audited ChangeLog
2
+ -------------------------------------------------------------------------------
3
+ * 2012-04-10 - Add Audit scopes for creates, updates and destroys [chriswfx]
4
+ * 2011-10-25 - Made ignored_attributes configurable [senny]
5
+ * 2011-09-09 - Rails 3.x support
6
+ Support for associated audits
7
+ Support for remote IP address storage
8
+ Plenty of bug fixes and refactoring
9
+ [kennethkalmer, ineu, PatrickMa, jrozner, dwarburton, bsiggelkow, dgm]
10
+ * 2009-01-27 - Store old and new values for updates, and store all attributes on destroy.
11
+ Refactored revisioning methods to work as expected
12
+ * 2008-10-10 - changed to make it work in development mode
13
+ * 2008-09-24 - Add ability to record parent record of the record being audited
14
+ [Kenneth Kalmer]
15
+ * 2008-04-19 - refactored to make compatible with dirty tracking in edge rails
16
+ and to stop storing both old and new values in a single audit
17
+ * 2008-04-18 - Fix NoMethodError when trying to access the :previous revision
18
+ on a model that doesn't have previous revisions [Alex Soto]
19
+ * 2008-03-21 - added #changed_attributes to get access to the changes before a
20
+ save [Chris Parker]
21
+ * 2007-12-16 - Added #revision_at for retrieving a revision from a specific
22
+ time [Jacob Atzen]
23
+ * 2007-12-16 - Fix error when getting revision from audit with no changes
24
+ [Geoffrey Wiseman]
25
+ * 2007-12-16 - Remove dependency on acts_as_list
26
+ * 2007-06-17 - Added support getting previous revisions
27
+ * 2006-11-17 - Replaced use of singleton User.current_user with cache sweeper
28
+ implementation for auditing the user that made the change
29
+ * 2006-11-17 - added migration generator
30
+ * 2006-08-14 - incorporated changes from Michael Schuerig to write_attribute
31
+ that saves the new value after every change and not just the
32
+ first, and performs proper type-casting before doing comparisons
33
+ * 2006-08-14 - The "changes" are now saved as a serialized hash
34
+ * 2006-07-21 - 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.
@@ -0,0 +1,206 @@
1
+ Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.png)](http://travis-ci.org/collectiveidea/audited) [![Dependency Status](https://gemnasium.com/collectiveidea/audited.png)](https://gemnasium.com/collectiveidea/audited)
2
+ =======
3
+
4
+ **Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited also allows you to record who made those changes, save comments and associate models related to the changes. Audited works with Rails 3.
5
+
6
+ ## Supported Rubies
7
+
8
+ Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions:
9
+
10
+ * 1.8.7
11
+ * 1.9.2
12
+ * 1.9.3
13
+ * Head
14
+
15
+ 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).
16
+
17
+ ## Supported ORMs
18
+
19
+ In a previous life, Audited was ActiveRecord-only. Audited will now audit models for the following backends:
20
+
21
+ * ActiveRecord
22
+ * MongoMapper
23
+
24
+ ## Installation
25
+
26
+ The installation process depends on what ORM your app is using.
27
+
28
+ ### ActiveRecord
29
+
30
+ Add the appropriate gem to your Gemfile:
31
+
32
+ ```ruby
33
+ gem "audited-activerecord", "~> 3.0"
34
+ ```
35
+
36
+ Then, from your Rails app directory, create the `audits` table:
37
+
38
+ ```bash
39
+ $ rails generate audited:install
40
+ $ rake db:migrate
41
+ ```
42
+
43
+ #### Upgrading
44
+
45
+ If you're already using Audited (or acts_as_audited), your `audits` table may require additional columns. After every upgrade, please run:
46
+
47
+ ```bash
48
+ $ rails generate audited:upgrade
49
+ $ rake db:migrate
50
+ ```
51
+
52
+ Upgrading will only make changes if changes are needed.
53
+
54
+ ### MongoMapper
55
+
56
+ ```ruby
57
+ gem "audited-mongo_mapper", "~> 3.0"
58
+ ```
59
+
60
+ ## Usage
61
+
62
+ Simply call `audited` on your models:
63
+
64
+ ```ruby
65
+ class User < ActiveRecord::Base
66
+ audited
67
+ end
68
+ ```
69
+
70
+ By default, whenever a user is created, updated or destroyed, a new audit is created.
71
+
72
+ ```ruby
73
+ user = User.create!(:name => "Steve")
74
+ user.audits.count # => 1
75
+ user.update_attributes!(:name => "Ryan")
76
+ user.audits.count # => 2
77
+ user.destroy
78
+ user.audits.count # => 3
79
+ ```
80
+
81
+ Audits contain information regarding what action was taken on the model and what changes were made.
82
+
83
+ ```ruby
84
+ user.update_attributes!(:name => "Ryan")
85
+ audit = user.audits.last
86
+ audit.action # => "update"
87
+ audit.audited_changes # => {"name"=>["Steve", "Ryan"]}
88
+ ```
89
+
90
+ ### Comments
91
+
92
+ You can attach comments to each audit using an `audit_comment` attribute on your model.
93
+
94
+ ```ruby
95
+ user.update_attributes!(:name => "Ryan", :audit_comment => "Changing name, just because")
96
+ user.audits.last.comment # => "Changing name, just because"
97
+ ```
98
+
99
+ You can optionally add the `:comment_required` option to your `audited` call to require comments for all audits.
100
+
101
+ ```ruby
102
+ class User < ActiveRecord::Base
103
+ audited :comment_required => true
104
+ end
105
+ ```
106
+
107
+ ### Current User Tracking
108
+
109
+ 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.
110
+
111
+ ```
112
+ class PostsController < ApplicationController
113
+ def create
114
+ current_user # => #<User name: "Steve">
115
+ @post = Post.create(params[:post])
116
+ @post.audits.last.user # => #<User name: "Steve">
117
+ end
118
+ end
119
+ ```
120
+
121
+ To use a method other than `current_user`, put the following in an intializer:
122
+
123
+ ```ruby
124
+ Audited.current_user_method = :authenticated_user
125
+ ```
126
+
127
+ Outside of a request, Audited can still record the user with the `as_user` method:
128
+
129
+ ```ruby
130
+ Audit.as_user(User.find(1)) do
131
+ post.update_attribute!(:title => "Hello, world!")
132
+ end
133
+ post.audits.last.user # => #<User id: 1>
134
+ ```
135
+
136
+ ### Associated Audits
137
+
138
+ Sometimes it's useful to associate an audit with a model other than the one being changed. For instance, given the following models:
139
+
140
+ ```ruby
141
+ class User < ActiveRecord::Base
142
+ belongs_to :company
143
+ audited
144
+ end
145
+
146
+ class Company < ActiveRecord::Base
147
+ has_many :users
148
+ end
149
+ ```
150
+
151
+ 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:
152
+
153
+ ```ruby
154
+ class User < ActiveRecord::Base
155
+ belongs_to :company
156
+ audited :associated_with => :company
157
+ end
158
+
159
+ class Company < ActiveRecord::Base
160
+ has_many :users
161
+ has_associated_audits
162
+ end
163
+ ```
164
+
165
+ Now, when a 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.
166
+
167
+ ```ruby
168
+ company = Company.create!(:name => "Collective Idea")
169
+ user = company.users.create!(:name => "Steve")
170
+ user.update_attribute!(:name => "Steve Richert")
171
+ user.audits.last.associated # => #<Company name: "Collective Idea">
172
+ company.associated_audits.last.auditable # => #<User name: "Steve Richert">
173
+ ```
174
+
175
+ ## Gotchas
176
+
177
+ ### ActiveRecord Accessible Attributes
178
+
179
+ If your model calls `attr_accessible` after `audited`, you'll need to set the `:protect => false` option. By default, Audited uses `attr_protected` to prevent malicious users from dissociating your audits, but Rails doesn't allow both `attr_protected` and `attr_accessible`.
180
+
181
+ ```ruby
182
+ class User < ActiveRecord::Base
183
+ audited :protect => false
184
+ attr_accessible :name
185
+ end
186
+ ```
187
+
188
+ ### MongoMapper Embedded Documents
189
+
190
+ Currently, Audited does not track changes on embedded documents. Audited works by tracking a model's [dirty changes](http://api.rubyonrails.org/classes/ActiveModel/Dirty.html) but changes to embedded documents don't appear in dirty tracking.
191
+
192
+ ## Support
193
+
194
+ You can find documentation at: http://rdoc.info/github/collectiveidea/audited
195
+
196
+ Or join the [mailing list](http://groups.google.com/group/audited) to get help or offer suggestions.
197
+
198
+ ## Contributing
199
+
200
+ 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:
201
+
202
+ * Use prerelease versions of Audited.
203
+ * [Report bugs](https://github.com/collectiveidea/audited/issues).
204
+ * Fix bugs and submit [pull requests](http://github.com/collectiveidea/audited/pulls).
205
+ * Write, clarify or fix documentation.
206
+ * Refactor code.
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_helper'
4
+ require 'rspec/core/rake_task'
5
+ require 'appraisal'
6
+
7
+ Bundler::GemHelper.install_tasks(:name => 'audited')
8
+ Bundler::GemHelper.install_tasks(:name => 'audited-activerecord')
9
+ Bundler::GemHelper.install_tasks(:name => 'audited-mongo_mapper')
10
+
11
+ ADAPTERS = %w(active_record mongo_mapper)
12
+
13
+ ADAPTERS.each do |adapter|
14
+ desc "Run RSpec code examples for #{adapter} adapter"
15
+ RSpec::Core::RakeTask.new(adapter) do |t|
16
+ t.pattern = "spec/audited/adapters/#{adapter}/**/*_spec.rb"
17
+ end
18
+ end
19
+
20
+ RSpec::Core::RakeTask.new(:spec => ADAPTERS) do |t|
21
+ t.pattern = 'spec/audited/*_spec.rb'
22
+ end
23
+
24
+ task :default => :spec
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'audited-activerecord'
5
+ gem.version = '3.0.0.rc1'
6
+
7
+ gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
8
+ gem.email = 'info@collectiveidea.com'
9
+ gem.description = 'Log all changes to your ActiveRecord models'
10
+ gem.summary = gem.description
11
+ gem.homepage = 'https://github.com/collectiveidea/audited'
12
+
13
+ gem.add_dependency 'audited', gem.version
14
+ gem.add_dependency 'activerecord', '~> 3.0'
15
+
16
+ gem.files = `git ls-files lib`.split($\).grep(/(active_?record|generators)/)
17
+ gem.require_paths = ['lib']
18
+ end
19
+
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'audited-mongo_mapper'
5
+ gem.version = '3.0.0.rc1'
6
+
7
+ gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
8
+ gem.email = 'info@collectiveidea.com'
9
+ gem.description = 'Log all changes to your MongoMapper models'
10
+ gem.summary = gem.description
11
+ gem.homepage = 'https://github.com/collectiveidea/audited'
12
+
13
+ gem.add_dependency 'audited', gem.version
14
+ gem.add_dependency 'mongo_mapper', '~> 0.11'
15
+
16
+ gem.files = `git ls-files lib`.split($\).grep(/mongo_mapper/)
17
+ gem.require_paths = ['lib']
18
+ end
19
+
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'audited'
5
+ gem.version = '3.0.0.rc1'
6
+
7
+ gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
8
+ gem.email = 'info@collectiveidea.com'
9
+ gem.description = 'Log all changes to your models'
10
+ gem.summary = gem.description
11
+ gem.homepage = 'https://github.com/collectiveidea/audited'
12
+
13
+ gem.add_development_dependency 'activerecord', '~> 3.0'
14
+ gem.add_development_dependency 'appraisal', '~> 0.4'
15
+ gem.add_development_dependency 'bson_ext', '~> 1.6'
16
+ gem.add_development_dependency 'mongo_mapper', '~> 0.11'
17
+ gem.add_development_dependency 'rails', '~> 3.0'
18
+ gem.add_development_dependency 'rspec-rails', '~> 2.0'
19
+ gem.add_development_dependency 'sqlite3', '~> 1.0'
20
+
21
+ gem.files = `git ls-files`.split($\).reject{|f| f =~ /(lib\/audited\-|adapters|generators)/ }
22
+ gem.test_files = gem.files.grep(/^spec\//)
23
+ gem.require_paths = ['lib']
24
+ end
25
+
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.0.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.1.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,11 @@
1
+ module Audited
2
+ VERSION = '3.0.0'
3
+
4
+ class << self
5
+ attr_accessor :ignored_attributes, :current_user_method, :audit_class
6
+ end
7
+
8
+ @ignored_attributes = %w(lock_version created_at updated_at created_on updated_on)
9
+
10
+ @current_user_method = :current_user
11
+ end
@@ -0,0 +1,105 @@
1
+ module Audited
2
+ module Audit
3
+ def self.included(klass)
4
+ klass.extend(ClassMethods)
5
+ klass.setup_audit
6
+ end
7
+
8
+ module ClassMethods
9
+ def setup_audit
10
+ belongs_to :auditable, :polymorphic => true
11
+ belongs_to :user, :polymorphic => true
12
+ belongs_to :associated, :polymorphic => true
13
+
14
+ before_create :set_version_number, :set_audit_user
15
+
16
+ cattr_accessor :audited_class_names
17
+ self.audited_class_names = Set.new
18
+
19
+ attr_accessible :action, :audited_changes, :comment, :associated
20
+ end
21
+
22
+ # Returns the list of classes that are being audited
23
+ def audited_classes
24
+ audited_class_names.map(&:constantize)
25
+ end
26
+
27
+ # All audits made during the block called will be recorded as made
28
+ # by +user+. This method is hopefully threadsafe, making it ideal
29
+ # for background operations that require audit information.
30
+ def as_user(user, &block)
31
+ Thread.current[:audited_user] = user
32
+
33
+ yieldval = yield
34
+
35
+ Thread.current[:audited_user] = nil
36
+
37
+ yieldval
38
+ end
39
+
40
+ # @private
41
+ def reconstruct_attributes(audits)
42
+ attributes = {}
43
+ result = audits.collect do |audit|
44
+ attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
45
+ yield attributes if block_given?
46
+ end
47
+ block_given? ? result : attributes
48
+ end
49
+
50
+ # @private
51
+ def assign_revision_attributes(record, attributes)
52
+ attributes.each do |attr, val|
53
+ record = record.dup if record.frozen?
54
+
55
+ if record.respond_to?("#{attr}=")
56
+ record.attributes.has_key?(attr.to_s) ?
57
+ record[attr] = val :
58
+ record.send("#{attr}=", val)
59
+ end
60
+ end
61
+ record
62
+ end
63
+ end
64
+
65
+ # Return an instance of what the object looked like at this revision. If
66
+ # the object has been destroyed, this will be a new record.
67
+ def revision
68
+ clazz = auditable_type.constantize
69
+ (clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
70
+ self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({ :version => version }))
71
+ end
72
+ end
73
+
74
+ # Returns a hash of the changed attributes with the new values
75
+ def new_attributes
76
+ (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
77
+ attrs[attr] = values.is_a?(Array) ? values.last : values
78
+ attrs
79
+ end
80
+ end
81
+
82
+ # Returns a hash of the changed attributes with the old values
83
+ def old_attributes
84
+ (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
85
+ attrs[attr] = Array(values).first
86
+
87
+ attrs
88
+ end
89
+ end
90
+
91
+ private
92
+ def set_version_number
93
+ max = self.class.where(
94
+ :auditable_id => auditable_id,
95
+ :auditable_type => auditable_type
96
+ ).order(:version.desc).first.try(:version) || 0
97
+ self.version = max + 1
98
+ end
99
+
100
+ def set_audit_user
101
+ self.user = Thread.current[:audited_user] if Thread.current[:audited_user]
102
+ nil # prevent stopping callback chains
103
+ end
104
+ end
105
+ end