track_changes 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -0
- data/VERSION.yml +2 -2
- data/lib/track_changes/audit_filter.rb +10 -2
- data/lib/track_changes/class_methods.rb +8 -2
- data/lib/track_changes/configuration.rb +38 -0
- data/lib/track_changes/initializer.rb +18 -0
- data/lib/track_changes.rb +2 -0
- data/test/rails_root/app/controllers/posts_controller.rb +1 -1
- data/test/rails_root/config/initializers/track_changes_setup.rb +8 -0
- data/test/rails_root/log/test.log +2192 -0
- data/test/track_changes/audit_filter_test.rb +96 -0
- data/test/track_changes/configuration_test.rb +36 -0
- data/test/track_changes/initializer_test.rb +37 -0
- metadata +10 -2
data/README.rdoc
CHANGED
@@ -34,6 +34,18 @@ Example Audited class:
|
|
34
34
|
has_many :audits, :as => :audited
|
35
35
|
end
|
36
36
|
|
37
|
+
You can also tweak some of the defaults by creating an initializer
|
38
|
+
in <tt>RAILS_ROOT/config/initializers/track_changes_configuration.rb</tt>
|
39
|
+
and calling TrackChanges::Initializer.instance which yields a
|
40
|
+
TrackChanges::Configuration object:
|
41
|
+
|
42
|
+
# These are the defaults anyways
|
43
|
+
TrackChanges::Initializer.instance do |c|
|
44
|
+
c.audit_assocation = :audits # Calls <tt>create!</tt> on this association method
|
45
|
+
c.current_user = :current_user # Controller is sent this method to obtain current user.
|
46
|
+
c.ignore_nil = true # Don't crash if a model is nil.
|
47
|
+
end
|
48
|
+
|
37
49
|
== Controller Example
|
38
50
|
|
39
51
|
In this example, after the `update` action is called,
|
data/VERSION.yml
CHANGED
@@ -25,13 +25,21 @@ module TrackChanges
|
|
25
25
|
|
26
26
|
self.cached_models = {}
|
27
27
|
self.models = models
|
28
|
+
|
29
|
+
@audit_accessor = options[:audit_accessor] || :audits
|
30
|
+
@current_user = options[:current_user] || :current_user
|
31
|
+
@ignore_nils = if options[:ignore_nil] == false
|
32
|
+
false
|
33
|
+
else
|
34
|
+
true
|
35
|
+
end
|
28
36
|
end
|
29
37
|
|
30
38
|
def before(controller)
|
31
39
|
self.models.each do |model|
|
32
40
|
instance_variable_symbol = "@#{model}".to_sym
|
33
41
|
instance = controller.instance_variable_get(instance_variable_symbol)
|
34
|
-
next if instance.nil?
|
42
|
+
next if instance.nil? && @ignore_nils
|
35
43
|
|
36
44
|
self.cached_models[instance_variable_symbol] = instance.clone
|
37
45
|
end
|
@@ -45,7 +53,7 @@ module TrackChanges
|
|
45
53
|
changes = changes_between(new_instance, original_instance)
|
46
54
|
|
47
55
|
unless changes.empty?
|
48
|
-
audit = new_instance.
|
56
|
+
audit = new_instance.send(@audit_accessor).create!(:user => controller.send(@current_user), :change_set => changes)
|
49
57
|
end
|
50
58
|
end
|
51
59
|
end
|
@@ -34,10 +34,16 @@ module TrackChanges
|
|
34
34
|
# # Create an audit of changes made to @order during the <tt>:update</tt> or <tt>:process</tt> actions.
|
35
35
|
# track_changes :order, :only => [:update, :process]
|
36
36
|
def track_changes(*args)
|
37
|
-
models = args.flatten
|
38
37
|
options = args.extract_options!
|
38
|
+
models = args.flatten
|
39
39
|
options = [:only => :update] if options.empty?
|
40
|
-
|
40
|
+
|
41
|
+
configuration = TrackChanges::Initializer.instance.configuration
|
42
|
+
|
43
|
+
audit_filter = AuditFilter.new(models,
|
44
|
+
:audit_accessor => configuration.audit_association,
|
45
|
+
:current_user => configuration.current_user,
|
46
|
+
:ignore_nil => configuration.ignore_nil)
|
41
47
|
around_filter(audit_filter, options)
|
42
48
|
end
|
43
49
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module TrackChanges
|
2
|
+
class Configuration
|
3
|
+
# The association used to create an audit. Defaults to
|
4
|
+
# <tt>:audits</tt>
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# :audits # => Call model.audits.create
|
8
|
+
# :changes # => Call model.changes.create
|
9
|
+
attr_accessor :audit_association
|
10
|
+
|
11
|
+
# The controller method called to obtain the current user.
|
12
|
+
# Defaults to <tt>:current_user</tt>
|
13
|
+
attr_accessor :current_user
|
14
|
+
|
15
|
+
# Ignore if the audited item is nil. Defaults to <tt>true</tt>.
|
16
|
+
attr_accessor :ignore_nil
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
self.audit_association = default_audit_association
|
20
|
+
self.current_user = default_current_user
|
21
|
+
self.ignore_nil = default_ignore_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def default_audit_association
|
27
|
+
:audits
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_current_user
|
31
|
+
:current_user
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_ignore_nil
|
35
|
+
true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TrackChanges
|
2
|
+
class Initializer
|
3
|
+
attr_accessor :configuration
|
4
|
+
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
class << self
|
8
|
+
alias_method :__tr_instance, :instance
|
9
|
+
|
10
|
+
def instance(&block)
|
11
|
+
obj = __tr_instance
|
12
|
+
obj.configuration ||= Configuration.new
|
13
|
+
yield obj.configuration if block_given?
|
14
|
+
obj
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/track_changes.rb
CHANGED