simple_audit_trail 1.0.0 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +12 -13
- data/lib/simple_audit_trail/version.rb +1 -1
- data/lib/simple_audit_trail.rb +24 -7
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +24 -29
- data/spec/dummy/log/test.log +2709 -898
- data/spec/lib/simple_audit_trail_auditor_spec.rb +5 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6676d538e06da759f96c5312bfddbe870847225
|
4
|
+
data.tar.gz: d1a314e1d44cba111a208a19c06875e60df109c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 591e787a48ca58f5f5772f2fb05c629bf0f369d7de229a0aad3ed5f68f267d8d63a345b7d533398904acaf1d31dcf6cc3a56771372b9c55782c626e9cb96ed06
|
7
|
+
data.tar.gz: 04a2b9116578b4a609bed9c2925c3f2bcdabbd5738b8f015a51ed8f1314d26631f34cd33d704d7d02a7dcd01da32795dfe17949168202372b86c5e8fcbf0cab4
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Simple Audit Trail
|
2
|
-
|
2
|
+
# ** rails_3 branch and 0.0.x gem versions is Rails 3x, for the rails 4 compatible gem, use the 1.x.x gem versions and the master branch**
|
3
3
|
## Synopsis
|
4
4
|
|
5
|
-
Use to create an audit trail of field changes in a model, storing what was
|
6
|
-
changed and who changed it.
|
5
|
+
Use to create an audit trail of field changes in a model, storing what was
|
6
|
+
changed and who changed it.
|
7
7
|
|
8
8
|
**Setup**
|
9
9
|
|
@@ -13,12 +13,12 @@ changed and who changed it.
|
|
13
13
|
1. Migrate: ``` rake db:migrate ```
|
14
14
|
|
15
15
|
|
16
|
-
1. You must have a current_user method in your app. If not, you'll need to override
|
17
|
-
``` simple_audit_trail_who_id ``` to provide a user id
|
16
|
+
1. You must have a current_user method in your app. If not, you'll need to override
|
17
|
+
``` simple_audit_trail_who_id ``` to provide a user id
|
18
18
|
|
19
19
|
**Model**
|
20
20
|
|
21
|
-
```
|
21
|
+
```
|
22
22
|
class Thing < ActiveRecord::Base
|
23
23
|
audit [:some_field, :some_other_field]
|
24
24
|
end
|
@@ -27,13 +27,13 @@ end
|
|
27
27
|
|
28
28
|
**Usage**
|
29
29
|
|
30
|
-
Thing instances now have an attribute, ` audited_user_id `.
|
30
|
+
Thing instances now have an attribute, ` audited_user_id `.
|
31
31
|
|
32
|
-
You must set this on the object before you save it with changes to the audited
|
32
|
+
You must set this on the object before you save it with changes to the audited
|
33
33
|
fields, or the audit attempt will fail, and raise an exception.
|
34
|
-
|
35
|
-
Assuming you have the above model, and that your controller has access to the
|
36
|
-
usual `current_user` method, you could do something like:
|
34
|
+
|
35
|
+
Assuming you have the above model, and that your controller has access to the
|
36
|
+
usual `current_user` method, you could do something like:
|
37
37
|
|
38
38
|
```ruby
|
39
39
|
t = Thing.find(1)
|
@@ -46,6 +46,5 @@ usual `current_user` method, you could do something like:
|
|
46
46
|
|
47
47
|
which would generate a record in ` t.simple_audits `
|
48
48
|
|
49
|
-
* If you don't wish to track users, but just track changes,
|
49
|
+
* If you don't wish to track users, but just track changes,
|
50
50
|
add ` :require_audited_user_id => false ` to your `audit` call.
|
51
|
-
|
data/lib/simple_audit_trail.rb
CHANGED
@@ -12,29 +12,46 @@ module SimpleAuditTrail
|
|
12
12
|
cattr_accessor :audit_options
|
13
13
|
self.audit_options = { :require_audited_user_id => true }.merge(options)
|
14
14
|
|
15
|
-
|
16
15
|
attr_accessor :audited_user_id
|
16
|
+
attr_accessible :audited_user_id
|
17
17
|
|
18
18
|
has_many :simple_audits,
|
19
19
|
:as => :simple_audit_trailable,
|
20
20
|
:class_name => "SimpleAuditTrail::Audit",
|
21
21
|
:autosave => true
|
22
22
|
|
23
|
+
after_create :save_all_audits
|
24
|
+
define_method :save_all_audits do
|
25
|
+
if audited_user_id.nil? && audit_options[:require_audited_user_id]
|
26
|
+
raise "audited setter method called without setting audited_user_id"
|
27
|
+
end
|
28
|
+
|
29
|
+
from = {}
|
30
|
+
to = Hash[audited_fields.map { |k| [k, send(k)] } ]
|
31
|
+
unchanged = {}
|
32
|
+
|
33
|
+
simple_audits.create(
|
34
|
+
:from => from.to_json,
|
35
|
+
:to => to.to_json,
|
36
|
+
:unchanged => unchanged.to_json,
|
37
|
+
:who_id => audited_user_id
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
23
41
|
before_update :save_audits
|
24
42
|
define_method :save_audits do
|
25
|
-
changed_audited_fields = audited_fields
|
26
|
-
send "#{f}_changed?"
|
27
|
-
end
|
43
|
+
changed_audited_fields = changes.slice(*audited_fields)
|
28
44
|
|
29
45
|
if changed_audited_fields.present?
|
30
46
|
if audited_user_id.nil? && audit_options[:require_audited_user_id]
|
31
47
|
raise "audited setter method called without setting audited_user_id"
|
32
48
|
end
|
33
49
|
|
34
|
-
|
35
|
-
from = Hash[changed_audited_fields.map { |
|
50
|
+
|
51
|
+
from = Hash[changed_audited_fields.map { |k, v| [k, v[0]] } ]
|
52
|
+
to = Hash[changed_audited_fields.map { |k, v| [k, v[1]] } ]
|
36
53
|
unchanged = Hash[
|
37
|
-
(audited_fields - changed_audited_fields).map do |f|
|
54
|
+
(audited_fields - changed_audited_fields.keys).map do |f|
|
38
55
|
[f, send(f)]
|
39
56
|
end
|
40
57
|
]
|
Binary file
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -1,31 +1,26 @@
|
|
1
|
-
|
2
|
-
[1m[
|
3
|
-
[1m[
|
4
|
-
[1m[
|
5
|
-
[1m[
|
6
|
-
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
1
|
+
Connecting to database specified by database.yml
|
2
|
+
[1m[36m (0.2ms)[0m [1mselect sqlite_version(*)[0m
|
3
|
+
[1m[35m (0.7ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
4
|
+
[1m[36m (0.5ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
5
|
+
[1m[35m (0.1ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
|
7
6
|
Migrating to CreateSimpleAuditTrailAudits (20150512224022)
|
8
|
-
[1m[
|
9
|
-
|
10
|
-
[1m[36m (0.
|
11
|
-
[1m[
|
12
|
-
|
7
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
8
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "simple_audit_trail_audits" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "simple_audit_trailable_id" integer, "simple_audit_trailable_type" varchar(255), "who_id" integer, "from" text, "to" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
9
|
+
[1m[36m (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES ('20150512224022')[0m
|
10
|
+
[1m[35m (0.4ms)[0m commit transaction
|
11
|
+
Migrating to CreateTinas (20150513181325)
|
12
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
13
|
+
[1m[35m (0.2ms)[0m CREATE TABLE "tinas" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "ladies" integer, "badonkadonks" integer, "mushy_snugglebites" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
14
|
+
[1m[36m (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES ('20150513181325')[0m
|
15
|
+
[1m[35m (0.4ms)[0m commit transaction
|
16
|
+
Migrating to CreateMrTorques (20150513221356)
|
17
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
18
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "mr_torques" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todays_quote" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
19
|
+
[1m[36m (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES ('20150513221356')[0m
|
20
|
+
[1m[35m (0.4ms)[0m commit transaction
|
13
21
|
Migrating to AddUnchangedColumn (20150701174723)
|
14
|
-
[1m[
|
15
|
-
[1m[
|
16
|
-
[1m[
|
17
|
-
[1m[
|
18
|
-
[1m[
|
19
|
-
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
20
|
-
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
21
|
-
[1m[36m (1.7ms)[0m [1mCREATE TABLE "mr_torques" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todays_quote" varchar, "created_at" datetime, "updated_at" datetime) [0m
|
22
|
-
[1m[35m (0.8ms)[0m CREATE TABLE "simple_audit_trail_audits" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "simple_audit_trailable_id" integer, "simple_audit_trailable_type" varchar, "who_id" integer, "from" text, "to" text, "created_at" datetime, "updated_at" datetime, "unchanged" text)
|
23
|
-
[1m[36m (0.7ms)[0m [1mCREATE TABLE "tinas" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "ladies" integer, "badonkadonks" integer, "mushy_snugglebites" varchar, "created_at" datetime, "updated_at" datetime) [0m
|
24
|
-
[1m[35m (0.9ms)[0m CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
|
25
|
-
[1m[36m (0.0ms)[0m [1mselect sqlite_version(*)[0m
|
26
|
-
[1m[35m (0.9ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
27
|
-
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
28
|
-
[1m[35m (0.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20150701174723')
|
29
|
-
[1m[36m (1.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20150513181325')[0m
|
30
|
-
[1m[35m (0.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20150513221356')
|
31
|
-
[1m[36m (0.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20150512224022')[0m
|
22
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
23
|
+
[1m[35m (0.2ms)[0m ALTER TABLE "simple_audit_trail_audits" ADD "unchanged" text
|
24
|
+
[1m[36m (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES ('20150701174723')[0m
|
25
|
+
[1m[35m (0.3ms)[0m commit transaction
|
26
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|