simple_audit_trail 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05392058a30f45516932956a54df4b3531bcf941
4
- data.tar.gz: df84d49f9092f4e4a296ebf706dd29a559b2d577
3
+ metadata.gz: d6676d538e06da759f96c5312bfddbe870847225
4
+ data.tar.gz: d1a314e1d44cba111a208a19c06875e60df109c3
5
5
  SHA512:
6
- metadata.gz: 001e958b666d15e6923e9721118fd3436b8917047578c5d3a45bc622c28cfe547065fca68d7a2172092dd3073e3d9dda65e71d0508c3cb4fae303a7c029cb220
7
- data.tar.gz: c242e91ecd549752fd03c9502414d17b0026577295b4d4d27e28c09b650ae0e4184bf91fb16f12a90a6d5db68af7ec7ddbb2bc0905525ece242180a670dc0c6a
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
-
@@ -1,3 +1,3 @@
1
1
  module SimpleAuditTrail
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -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.select do |f|
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
- to = Hash[changed_audited_fields.map { |f| [f, send(f)] }]
35
- from = Hash[changed_audited_fields.map { |f| [f, send("#{f}_was")] }]
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
Binary file
@@ -1,31 +1,26 @@
1
-  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
2
-  (0.1ms) select sqlite_version(*)
3
-  (0.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
4
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
5
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
6
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
1
+ Connecting to database specified by database.yml
2
+  (0.2ms) select sqlite_version(*)
3
+  (0.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
4
+  (0.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
5
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
7
6
  Migrating to CreateSimpleAuditTrailAudits (20150512224022)
8
-  (0.1ms) begin transaction
9
- DEPRECATION WARNING: `#timestamps` was called without specifying an option for `null`. In Rails 5, this behavior will change to `null: false`. You should manually specify `null: true` to prevent the behavior of your existing migrations from changing. (called from block in change at /Users/christophermaujean/projects/simple_audit_trail/db/migrate/20150512224022_create_simple_audit_trail_audits.rb:10)
10
-  (0.4ms) 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) 
11
- SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150512224022"]]
12
-  (1.0ms) commit transaction
7
+  (0.0ms) begin transaction
8
+  (0.3ms) 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
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20150512224022')
10
+  (0.4ms) commit transaction
11
+ Migrating to CreateTinas (20150513181325)
12
+  (0.0ms) begin transaction
13
+  (0.2ms) 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
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20150513181325')
15
+  (0.4ms) commit transaction
16
+ Migrating to CreateMrTorques (20150513221356)
17
+  (0.0ms) begin transaction
18
+  (0.3ms) 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
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20150513221356')
20
+  (0.4ms) commit transaction
13
21
  Migrating to AddUnchangedColumn (20150701174723)
14
-  (0.0ms) begin transaction
15
-  (0.3ms) ALTER TABLE "simple_audit_trail_audits" ADD "unchanged" text
16
- SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150701174723"]]
17
-  (0.5ms) commit transaction
18
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
19
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
20
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
21
-  (1.7ms) CREATE TABLE "mr_torques" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todays_quote" varchar, "created_at" datetime, "updated_at" datetime) 
22
-  (0.8ms) 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
-  (0.7ms) CREATE TABLE "tinas" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "ladies" integer, "badonkadonks" integer, "mushy_snugglebites" varchar, "created_at" datetime, "updated_at" datetime) 
24
-  (0.9ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
25
-  (0.0ms) select sqlite_version(*)
26
-  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
27
-  (0.1ms) SELECT version FROM "schema_migrations"
28
-  (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20150701174723')
29
-  (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20150513181325')
30
-  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20150513221356')
31
-  (0.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20150512224022')
22
+  (0.0ms) begin transaction
23
+  (0.2ms) ALTER TABLE "simple_audit_trail_audits" ADD "unchanged" text
24
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20150701174723')
25
+  (0.3ms) commit transaction
26
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"