audited 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,2 @@
1
+ ActiveSupport::Inflector.inflections do |inflect|
2
+ end
@@ -0,0 +1,2 @@
1
+ Rails.application.config.secret_token = 'ea942c41850d502f2c8283e26bdc57829f471bb18224ddff0a192c4f32cdf6cb5aa0d82b3a7a7adbeb640c4b06f3aa1cd5f098162d8240f669b39d6b49680571'
2
+ Rails.application.config.session_store :cookie_store, :key => "_my_app"
@@ -0,0 +1,6 @@
1
+ Rails.application.routes.draw do
2
+
3
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
4
+ # Note: This route will make all actions in every controller accessible via GET requests.
5
+ match ':controller(/:action(/:id(.:format)))'
6
+ end
@@ -0,0 +1,23 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'rails/all'
4
+ require 'rails_app/config/environment'
5
+ require 'rspec/rails'
6
+ require 'audited'
7
+ require 'audited_spec_helpers'
8
+
9
+ SPEC_ROOT = Pathname.new(File.expand_path('../', __FILE__))
10
+
11
+ Dir[SPEC_ROOT.join('support/*.rb')].each{|f| require f }
12
+
13
+ RSpec.configure do |config|
14
+ config.include AuditedSpecHelpers
15
+
16
+ config.before(:each, :adapter => :active_record) do
17
+ Audited.audit_class = Audited::Adapters::ActiveRecord::Audit
18
+ end
19
+
20
+ config.before(:each, :adapter => :mongo_mapper) do
21
+ Audited.audit_class = Audited::Adapters::MongoMapper::Audit
22
+ end
23
+ end
@@ -0,0 +1,84 @@
1
+ require 'cgi'
2
+ require File.expand_path('../schema', __FILE__)
3
+
4
+ module Models
5
+ module ActiveRecord
6
+ class User < ::ActiveRecord::Base
7
+ audited :except => :password
8
+
9
+ attr_protected :logins
10
+
11
+ def name=(val)
12
+ write_attribute(:name, CGI.escapeHTML(val))
13
+ end
14
+ end
15
+
16
+ class CommentRequiredUser < ::ActiveRecord::Base
17
+ self.table_name = :users
18
+ audited :comment_required => true
19
+ end
20
+
21
+ class UnprotectedUser < ::ActiveRecord::Base
22
+ self.table_name = :users
23
+ audited :protect => false
24
+ attr_accessible :name, :username, :password
25
+ end
26
+
27
+ class AccessibleUser < ::ActiveRecord::Base
28
+ self.table_name = :users
29
+ attr_accessible :name, :username, :password # declare attr_accessible before calling aaa
30
+ audited
31
+ end
32
+
33
+ class NoAttributeProtectionUser < ::ActiveRecord::Base
34
+ self.table_name = :users
35
+ audited
36
+ end
37
+
38
+ class UserWithAfterAudit < ::ActiveRecord::Base
39
+ self.table_name = :users
40
+ audited
41
+ attr_accessor :bogus_attr
42
+
43
+ def after_audit
44
+ self.bogus_attr = "do something"
45
+ end
46
+ end
47
+
48
+ class Company < ::ActiveRecord::Base
49
+ audited
50
+ end
51
+
52
+ class Owner < ::ActiveRecord::Base
53
+ self.table_name = 'users'
54
+ has_associated_audits
55
+ end
56
+
57
+ class OwnedCompany < ::ActiveRecord::Base
58
+ self.table_name = 'companies'
59
+ belongs_to :owner, :class_name => "Owner"
60
+ attr_accessible :name, :owner # declare attr_accessible before calling aaa
61
+ audited :associated_with => :owner
62
+ end
63
+
64
+ class OnUpdateDestroy < ::ActiveRecord::Base
65
+ self.table_name = 'companies'
66
+ audited :on => [:update, :destroy]
67
+ end
68
+
69
+ class OnCreateDestroy < ::ActiveRecord::Base
70
+ self.table_name = 'companies'
71
+ audited :on => [:create, :destroy]
72
+ end
73
+
74
+ class OnCreateDestroyExceptName < ::ActiveRecord::Base
75
+ self.table_name = 'companies'
76
+ audited :except => :name, :on => [:create, :destroy]
77
+ end
78
+
79
+ class OnCreateUpdate < ::ActiveRecord::Base
80
+ self.table_name = 'companies'
81
+ audited :on => [:create, :update]
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_record'
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
5
+ ActiveRecord::Base.logger = Logger.new(SPEC_ROOT.join('debug.log'))
6
+ ActiveRecord::Migration.verbose = false
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table :users, :force => true do |t|
10
+ t.column :name, :string
11
+ t.column :username, :string
12
+ t.column :password, :string
13
+ t.column :activated, :boolean
14
+ t.column :suspended_at, :datetime
15
+ t.column :logins, :integer, :default => 0
16
+ t.column :created_at, :datetime
17
+ t.column :updated_at, :datetime
18
+ end
19
+
20
+ create_table :companies, :force => true do |t|
21
+ t.column :name, :string
22
+ t.column :owner_id, :integer
23
+ end
24
+
25
+ create_table :authors, :force => true do |t|
26
+ t.column :name, :string
27
+ end
28
+
29
+ create_table :books, :force => true do |t|
30
+ t.column :authord_id, :integer
31
+ t.column :title, :string
32
+ end
33
+
34
+ create_table :audits, :force => true do |t|
35
+ t.column :auditable_id, :integer
36
+ t.column :auditable_type, :string
37
+ t.column :associated_id, :integer
38
+ t.column :associated_type, :string
39
+ t.column :user_id, :integer
40
+ t.column :user_type, :string
41
+ t.column :username, :string
42
+ t.column :action, :string
43
+ t.column :audited_changes, :text
44
+ t.column :version, :integer, :default => 0
45
+ t.column :comment, :string
46
+ t.column :remote_address, :string
47
+ t.column :created_at, :datetime
48
+ end
49
+
50
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
51
+ add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
52
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
53
+ add_index :audits, :created_at
54
+ end
@@ -0,0 +1,4 @@
1
+ require 'mongo_mapper'
2
+
3
+ MongoMapper.connection = Mongo::Connection.new
4
+ MongoMapper.database = 'audited_test'
@@ -0,0 +1,174 @@
1
+ require 'cgi'
2
+ require 'mongo_mapper'
3
+ require File.expand_path('../connection', __FILE__)
4
+
5
+ module Models
6
+ module MongoMapper
7
+ class User
8
+ include ::MongoMapper::Document
9
+
10
+ key :name, String
11
+ key :username, String
12
+ key :password, String
13
+ key :activated, Boolean
14
+ key :suspended_at, Time
15
+ key :logins, Integer, :default => 0
16
+ timestamps!
17
+
18
+ audited :except => :password
19
+
20
+ attr_protected :logins
21
+
22
+ def name=(val)
23
+ write_attribute(:name, CGI.escapeHTML(val))
24
+ end
25
+ end
26
+
27
+ class CommentRequiredUser
28
+ include ::MongoMapper::Document
29
+
30
+ key :name, String
31
+ key :username, String
32
+ key :password, String
33
+ key :activated, Boolean
34
+ key :suspended_at, Time
35
+ key :logins, Integer, :default => 0
36
+ timestamps!
37
+
38
+ audited :comment_required => true
39
+ end
40
+
41
+ class UnprotectedUser
42
+ include ::MongoMapper::Document
43
+
44
+ key :name, String
45
+ key :username, String
46
+ key :password, String
47
+ key :activated, Boolean
48
+ key :suspended_at, Time
49
+ key :logins, Integer, :default => 0
50
+ timestamps!
51
+
52
+ audited :protect => false
53
+ attr_accessible :name, :username, :password
54
+ end
55
+
56
+ class AccessibleUser
57
+ include ::MongoMapper::Document
58
+
59
+ key :name, String
60
+ key :username, String
61
+ key :password, String
62
+ key :activated, Boolean
63
+ key :suspended_at, Time
64
+ key :logins, Integer, :default => 0
65
+ timestamps!
66
+
67
+ attr_accessible :name, :username, :password # declare attr_accessible before calling aaa
68
+ audited
69
+ end
70
+
71
+ class NoAttributeProtectionUser
72
+ include ::MongoMapper::Document
73
+
74
+ key :name, String
75
+ key :username, String
76
+ key :password, String
77
+ key :activated, Boolean
78
+ key :suspended_at, Time
79
+ key :logins, Integer, :default => 0
80
+ timestamps!
81
+
82
+ audited
83
+ end
84
+
85
+ class UserWithAfterAudit
86
+ include ::MongoMapper::Document
87
+
88
+ key :name, String
89
+ key :username, String
90
+ key :password, String
91
+ key :activated, Boolean
92
+ key :suspended_at, Time
93
+ key :logins, Integer, :default => 0
94
+ timestamps!
95
+
96
+ audited
97
+ attr_accessor :bogus_attr
98
+
99
+ def after_audit
100
+ self.bogus_attr = "do something"
101
+ end
102
+ end
103
+
104
+ class Company
105
+ include ::MongoMapper::Document
106
+
107
+ key :name, String
108
+ key :owner_id, ObjectId
109
+
110
+ audited
111
+ end
112
+
113
+ class Owner
114
+ include ::MongoMapper::Document
115
+
116
+ key :name, String
117
+ key :username, String
118
+ key :password, String
119
+ key :activated, Boolean
120
+ key :suspended_at, Time
121
+ key :logins, Integer, :default => 0
122
+ timestamps!
123
+
124
+ has_associated_audits
125
+ end
126
+
127
+ class OwnedCompany
128
+ include ::MongoMapper::Document
129
+
130
+ key :name, String
131
+ key :owner_id, ObjectId
132
+
133
+ belongs_to :owner, :class_name => "Owner"
134
+ attr_accessible :name, :owner # declare attr_accessible before calling aaa
135
+ audited :associated_with => :owner
136
+ end
137
+
138
+ class OnUpdateDestroy
139
+ include ::MongoMapper::Document
140
+
141
+ key :name, String
142
+ key :owner_id, ObjectId
143
+
144
+ audited :on => [:update, :destroy]
145
+ end
146
+
147
+ class OnCreateDestroy
148
+ include ::MongoMapper::Document
149
+
150
+ key :name, String
151
+ key :owner_id, ObjectId
152
+
153
+ audited :on => [:create, :destroy]
154
+ end
155
+
156
+ class OnCreateDestroyExceptName
157
+ include ::MongoMapper::Document
158
+
159
+ key :name, String
160
+ key :owner_id, ObjectId
161
+
162
+ audited :except => :name, :on => [:create, :destroy]
163
+ end
164
+
165
+ class OnCreateUpdate
166
+ include ::MongoMapper::Document
167
+
168
+ key :name, String
169
+ key :owner_id, ObjectId
170
+
171
+ audited :on => [:create, :update]
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,17 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :audits, :force => true do |t|
3
+ t.column :auditable_id, :integer
4
+ t.column :auditable_type, :string
5
+ t.column :user_id, :integer
6
+ t.column :user_type, :string
7
+ t.column :username, :string
8
+ t.column :action, :string
9
+ t.column :changes, :text
10
+ t.column :version, :integer, :default => 0
11
+ t.column :created_at, :datetime
12
+ end
13
+
14
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
15
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
16
+ add_index :audits, :created_at
17
+ end
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :audits, :force => true do |t|
3
+ t.column :auditable_id, :integer
4
+ t.column :auditable_type, :string
5
+ t.column :user_id, :integer
6
+ t.column :user_type, :string
7
+ t.column :username, :string
8
+ t.column :action, :string
9
+ t.column :changes, :text
10
+ t.column :version, :integer, :default => 0
11
+ t.column :comment, :string
12
+ t.column :created_at, :datetime
13
+ end
14
+
15
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
16
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
17
+ add_index :audits, :created_at
18
+ end
@@ -0,0 +1,19 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :audits, :force => true do |t|
3
+ t.column :auditable_id, :integer
4
+ t.column :auditable_type, :string
5
+ t.column :user_id, :integer
6
+ t.column :user_type, :string
7
+ t.column :username, :string
8
+ t.column :action, :string
9
+ t.column :audited_changes, :text
10
+ t.column :version, :integer, :default => 0
11
+ t.column :comment, :string
12
+ t.column :created_at, :datetime
13
+ end
14
+
15
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
16
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
17
+ add_index :audits, :created_at
18
+ end
19
+
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :audits, :force => true do |t|
3
+ t.column :auditable_id, :integer
4
+ t.column :auditable_type, :string
5
+ t.column :user_id, :integer
6
+ t.column :user_type, :string
7
+ t.column :username, :string
8
+ t.column :action, :string
9
+ t.column :audited_changes, :text
10
+ t.column :version, :integer, :default => 0
11
+ t.column :comment, :string
12
+ t.column :created_at, :datetime
13
+ t.column :remote_address, :string
14
+ end
15
+
16
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
17
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
18
+ add_index :audits, :created_at
19
+ end
20
+