auditing 1.1.3 → 1.2.0
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.
- data/.rvmrc +5 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +25 -24
- data/README.rdoc +69 -4
- data/Rakefile +13 -13
- data/VERSION +1 -1
- data/auditing.gemspec +38 -28
- data/lib/auditing/auditor.rb +2 -1
- data/lib/auditing/base.rb +3 -0
- data/spec/auditing/with_users_spec.rb +37 -0
- data/spec/schema.rb +6 -2
- data/spec/spec_helper.rb +3 -0
- metadata +58 -10
- data/.gitignore +0 -22
data/.rvmrc
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,36 +1,37 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.0.
|
5
|
-
activesupport (= 3.0.
|
4
|
+
activemodel (3.0.3)
|
5
|
+
activesupport (= 3.0.3)
|
6
6
|
builder (~> 2.1.2)
|
7
|
-
i18n (~> 0.4
|
8
|
-
activerecord (3.0.
|
9
|
-
activemodel (= 3.0.
|
10
|
-
activesupport (= 3.0.
|
11
|
-
arel (~>
|
7
|
+
i18n (~> 0.4)
|
8
|
+
activerecord (3.0.3)
|
9
|
+
activemodel (= 3.0.3)
|
10
|
+
activesupport (= 3.0.3)
|
11
|
+
arel (~> 2.0.2)
|
12
12
|
tzinfo (~> 0.3.23)
|
13
|
-
activesupport (3.0.
|
14
|
-
arel (
|
15
|
-
activesupport (>= 3.0.0.beta)
|
13
|
+
activesupport (3.0.3)
|
14
|
+
arel (2.0.7)
|
16
15
|
builder (2.1.2)
|
17
16
|
diff-lcs (1.1.2)
|
18
|
-
i18n (0.
|
19
|
-
rspec (2.
|
20
|
-
rspec-core (
|
21
|
-
rspec-expectations (
|
22
|
-
rspec-mocks (
|
23
|
-
rspec-core (2.
|
24
|
-
rspec-expectations (2.
|
25
|
-
diff-lcs (
|
26
|
-
rspec-mocks (2.
|
27
|
-
sqlite3
|
28
|
-
|
17
|
+
i18n (0.5.0)
|
18
|
+
rspec (2.4.0)
|
19
|
+
rspec-core (~> 2.4.0)
|
20
|
+
rspec-expectations (~> 2.4.0)
|
21
|
+
rspec-mocks (~> 2.4.0)
|
22
|
+
rspec-core (2.4.0)
|
23
|
+
rspec-expectations (2.4.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.4.0)
|
26
|
+
sqlite3 (1.3.3)
|
27
|
+
sqlite3-ruby (1.3.3)
|
28
|
+
sqlite3 (>= 1.3.3)
|
29
|
+
tzinfo (0.3.24)
|
29
30
|
|
30
31
|
PLATFORMS
|
31
32
|
ruby
|
32
33
|
|
33
34
|
DEPENDENCIES
|
34
|
-
activerecord
|
35
|
-
rspec
|
36
|
-
sqlite3-ruby
|
35
|
+
activerecord
|
36
|
+
rspec
|
37
|
+
sqlite3-ruby
|
data/README.rdoc
CHANGED
@@ -9,7 +9,11 @@ http://github.com/bcantin/auditing_project
|
|
9
9
|
|
10
10
|
gem install auditing
|
11
11
|
|
12
|
-
You will have to supply a model named Audit in your rails application
|
12
|
+
You will have to supply a model named Audit in your rails application
|
13
|
+
|
14
|
+
rails g model audit
|
15
|
+
|
16
|
+
Here is a migration for your audit model following migration
|
13
17
|
|
14
18
|
create_table :audits, :force => true do |t|
|
15
19
|
t.string :action
|
@@ -25,9 +29,9 @@ You will have to supply a model named Audit in your rails application with the f
|
|
25
29
|
t.timestamps
|
26
30
|
end
|
27
31
|
|
28
|
-
If you want to track the user, uncomment the t.integer :user_id above.
|
32
|
+
If you want to track the user, uncomment the t.integer :user_id above. See the Tracking Users section below.
|
29
33
|
|
30
|
-
|
34
|
+
Update your Audit class
|
31
35
|
|
32
36
|
class Audit < ActiveRecord::Base
|
33
37
|
include Auditing::Auditor
|
@@ -77,7 +81,67 @@ If your relationship is polymorphic, you can supply an array of classes
|
|
77
81
|
that you only want to log.
|
78
82
|
|
79
83
|
audit_relationship_enabled :only => [Company, Person]
|
84
|
+
|
85
|
+
== Tracking Users
|
86
|
+
|
87
|
+
NOTE: there is probably a more elegant solution to this, if you know of
|
88
|
+
one, please drop me a line.
|
89
|
+
|
90
|
+
NOTE: currently this only works with a model called User. I welcome patches
|
91
|
+
to change this.
|
92
|
+
|
93
|
+
To track which logged in users have made the changes, you will need to have a
|
94
|
+
user_id column in your audits table. If you did not add it when you created
|
95
|
+
the audits table, you can add it in another migration
|
96
|
+
|
97
|
+
add_column :audits, :user_id, :integer
|
98
|
+
|
99
|
+
Your user class should respond to a class method called current_user
|
100
|
+
and current_user= (some authentication systems do this, others do not).
|
101
|
+
|
102
|
+
Here is a suggested method
|
103
|
+
|
104
|
+
class User < ActiveRecord::Base
|
105
|
+
|
106
|
+
def self.current_user=(current_user)
|
107
|
+
@current_user = current_user
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.current_user
|
111
|
+
@current_user
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
Your ApplicationController should also set the current user so the auditing gem can
|
117
|
+
get the current user properly
|
118
|
+
|
119
|
+
class ApplicationController < ActionController::Base
|
120
|
+
|
121
|
+
before_filter :set_current_user
|
122
|
+
after_filter :unser_current_user
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def set_current_user
|
127
|
+
User.current_user = current_user
|
128
|
+
end
|
129
|
+
|
130
|
+
def unset_current_user
|
131
|
+
User.current_user = nil
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
80
135
|
|
136
|
+
== Testing
|
137
|
+
|
138
|
+
To run the tests, first clone this repo, then
|
139
|
+
|
140
|
+
cd auditing
|
141
|
+
bundle
|
142
|
+
rspec spec
|
143
|
+
|
144
|
+
|
81
145
|
== Note on Patches/Pull Requests
|
82
146
|
|
83
147
|
* Fork the project.
|
@@ -85,7 +149,8 @@ that you only want to log.
|
|
85
149
|
* Add tests for it. This is important so I don't break it in a
|
86
150
|
future version unintentionally.
|
87
151
|
* Commit, do not mess with rakefile, version, or history.
|
88
|
-
(if you want to have your own version, that is fine but bump version in
|
152
|
+
(if you want to have your own version, that is fine but bump version in
|
153
|
+
a commit by itself I can ignore when I pull)
|
89
154
|
* Send me a pull request. Bonus points for topic branches.
|
90
155
|
|
91
156
|
== Copyright
|
data/Rakefile
CHANGED
@@ -18,19 +18,19 @@ rescue LoadError
|
|
18
18
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
19
|
end
|
20
20
|
|
21
|
-
require 'spec/rake/spectask'
|
22
|
-
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
task :spec => :check_dependencies
|
21
|
+
# require 'spec/rake/spectask'
|
22
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
# spec.libs << 'lib' << 'spec'
|
24
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
# spec.libs << 'lib' << 'spec'
|
29
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
# spec.rcov = true
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# task :spec => :check_dependencies
|
34
34
|
|
35
35
|
task :default => :spec
|
36
36
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/auditing.gemspec
CHANGED
@@ -1,54 +1,55 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{auditing}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brad Cantin"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-02-01}
|
13
13
|
s.description = %q{acts_as_versioned is good. This allows an attribute level rollback instead}
|
14
14
|
s.email = %q{brad.cantin@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".bundle/config",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
21
|
+
".document",
|
22
|
+
".rspec",
|
23
|
+
".rvmrc",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"auditing.gemspec",
|
31
|
+
"lib/auditing.rb",
|
32
|
+
"lib/auditing/audit_relationship.rb",
|
33
|
+
"lib/auditing/auditor.rb",
|
34
|
+
"lib/auditing/base.rb",
|
35
|
+
"spec/auditing/audit_relationship_spec.rb",
|
36
|
+
"spec/auditing/auditor_spec.rb",
|
37
|
+
"spec/auditing/base_spec.rb",
|
38
|
+
"spec/auditing/with_users_spec.rb",
|
39
|
+
"spec/schema.rb",
|
40
|
+
"spec/spec_helper.rb"
|
40
41
|
]
|
41
42
|
s.homepage = %q{http://github.com/bcantin/auditing}
|
42
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
43
43
|
s.require_paths = ["lib"]
|
44
44
|
s.rubygems_version = %q{1.3.7}
|
45
45
|
s.summary = %q{A gem to keep track of audit hisory of a record}
|
46
46
|
s.test_files = [
|
47
47
|
"spec/auditing/audit_relationship_spec.rb",
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
"spec/auditing/auditor_spec.rb",
|
49
|
+
"spec/auditing/base_spec.rb",
|
50
|
+
"spec/auditing/with_users_spec.rb",
|
51
|
+
"spec/schema.rb",
|
52
|
+
"spec/spec_helper.rb"
|
52
53
|
]
|
53
54
|
|
54
55
|
if s.respond_to? :specification_version then
|
@@ -56,11 +57,20 @@ Gem::Specification.new do |s|
|
|
56
57
|
s.specification_version = 3
|
57
58
|
|
58
59
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_development_dependency(%q<activerecord>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
59
63
|
s.add_development_dependency(%q<rspec>, [">= 2.0"])
|
60
64
|
else
|
65
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
66
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
67
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
61
68
|
s.add_dependency(%q<rspec>, [">= 2.0"])
|
62
69
|
end
|
63
70
|
else
|
71
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
72
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
73
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
64
74
|
s.add_dependency(%q<rspec>, [">= 2.0"])
|
65
75
|
end
|
66
76
|
end
|
data/lib/auditing/auditor.rb
CHANGED
data/lib/auditing/base.rb
CHANGED
@@ -70,6 +70,9 @@ module Auditing
|
|
70
70
|
|
71
71
|
private
|
72
72
|
def add_audit(hash={})
|
73
|
+
if User.respond_to?(:current_user) && !User.current_user.blank?
|
74
|
+
hash[:user_id] = User.current_user.id
|
75
|
+
end
|
73
76
|
Audit.create!({:auditable => self}.merge(hash))
|
74
77
|
end
|
75
78
|
end # Auditing::InstanceMethods
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "auditing with users" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class School < ActiveRecord::Base
|
7
|
+
audit_enabled
|
8
|
+
end
|
9
|
+
|
10
|
+
class User < ActiveRecord::Base
|
11
|
+
def self.current_user=(user)
|
12
|
+
@current_user = user
|
13
|
+
end
|
14
|
+
def self.current_user
|
15
|
+
@current_user
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@user1 = User.create(:email=>'foo@example.com')
|
20
|
+
@user2 = User.create(:email=>'bar@example.com')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "adds the user to the audit" do
|
24
|
+
User.current_user = @user1
|
25
|
+
school = School.create(:name => 'PS118')
|
26
|
+
a = school.audits.first
|
27
|
+
a.user_id.should == @user1.id
|
28
|
+
User.current_user = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "does not add the user to the audit if User.current_user is not set" do
|
32
|
+
school = School.create(:name => 'PS118')
|
33
|
+
a = school.audits.first
|
34
|
+
a.user_id.should == nil
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/spec/schema.rb
CHANGED
@@ -9,7 +9,7 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
9
9
|
t.string :field_name
|
10
10
|
t.string :old_value
|
11
11
|
t.string :new_value
|
12
|
-
|
12
|
+
t.integer :user_id
|
13
13
|
t.boolean :undoable, :default => true
|
14
14
|
t.timestamps
|
15
15
|
end
|
@@ -56,5 +56,9 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
56
56
|
t.string :start_date
|
57
57
|
t.timestamps
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
|
+
create_table :users do |t|
|
61
|
+
t.string :email
|
62
|
+
end
|
63
|
+
|
60
64
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auditing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Brad Cantin
|
@@ -14,23 +15,66 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-02-01 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
name: activerecord
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
name: sqlite3-ruby
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
requirement: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
21
52
|
name: rspec
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirement: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
type: :development
|
22
65
|
prerelease: false
|
23
|
-
|
66
|
+
name: rspec
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
24
68
|
none: false
|
25
69
|
requirements:
|
26
70
|
- - ">="
|
27
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
28
73
|
segments:
|
29
74
|
- 2
|
30
75
|
- 0
|
31
76
|
version: "2.0"
|
32
|
-
|
33
|
-
version_requirements: *id001
|
77
|
+
requirement: *id004
|
34
78
|
description: acts_as_versioned is good. This allows an attribute level rollback instead
|
35
79
|
email: brad.cantin@gmail.com
|
36
80
|
executables: []
|
@@ -43,8 +87,8 @@ extra_rdoc_files:
|
|
43
87
|
files:
|
44
88
|
- .bundle/config
|
45
89
|
- .document
|
46
|
-
- .gitignore
|
47
90
|
- .rspec
|
91
|
+
- .rvmrc
|
48
92
|
- Gemfile
|
49
93
|
- Gemfile.lock
|
50
94
|
- LICENSE
|
@@ -59,6 +103,7 @@ files:
|
|
59
103
|
- spec/auditing/audit_relationship_spec.rb
|
60
104
|
- spec/auditing/auditor_spec.rb
|
61
105
|
- spec/auditing/base_spec.rb
|
106
|
+
- spec/auditing/with_users_spec.rb
|
62
107
|
- spec/schema.rb
|
63
108
|
- spec/spec_helper.rb
|
64
109
|
has_rdoc: true
|
@@ -66,8 +111,8 @@ homepage: http://github.com/bcantin/auditing
|
|
66
111
|
licenses: []
|
67
112
|
|
68
113
|
post_install_message:
|
69
|
-
rdoc_options:
|
70
|
-
|
114
|
+
rdoc_options: []
|
115
|
+
|
71
116
|
require_paths:
|
72
117
|
- lib
|
73
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -75,6 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
120
|
requirements:
|
76
121
|
- - ">="
|
77
122
|
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
78
124
|
segments:
|
79
125
|
- 0
|
80
126
|
version: "0"
|
@@ -83,6 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
129
|
requirements:
|
84
130
|
- - ">="
|
85
131
|
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
86
133
|
segments:
|
87
134
|
- 0
|
88
135
|
version: "0"
|
@@ -97,5 +144,6 @@ test_files:
|
|
97
144
|
- spec/auditing/audit_relationship_spec.rb
|
98
145
|
- spec/auditing/auditor_spec.rb
|
99
146
|
- spec/auditing/base_spec.rb
|
147
|
+
- spec/auditing/with_users_spec.rb
|
100
148
|
- spec/schema.rb
|
101
149
|
- spec/spec_helper.rb
|