audit_log 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,9 @@
1
1
  require "audit_log/version"
2
+ require "audit_log/current_thread"
3
+ require "audit_log/controller"
2
4
  require "audit_log/mapping"
3
5
  require "audit_log/observer"
6
+ require "audit_log/logged_model"
4
7
 
5
8
  module AuditLog
6
9
 
@@ -0,0 +1,19 @@
1
+ module AuditLog
2
+
3
+ module Controller
4
+
5
+ def self.included(controller)
6
+ controller.before_filter :user_for_audit_log
7
+ end
8
+
9
+ def user_for_audit_log
10
+ AuditLog::CurrentThread.who = current_user_for_audit_log
11
+ end
12
+
13
+ def current_user_for_audit_log
14
+ current_user
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,23 @@
1
+ module AuditLog
2
+
3
+ class CurrentThread
4
+
5
+ class << self
6
+ def who=(user)
7
+ auditing_data[:who] = user
8
+ end
9
+
10
+ def who
11
+ auditing_data[:who]
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def self.auditing_data
18
+ Thread.current[:auditing_log] ||= {}
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,8 @@
1
+ class LoggedModel < ActiveRecord::Base
2
+ attr_accessible :who, :what, :model_id, :model_name
3
+
4
+ def when
5
+ created_at
6
+ end
7
+
8
+ end
@@ -31,4 +31,8 @@ module AuditLog
31
31
  end
32
32
  end
33
33
 
34
+ end
35
+
36
+ ActiveSupport.on_load(:action_controller) do
37
+ include AuditLog::Controller
34
38
  end
@@ -1,6 +1,3 @@
1
- #require 'active_support/inflector'
2
- #require 'active_record'
3
-
4
1
  class AuditedModelsObserver < ActiveRecord::Observer
5
2
 
6
3
  def self.observed_classes
@@ -14,6 +11,7 @@ class AuditedModelsObserver < ActiveRecord::Observer
14
11
  def after_create(model)
15
12
  if Thread.current[:auditing] == model
16
13
  logged_model = LoggedModel.new(
14
+ who: AuditLog::CurrentThread.who,
17
15
  what: {id: model.id, event: :create},
18
16
  model_name: model.class.name,
19
17
  model_id: model.id
@@ -23,9 +21,12 @@ class AuditedModelsObserver < ActiveRecord::Observer
23
21
  end
24
22
 
25
23
 
26
- def after_destroy(model)
24
+ def before_destroy(model)
25
+ Thread.current[:auditing] ||= model
26
+
27
27
  if Thread.current[:auditing] == model
28
28
  logged_model = LoggedModel.new(
29
+ who: AuditLog::CurrentThread.who,
29
30
  what: {id: model.id, event: :destroy},
30
31
  model_name: model.class.name,
31
32
  model_id: model.id
@@ -42,6 +43,7 @@ class AuditedModelsObserver < ActiveRecord::Observer
42
43
  what = WhatBuilder.new(changes).build
43
44
 
44
45
  logged_model = LoggedModel.new(
46
+ who: AuditLog::CurrentThread.who,
45
47
  what: what,
46
48
  model_name: model.class.name,
47
49
  model_id: model.id
@@ -51,19 +53,6 @@ class AuditedModelsObserver < ActiveRecord::Observer
51
53
  end
52
54
  end
53
55
 
54
- # {
55
- # model: model,
56
- # fields_updates: {field_name: {from: "", to: ""}},
57
- # has_many: {
58
- # association_name: [
59
- # {
60
- # model: model,
61
- # event: :event_name,
62
- # fields_updates: {field_name: {from: "", to: ""}}
63
- # }
64
- # ]
65
- # }
66
- # }
67
56
  def before_update(model)
68
57
  if Thread.current[:auditing] == model
69
58
  if (model.changed? && !(model.changed_attributes.keys.collect{|attr| attr.to_sym}.uniq.sort - ignored_fields(model).uniq.sort).empty?) ||
@@ -1,3 +1,3 @@
1
1
  module AuditLog
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ module AuditLog
6
+
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+ extend ActiveRecord::Generators::Migration
10
+
11
+ source_root File.expand_path('../templates', __FILE__)
12
+
13
+ desc 'Generates (but does not run) a migration to add a versions table.'
14
+
15
+ def create_migration_file
16
+ migration_template 'create_logged_models.rb', 'db/migrate/create_logged_models.rb'
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,14 @@
1
+ class CreateLoggedModels < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :logged_models do |t|
5
+ t.integer :who
6
+ t.text :what
7
+ t.integer :model_id
8
+ t.string :model_name
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: audit_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
12
+ date: 2012-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -67,8 +67,13 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - lib/audit_log/version.rb
70
+ - lib/audit_log/controller.rb
70
71
  - lib/audit_log/observer.rb
72
+ - lib/audit_log/logged_model.rb
71
73
  - lib/audit_log/mapping.rb
74
+ - lib/audit_log/current_thread.rb
75
+ - lib/generators/audit_log/templates/create_logged_models.rb
76
+ - lib/generators/audit_log/install_generator.rb
72
77
  - lib/audit_log.rb
73
78
  - lib/tasks/audit_log_tasks.rake
74
79
  - MIT-LICENSE
@@ -97,5 +102,5 @@ rubyforge_project:
97
102
  rubygems_version: 1.8.24
98
103
  signing_key:
99
104
  specification_version: 3
100
- summary: Logs of model changes, including nested attributesg.
105
+ summary: Logs of model changes, including nested attributes.
101
106
  test_files: []