auditing 1.3.1 → 1.4.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/.bundle/config +2 -2
- data/.rvmrc.rbx +1 -0
- data/Gemfile.lock +9 -9
- data/README.markdown +41 -16
- data/lib/auditing.rb +10 -0
- data/lib/auditing/base.rb +6 -5
- data/lib/auditing/version.rb +1 -1
- data/lib/generators/auditing/USAGE +1 -0
- data/lib/generators/auditing/install_generator.rb +4 -0
- data/lib/generators/auditing/templates/auditing.rb +4 -0
- data/lib/generators/auditing/upgrade_generator.rb +4 -0
- data/spec/auditing/config_report_spec.rb +82 -0
- data/spec/schema.rb +4 -0
- data/spec/spec_helper.rb +6 -3
- data/todo.txt +27 -0
- metadata +7 -4
- data/spec/auditing/with_users_spec.rb +0 -37
data/.bundle/config
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
---
|
2
|
-
|
1
|
+
--- {}
|
2
|
+
|
data/.rvmrc.rbx
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use rbx@auditing
|
data/Gemfile.lock
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
auditing (1.
|
4
|
+
auditing (1.4.0)
|
5
5
|
activerecord (~> 3.0.0)
|
6
6
|
activesupport (~> 3.0.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (3.0.
|
12
|
-
activesupport (= 3.0.
|
11
|
+
activemodel (3.0.7)
|
12
|
+
activesupport (= 3.0.7)
|
13
13
|
builder (~> 2.1.2)
|
14
14
|
i18n (~> 0.5.0)
|
15
|
-
activerecord (3.0.
|
16
|
-
activemodel (= 3.0.
|
17
|
-
activesupport (= 3.0.
|
15
|
+
activerecord (3.0.7)
|
16
|
+
activemodel (= 3.0.7)
|
17
|
+
activesupport (= 3.0.7)
|
18
18
|
arel (~> 2.0.2)
|
19
19
|
tzinfo (~> 0.3.23)
|
20
|
-
activesupport (3.0.
|
21
|
-
arel (2.0.
|
20
|
+
activesupport (3.0.7)
|
21
|
+
arel (2.0.10)
|
22
22
|
builder (2.1.2)
|
23
23
|
diff-lcs (1.1.2)
|
24
24
|
i18n (0.5.0)
|
@@ -35,7 +35,7 @@ GEM
|
|
35
35
|
sqlite3-ruby (1.3.3)
|
36
36
|
sqlite3 (>= 1.3.3)
|
37
37
|
timecop (0.3.5)
|
38
|
-
tzinfo (0.3.
|
38
|
+
tzinfo (0.3.27)
|
39
39
|
|
40
40
|
PLATFORMS
|
41
41
|
ruby
|
data/README.markdown
CHANGED
@@ -24,7 +24,7 @@ and, of course, to use it
|
|
24
24
|
If you want to track the user, uncomment the t.integer :user_id above. See the Tracking Users section below.
|
25
25
|
|
26
26
|
|
27
|
-
## Upgrading to 1.
|
27
|
+
## Upgrading to 1.4.0
|
28
28
|
|
29
29
|
New installs will not need to do this. Anyone that is using 1.2.X and below should upgrade
|
30
30
|
|
@@ -34,6 +34,7 @@ New installs will not need to do this. Anyone that is using 1.2.X and below sho
|
|
34
34
|
rake db:migrate
|
35
35
|
|
36
36
|
This will create a new migration to update the :old_value and :new_value attribute from STRING to TEXT
|
37
|
+
It will also create an initializer (see Tracking Users section below)
|
37
38
|
|
38
39
|
|
39
40
|
## Basic Usage
|
@@ -80,32 +81,58 @@ that you only want to log.
|
|
80
81
|
|
81
82
|
## Tracking Users
|
82
83
|
|
83
|
-
NOTE: there is probably a more elegant solution to this, if you know of
|
84
|
-
one, please drop me a line.
|
85
|
-
|
86
|
-
NOTE: currently this only works with a model called User. I welcome patches
|
87
|
-
to change this.
|
88
|
-
|
89
84
|
To track which logged in users have made the changes, you will need to have a
|
90
85
|
user_id column in your audits table. If you did not add it when you created
|
91
86
|
the audits table, you can add it in another migration
|
92
87
|
|
93
88
|
add_column :audits, :user_id, :integer
|
94
89
|
|
95
|
-
|
90
|
+
To let the auditing gem know which model to attribute the changes to, you will
|
91
|
+
need to do some minor setup. Edit the config/initializers/auditing.rb file
|
92
|
+
created during the installation. If you do not have that file, it should
|
93
|
+
look like this...
|
94
|
+
|
95
|
+
Auditing.configure do |config|
|
96
|
+
config.report_on = nil # User
|
97
|
+
config.report_method = nil # 'current_user'
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
You will need to set the config.report_on to the model you are using. Most
|
102
|
+
applications use a model called User and a method called 'current_user'. If
|
103
|
+
this is your case, then your config file should be updated to look like this...
|
104
|
+
|
105
|
+
Auditing.configure do |config|
|
106
|
+
config.report_on = User
|
107
|
+
config.report_method = 'current_user'
|
108
|
+
end
|
109
|
+
|
110
|
+
If your application uses something else, say a model called Member, and you
|
111
|
+
have a method to get the logged in member called 'my_awesome_member' then your
|
112
|
+
config file should be updated to look like this...
|
113
|
+
|
114
|
+
Auditing.configure do |config|
|
115
|
+
config.report_on = Member
|
116
|
+
config.report_method = 'my_awesome_member'
|
117
|
+
end
|
118
|
+
|
119
|
+
You will need to add the relationship to the Audit class and to your User / Member /
|
120
|
+
(Insert your custom model here)
|
96
121
|
|
97
122
|
class Audit < ActiveRecord::Base
|
98
123
|
belongs_to :user
|
99
124
|
end
|
100
125
|
|
101
|
-
If you want to see all the audits for a particular user, you can
|
102
|
-
add the has_many relationship to the User model as well
|
103
|
-
|
104
126
|
class User < ActiveRecord::Base
|
105
127
|
has_many :audits
|
106
128
|
end
|
107
129
|
|
108
|
-
|
130
|
+
You can alter the suggested implementation below to suite your needs. I am showing
|
131
|
+
a model named User and a method called current_user.
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
Your User class should respond to a class method called current_user
|
109
136
|
and current_user= (some authentication systems do this, others do not).
|
110
137
|
|
111
138
|
Here is a suggested method
|
@@ -122,11 +149,10 @@ Here is a suggested method
|
|
122
149
|
|
123
150
|
end
|
124
151
|
|
125
|
-
Your ApplicationController should also set the
|
126
|
-
get the
|
152
|
+
Your ApplicationController should also set the current_user so the auditing gem can
|
153
|
+
get the current_user properly
|
127
154
|
|
128
155
|
class ApplicationController < ActionController::Base
|
129
|
-
|
130
156
|
before_filter :set_current_user
|
131
157
|
after_filter :unset_current_user
|
132
158
|
|
@@ -139,7 +165,6 @@ get the current user properly
|
|
139
165
|
def unset_current_user
|
140
166
|
User.current_user = nil
|
141
167
|
end
|
142
|
-
|
143
168
|
end
|
144
169
|
|
145
170
|
## Testing
|
data/lib/auditing.rb
CHANGED
data/lib/auditing/base.rb
CHANGED
@@ -28,8 +28,8 @@ module Auditing
|
|
28
28
|
end
|
29
29
|
|
30
30
|
module InstanceMethods
|
31
|
+
|
31
32
|
# http://stackoverflow.com/questions/1906421/problem-saving-rails-marshal-in-sqlite3-db
|
32
|
-
|
33
33
|
def dump_data(value)
|
34
34
|
ActiveSupport::Base64.encode64(Marshal.dump(value))
|
35
35
|
end
|
@@ -46,8 +46,7 @@ module Auditing
|
|
46
46
|
add_audit(:action => 'updated',
|
47
47
|
:field_name => field,
|
48
48
|
:old_value => dump_data(change[0]),
|
49
|
-
:new_value => dump_data(change[1])
|
50
|
-
)
|
49
|
+
:new_value => dump_data(change[1]) )
|
51
50
|
end
|
52
51
|
end
|
53
52
|
end
|
@@ -85,8 +84,10 @@ module Auditing
|
|
85
84
|
|
86
85
|
private
|
87
86
|
def add_audit(hash={})
|
88
|
-
|
89
|
-
|
87
|
+
unless Auditing.report_on.nil?
|
88
|
+
if class_exists?(Auditing.report_on.to_s) && Auditing.report_on.respond_to?(Auditing.report_method.to_sym) && !Auditing.report_on.send(Auditing.report_method.to_sym).blank?
|
89
|
+
hash[:user_id] = Auditing.report_on.send(Auditing.report_method.to_sym).id
|
90
|
+
end
|
90
91
|
end
|
91
92
|
Audit.create!({:auditable => self}.merge(hash))
|
92
93
|
end
|
data/lib/auditing/version.rb
CHANGED
@@ -12,6 +12,10 @@ module Auditing
|
|
12
12
|
migration_template 'update_migration.rb', "db/migrate/update_audits.rb"
|
13
13
|
end
|
14
14
|
|
15
|
+
def create_initializer
|
16
|
+
template 'auditing.rb', 'config/initializers/auditing.rb'
|
17
|
+
end
|
18
|
+
|
15
19
|
private
|
16
20
|
|
17
21
|
def self.next_migration_number(dirname) #:nodoc:
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "auditing configuration" do
|
4
|
+
|
5
|
+
describe ":report_on => User; :report_method => 'current_user'" do
|
6
|
+
before do
|
7
|
+
class School < ActiveRecord::Base
|
8
|
+
audit_enabled
|
9
|
+
end
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
def self.current_user=(user)
|
13
|
+
@current_user = user
|
14
|
+
end
|
15
|
+
def self.current_user
|
16
|
+
@current_user
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Auditing.configure do |config|
|
21
|
+
config.report_on = User
|
22
|
+
config.report_method = 'current_user'
|
23
|
+
end
|
24
|
+
|
25
|
+
@user1 = User.create(:email=>'foo@example.com')
|
26
|
+
@user2 = User.create(:email=>'bar@example.com')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "adds the user to the audit" do
|
30
|
+
User.current_user = @user1
|
31
|
+
school = School.create(:name => 'PS118')
|
32
|
+
a = school.audits.first
|
33
|
+
a.user_id.should == @user1.id
|
34
|
+
User.current_user = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "does not add the user to the audit if User.current_user is not set" do
|
38
|
+
school = School.create(:name => 'PS118')
|
39
|
+
a = school.audits.first
|
40
|
+
a.user_id.should == nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ":report_on => Member; :report_method => 'current_member'" do
|
45
|
+
before do
|
46
|
+
class School < ActiveRecord::Base
|
47
|
+
audit_enabled
|
48
|
+
end
|
49
|
+
|
50
|
+
class Member < ActiveRecord::Base
|
51
|
+
def self.current_member=(member)
|
52
|
+
@current_member = member
|
53
|
+
end
|
54
|
+
def self.current_member
|
55
|
+
@current_member
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
Auditing.configure do |config|
|
60
|
+
config.report_on = Member
|
61
|
+
config.report_method = 'current_member'
|
62
|
+
end
|
63
|
+
|
64
|
+
@member1 = Member.create(:member_name=>'foo_dude')
|
65
|
+
@member2 = Member.create(:member_name=>'bar_dude')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "adds the member to the audit" do
|
69
|
+
Member.current_member = @member1
|
70
|
+
school = School.create(:name => 'PS118')
|
71
|
+
a = school.audits.first
|
72
|
+
a.user_id.should == @member1.id
|
73
|
+
Member.current_member = nil
|
74
|
+
end
|
75
|
+
|
76
|
+
it "does not add the member to the audit if Member.current_member is not set" do
|
77
|
+
school = School.create(:name => 'PS118')
|
78
|
+
a = school.audits.first
|
79
|
+
a.user_id.should == nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/schema.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -4,6 +4,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
4
4
|
require 'active_record'
|
5
5
|
require 'auditing'
|
6
6
|
require 'rspec'
|
7
|
+
require 'timecop'
|
7
8
|
|
8
9
|
# our test database
|
9
10
|
TEST_DB = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
|
@@ -16,11 +17,13 @@ ActiveRecord::Base.establish_connection(
|
|
16
17
|
# our schema
|
17
18
|
load(File.dirname(__FILE__) + '/schema.rb')
|
18
19
|
|
20
|
+
Auditing.configure do |config|
|
21
|
+
config.report_on = nil
|
22
|
+
config.report_method = nil
|
23
|
+
end
|
24
|
+
|
19
25
|
class Audit < ActiveRecord::Base
|
20
26
|
include Auditing::Auditor
|
21
27
|
belongs_to :auditable, :polymorphic => true
|
22
28
|
belongs_to :association, :polymorphic => true
|
23
29
|
end
|
24
|
-
|
25
|
-
# class User < ActiveRecord::Base
|
26
|
-
# end
|
data/todo.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# TODO
|
2
|
+
config (aka => tigre-clint)
|
3
|
+
serialization (default ?)
|
4
|
+
allow dev to create their own, location should be lib/auditing/serialization
|
5
|
+
publish API (load / dump)
|
6
|
+
|
7
|
+
serialization schemes
|
8
|
+
YAML
|
9
|
+
BASE64
|
10
|
+
Marshal
|
11
|
+
other?
|
12
|
+
|
13
|
+
|
14
|
+
documentation
|
15
|
+
investigate YARD
|
16
|
+
update auditing_project
|
17
|
+
create branch that shows
|
18
|
+
defaults
|
19
|
+
custom user => MEMBER
|
20
|
+
custom serialization => maybe put BASE64 here instead ?
|
21
|
+
|
22
|
+
|
23
|
+
test all column types with all supported serialization schemes
|
24
|
+
use BASE64 to document how to add / test your own
|
25
|
+
|
26
|
+
possible screencast about installation / usage ?
|
27
|
+
possible opensourceitwith.me/gems/auditing
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: auditing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brad Cantin
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-22 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- .gitignore
|
95
95
|
- .rspec
|
96
96
|
- .rvmrc
|
97
|
+
- .rvmrc.rbx
|
97
98
|
- Gemfile
|
98
99
|
- Gemfile.lock
|
99
100
|
- LICENSE
|
@@ -108,6 +109,7 @@ files:
|
|
108
109
|
- lib/generators/auditing/USAGE
|
109
110
|
- lib/generators/auditing/install_generator.rb
|
110
111
|
- lib/generators/auditing/templates/audit.rb
|
112
|
+
- lib/generators/auditing/templates/auditing.rb
|
111
113
|
- lib/generators/auditing/templates/audits_controller.rb
|
112
114
|
- lib/generators/auditing/templates/audits_helper.rb
|
113
115
|
- lib/generators/auditing/templates/index.html.erb
|
@@ -118,10 +120,11 @@ files:
|
|
118
120
|
- spec/auditing/audit_relationship_spec.rb
|
119
121
|
- spec/auditing/auditor_spec.rb
|
120
122
|
- spec/auditing/base_spec.rb
|
123
|
+
- spec/auditing/config_report_spec.rb
|
121
124
|
- spec/auditing/formats_spec.rb
|
122
|
-
- spec/auditing/with_users_spec.rb
|
123
125
|
- spec/schema.rb
|
124
126
|
- spec/spec_helper.rb
|
127
|
+
- todo.txt
|
125
128
|
has_rdoc: true
|
126
129
|
homepage: https://github.com/bcantin/auditing
|
127
130
|
licenses: []
|
@@ -154,7 +157,7 @@ test_files:
|
|
154
157
|
- spec/auditing/audit_relationship_spec.rb
|
155
158
|
- spec/auditing/auditor_spec.rb
|
156
159
|
- spec/auditing/base_spec.rb
|
160
|
+
- spec/auditing/config_report_spec.rb
|
157
161
|
- spec/auditing/formats_spec.rb
|
158
|
-
- spec/auditing/with_users_spec.rb
|
159
162
|
- spec/schema.rb
|
160
163
|
- spec/spec_helper.rb
|
@@ -1,37 +0,0 @@
|
|
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
|