htanata-acts_as_audited 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG +25 -0
- data/LICENSE +19 -0
- data/README +70 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/acts_as_audited.gemspec +73 -0
- data/generators/audited_migration/USAGE +7 -0
- data/generators/audited_migration/audited_migration_generator.rb +7 -0
- data/generators/audited_migration/templates/migration.rb +23 -0
- data/init.rb +1 -0
- data/lib/acts_as_audited.rb +242 -0
- data/lib/acts_as_audited/audit.rb +119 -0
- data/lib/acts_as_audited/audit_sweeper.rb +36 -0
- data/rails/init.rb +8 -0
- data/test/acts_as_audited_test.rb +380 -0
- data/test/audit_sweeper_test.rb +31 -0
- data/test/audit_test.rb +179 -0
- data/test/db/database.yml +21 -0
- data/test/db/schema.rb +32 -0
- data/test/test_helper.rb +75 -0
- metadata +135 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class AuditsController < ActionController::Base
|
4
|
+
def audit
|
5
|
+
@company = Company.create
|
6
|
+
render :nothing => true
|
7
|
+
end
|
8
|
+
|
9
|
+
def update_user
|
10
|
+
current_user.update_attributes({:password => 'foo'})
|
11
|
+
render :nothing => true
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
attr_accessor :current_user
|
16
|
+
end
|
17
|
+
AuditsController.view_paths = [File.dirname(__FILE__)]
|
18
|
+
ActionController::Routing::Routes.draw {|m| m.connect ':controller/:action/:id' }
|
19
|
+
|
20
|
+
class AuditsControllerTest < ActionController::TestCase
|
21
|
+
should "audit user" do
|
22
|
+
user = @controller.send(:current_user=, create_user)
|
23
|
+
lambda { post :audit }.should change { Audit.count }
|
24
|
+
assigns(:company).audits.last.user.should == user
|
25
|
+
end
|
26
|
+
|
27
|
+
should "not save blank audits" do
|
28
|
+
user = @controller.send(:current_user=, create_user)
|
29
|
+
lambda { post :update_user }.should_not change { Audit.count }
|
30
|
+
end
|
31
|
+
end
|
data/test/audit_test.rb
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class AuditTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@user = User.new :name => "testing"
|
6
|
+
@audit = Audit.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "user=" do
|
10
|
+
should "be able to set the user to a model object" do
|
11
|
+
@audit.user = @user
|
12
|
+
@audit.user.should == @user
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to set the user to nil" do
|
16
|
+
@audit.user_id = 1
|
17
|
+
@audit.user_type = 'User'
|
18
|
+
@audit.username = 'joe'
|
19
|
+
|
20
|
+
@audit.user = nil
|
21
|
+
|
22
|
+
@audit.user.should == nil
|
23
|
+
@audit.user_id.should == nil
|
24
|
+
@audit.user_type.should == nil
|
25
|
+
@audit.username.should == nil
|
26
|
+
end
|
27
|
+
|
28
|
+
should "be able to set the user to a string" do
|
29
|
+
@audit.user = 'testing'
|
30
|
+
@audit.user.should == 'testing'
|
31
|
+
end
|
32
|
+
|
33
|
+
should "clear model when setting to a string" do
|
34
|
+
@audit.user = @user
|
35
|
+
@audit.user = 'testing'
|
36
|
+
@audit.user_id.should be(nil)
|
37
|
+
@audit.user_type.should be(nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "clear the username when setting to a model" do
|
41
|
+
@audit.username = 'testing'
|
42
|
+
@audit.user = @user
|
43
|
+
@audit.username.should be(nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "revision" do
|
49
|
+
should "recreate attributes" do
|
50
|
+
user = User.create :name => "1"
|
51
|
+
5.times {|i| user.update_attribute :name, (i + 2).to_s }
|
52
|
+
user.audits.each do |audit|
|
53
|
+
audit.revision.name.should == audit.version.to_s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
should "set protected attributes" do
|
58
|
+
u = User.create(:name => 'Brandon')
|
59
|
+
u.update_attribute :logins, 1
|
60
|
+
u.update_attribute :logins, 2
|
61
|
+
|
62
|
+
u.audits[2].revision.logins.should == 2
|
63
|
+
u.audits[1].revision.logins.should == 1
|
64
|
+
u.audits[0].revision.logins.should == 0
|
65
|
+
end
|
66
|
+
|
67
|
+
should "bypass attribute assignment wrappers" do
|
68
|
+
u = User.create(:name => '<Joe>')
|
69
|
+
u.audits.first.revision.name.should == '<Joe>'
|
70
|
+
end
|
71
|
+
|
72
|
+
should "work for deleted records" do
|
73
|
+
user = User.create :name => "1"
|
74
|
+
user.destroy
|
75
|
+
revision = user.audits.last.revision
|
76
|
+
revision.name.should == user.name
|
77
|
+
revision.new_record?.should be(true)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
should "set the version number on create" do
|
82
|
+
user = User.create! :name => "Set Version Number"
|
83
|
+
user.audits.first.version.should == 1
|
84
|
+
user.update_attribute :name, "Set to 2"
|
85
|
+
user.audits(true).first.version.should == 1
|
86
|
+
user.audits(true).last.version.should == 2
|
87
|
+
user.destroy
|
88
|
+
user.audits(true).last.version.should == 3
|
89
|
+
end
|
90
|
+
|
91
|
+
context "reconstruct_attributes" do
|
92
|
+
should "work with with old way of storing just the new value" do
|
93
|
+
audits = Audit.reconstruct_attributes([Audit.new(:changes => {'attribute' => 'value'})])
|
94
|
+
audits['attribute'].should == 'value'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "audited_classes" do
|
99
|
+
class CustomUser < ActiveRecord::Base
|
100
|
+
end
|
101
|
+
class CustomUserSubclass < CustomUser
|
102
|
+
acts_as_audited
|
103
|
+
end
|
104
|
+
|
105
|
+
should "include audited classes" do
|
106
|
+
Audit.audited_classes.should include(User)
|
107
|
+
end
|
108
|
+
|
109
|
+
should "include subclasses" do
|
110
|
+
Audit.audited_classes.should include(CustomUserSubclass)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "new_attributes" do
|
115
|
+
should "return a hash of the new values" do
|
116
|
+
Audit.new(:changes => {:a => [1, 2], :b => [3, 4]}).new_attributes.should == {'a' => 2, 'b' => 4}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "old_attributes" do
|
121
|
+
should "return a hash of the old values" do
|
122
|
+
Audit.new(:changes => {:a => [1, 2], :b => [3, 4]}).old_attributes.should == {'a' => 1, 'b' => 3}
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "as_user" do
|
127
|
+
setup do
|
128
|
+
@user = User.create :name => 'testing'
|
129
|
+
end
|
130
|
+
|
131
|
+
should "record user objects" do
|
132
|
+
Audit.as_user(@user) do
|
133
|
+
company = Company.create :name => 'The auditors'
|
134
|
+
company.name = 'The Auditors, Inc'
|
135
|
+
company.save
|
136
|
+
|
137
|
+
company.audits.each do |audit|
|
138
|
+
audit.user.should == @user
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
should "record usernames" do
|
144
|
+
Audit.as_user(@user.name) do
|
145
|
+
company = Company.create :name => 'The auditors'
|
146
|
+
company.name = 'The Auditors, Inc'
|
147
|
+
company.save
|
148
|
+
|
149
|
+
company.audits.each do |audit|
|
150
|
+
audit.username.should == @user.name
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
should "be thread safe" do
|
156
|
+
begin
|
157
|
+
t1 = Thread.new do
|
158
|
+
Audit.as_user(@user) do
|
159
|
+
sleep 1
|
160
|
+
Company.create(:name => 'The Auditors, Inc').audits.first.user.should == @user
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
t2 = Thread.new do
|
165
|
+
Audit.as_user(@user.name) do
|
166
|
+
Company.create(:name => 'The Competing Auditors, LLC').audits.first.username.should == @user.name
|
167
|
+
sleep 0.5
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
t1.join
|
172
|
+
t2.join
|
173
|
+
rescue ActiveRecord::StatementInvalid
|
174
|
+
STDERR.puts "Thread safety tests cannot be run with SQLite"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
sqlite:
|
2
|
+
adapter: sqlite
|
3
|
+
database: acts_as_audited_plugin.sqlite.db
|
4
|
+
sqlite3mem:
|
5
|
+
adapter: sqlite3
|
6
|
+
database: ":memory:"
|
7
|
+
sqlite3:
|
8
|
+
adapter: sqlite3
|
9
|
+
database: acts_as_audited_plugin.sqlite3.db
|
10
|
+
postgresql:
|
11
|
+
adapter: postgresql
|
12
|
+
username: postgres
|
13
|
+
password: postgres
|
14
|
+
database: acts_as_audited_plugin_test
|
15
|
+
min_messages: ERROR
|
16
|
+
mysql:
|
17
|
+
adapter: mysql
|
18
|
+
host: localhost
|
19
|
+
username: root
|
20
|
+
password:
|
21
|
+
database: acts_as_audited_plugin_test
|
data/test/db/schema.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :users, :force => true do |t|
|
3
|
+
t.column :name, :string
|
4
|
+
t.column :username, :string
|
5
|
+
t.column :password, :string
|
6
|
+
t.column :activated, :boolean
|
7
|
+
t.column :suspended_at, :datetime
|
8
|
+
t.column :logins, :integer, :default => 0
|
9
|
+
t.column :created_at, :datetime
|
10
|
+
t.column :updated_at, :datetime
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :companies, :force => true do |t|
|
14
|
+
t.column :name, :string
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :audits, :force => true do |t|
|
18
|
+
t.column :auditable_id, :integer
|
19
|
+
t.column :auditable_type, :string
|
20
|
+
t.column :user_id, :integer
|
21
|
+
t.column :user_type, :string
|
22
|
+
t.column :username, :string
|
23
|
+
t.column :action, :string
|
24
|
+
t.column :changes, :text
|
25
|
+
t.column :version, :integer, :default => 0
|
26
|
+
t.column :created_at, :datetime
|
27
|
+
end
|
28
|
+
|
29
|
+
add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
|
30
|
+
add_index :audits, [:user_id, :user_type], :name => 'user_index'
|
31
|
+
add_index :audits, :created_at
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
require 'rubygems'
|
4
|
+
require 'multi_rails_init'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/version'
|
7
|
+
require 'active_record/fixtures'
|
8
|
+
require 'action_controller'
|
9
|
+
require 'action_controller/test_process'
|
10
|
+
require 'action_view'
|
11
|
+
require 'test/unit'
|
12
|
+
require 'shoulda'
|
13
|
+
|
14
|
+
gem 'jnunemaker-matchy'
|
15
|
+
require 'matchy'
|
16
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
17
|
+
|
18
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/db/database.yml'))
|
19
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
20
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3mem'])
|
21
|
+
ActiveRecord::Migration.verbose = false
|
22
|
+
load(File.dirname(__FILE__) + "/db/schema.rb")
|
23
|
+
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
acts_as_audited :except => :password
|
26
|
+
|
27
|
+
attr_protected :logins
|
28
|
+
|
29
|
+
def name=(val)
|
30
|
+
write_attribute(:name, CGI.escapeHTML(val))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Company < ActiveRecord::Base
|
35
|
+
acts_as_audited
|
36
|
+
end
|
37
|
+
|
38
|
+
class OnUpdateDestroy < ActiveRecord::Base
|
39
|
+
set_table_name 'companies'
|
40
|
+
acts_as_audited :on => [:update, :destroy]
|
41
|
+
end
|
42
|
+
|
43
|
+
class OnCreateDestroy < ActiveRecord::Base
|
44
|
+
set_table_name 'companies'
|
45
|
+
acts_as_audited :on => [:create, :destroy]
|
46
|
+
end
|
47
|
+
|
48
|
+
class OnCreateDestroyExceptName < ActiveRecord::Base
|
49
|
+
set_table_name 'companies'
|
50
|
+
acts_as_audited :except => :name, :on => [:create, :destroy]
|
51
|
+
end
|
52
|
+
|
53
|
+
class OnCreateUpdate < ActiveRecord::Base
|
54
|
+
set_table_name 'companies'
|
55
|
+
acts_as_audited :on => [:create, :update]
|
56
|
+
end
|
57
|
+
|
58
|
+
class Test::Unit::TestCase
|
59
|
+
# def change(receiver=nil, message=nil, &block)
|
60
|
+
# ChangeExpectation.new(self, receiver, message, &block)
|
61
|
+
# end
|
62
|
+
|
63
|
+
def create_user(attrs = {})
|
64
|
+
User.create({:name => 'Brandon', :username => 'brandon', :password => 'password'}.merge(attrs))
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_versions(n = 2)
|
68
|
+
returning User.create(:name => 'Foobar 1') do |u|
|
69
|
+
(n - 1).times do |i|
|
70
|
+
u.update_attribute :name, "Foobar #{i + 2}"
|
71
|
+
end
|
72
|
+
u.reload
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: htanata-acts_as_audited
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brandon Keepers
|
14
|
+
- Hendy Tanata
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-06-02 00:00:00 +08:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: activerecord
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 1
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 1
|
34
|
+
version: "2.1"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: thoughtbot-shoulda
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: jnunemaker-matchy
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description:
|
66
|
+
email: htanata@gmail.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- LICENSE
|
73
|
+
- README
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- CHANGELOG
|
77
|
+
- LICENSE
|
78
|
+
- README
|
79
|
+
- Rakefile
|
80
|
+
- VERSION
|
81
|
+
- acts_as_audited.gemspec
|
82
|
+
- generators/audited_migration/USAGE
|
83
|
+
- generators/audited_migration/audited_migration_generator.rb
|
84
|
+
- generators/audited_migration/templates/migration.rb
|
85
|
+
- init.rb
|
86
|
+
- lib/acts_as_audited.rb
|
87
|
+
- lib/acts_as_audited/audit.rb
|
88
|
+
- lib/acts_as_audited/audit_sweeper.rb
|
89
|
+
- rails/init.rb
|
90
|
+
- test/acts_as_audited_test.rb
|
91
|
+
- test/audit_sweeper_test.rb
|
92
|
+
- test/audit_test.rb
|
93
|
+
- test/db/database.yml
|
94
|
+
- test/db/schema.rb
|
95
|
+
- test/test_helper.rb
|
96
|
+
has_rdoc: true
|
97
|
+
homepage: http://github.com/htanata/acts_as_audited
|
98
|
+
licenses: []
|
99
|
+
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options:
|
102
|
+
- --charset=UTF-8
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
hash: 3
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
version: "0"
|
123
|
+
requirements: []
|
124
|
+
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.3.7
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: ActiveRecord extension that logs all changes to your models in an audits table
|
130
|
+
test_files:
|
131
|
+
- test/test_helper.rb
|
132
|
+
- test/audit_sweeper_test.rb
|
133
|
+
- test/db/schema.rb
|
134
|
+
- test/audit_test.rb
|
135
|
+
- test/acts_as_audited_test.rb
|