track_changes 0.4.1 → 0.5.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/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
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 4
4
- :patch: 1
3
+ :minor: 5
4
+ :patch: 0
5
5
  :build:
@@ -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.audits.create!(:user => controller.send(:current_user), :change_set => changes)
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
- audit_filter = AuditFilter.new(models)
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
@@ -1,4 +1,6 @@
1
1
  require 'track_changes/result'
2
+ require 'track_changes/configuration'
3
+ require 'track_changes/initializer'
2
4
  require 'track_changes/audit_filter'
3
5
  require 'track_changes/filter'
4
6
  require 'track_changes/instance_methods'
@@ -3,7 +3,7 @@ require 'track_changes'
3
3
  class PostsController < ApplicationController
4
4
  include TrackChanges
5
5
  before_filter :find_post
6
- track_changes :post
6
+ track_changes :post, :only => :update
7
7
 
8
8
  def show
9
9
  render :text => @post.inspect
@@ -0,0 +1,8 @@
1
+ require 'track_changes'
2
+
3
+ TrackChanges::Initializer.instance do |config|
4
+ config.audit_association = :audits
5
+ config.current_user = :current_user
6
+ config.ignore_nil = true
7
+ end
8
+