@@ -19,6 +19,10 @@ module Auditor #:nodoc:
1919 CALLBACKS = [ :audit_create , :audit_update , :audit_destroy ]
2020
2121 module ClassMethods
22+ def audit_class
23+ audit_class_name &.safe_constantize || Audited . audit_class
24+ end
25+
2226 # == Configuration options
2327 #
2428 #
@@ -67,20 +71,23 @@ def audited(options = {})
6771
6872 class_attribute :audit_associated_with , instance_writer : false
6973 class_attribute :audited_options , instance_writer : false
74+ class_attribute :audit_class_name , instance_writer : false
75+
7076 attr_accessor :audit_version , :audit_comment
7177
7278 self . audited_options = options
7379 normalize_audited_options
7480
7581 self . audit_associated_with = audited_options [ :associated_with ]
82+ self . audit_class_name = audited_options [ :audit_class_name ]
7683
7784 if audited_options [ :comment_required ]
7885 validate :presence_of_audit_comment
7986 before_destroy :require_comment if audited_options [ :on ] . include? ( :destroy )
8087 end
8188
82- has_many :audits , -> { order ( version : :asc ) } , as : :auditable , class_name : Audited . audit_class . name , inverse_of : :auditable
83- Audited . audit_class . audited_class_names << to_s
89+ has_many :audits , -> { order ( version : :asc ) } , as : :auditable , class_name : audit_class . name , inverse_of : :auditable
90+ audit_class . audited_class_names << to_s
8491
8592 after_create :audit_create if audited_options [ :on ] . include? ( :create )
8693 before_update :audit_update if audited_options [ :on ] . include? ( :update )
@@ -96,14 +103,18 @@ def audited(options = {})
96103 enable_auditing
97104 end
98105
99- def has_associated_audits
100- has_many :associated_audits , as : :associated , class_name : Audited . audit_class . name
106+ def has_associated_audits ( audit_class_name : Audited . audit_class . name )
107+ has_many :associated_audits , as : :associated , class_name : audit_class_name
101108 end
102109 end
103110
104111 module AuditedInstanceMethods
105112 REDACTED = "[REDACTED]"
106113
114+ def audit_class
115+ self . class . audit_class
116+ end
117+
107118 # Temporarily turns off auditing while saving.
108119 def save_without_auditing
109120 without_auditing { save }
@@ -159,14 +170,14 @@ def revisions(from_version = 1)
159170 # Returns nil for versions greater than revisions count
160171 def revision ( version )
161172 if version == :previous || audits . last . version >= version
162- revision_with Audited . audit_class . reconstruct_attributes ( audits_to ( version ) )
173+ revision_with audit_class . reconstruct_attributes ( audits_to ( version ) )
163174 end
164175 end
165176
166177 # Find the oldest revision recorded prior to the date/time provided.
167178 def revision_at ( date_or_time )
168179 audits = self . audits . up_until ( date_or_time )
169- revision_with Audited . audit_class . reconstruct_attributes ( audits ) unless audits . empty?
180+ revision_with audit_class . reconstruct_attributes ( audits ) unless audits . empty?
170181 end
171182
172183 # List of attributes that are audited.
@@ -177,7 +188,7 @@ def audited_attributes
177188
178189 # Returns a list combined of record audits and associated audits.
179190 def own_and_associated_audits
180- Audited . audit_class . unscoped
191+ audit_class . unscoped
181192 . where ( "(auditable_type = :type AND auditable_id = :id) OR (associated_type = :type AND associated_id = :id)" ,
182193 type : self . class . base_class . name , id : id )
183194 . order ( created_at : :desc )
@@ -206,7 +217,7 @@ def revision_with(attributes)
206217 revision . send :instance_variable_set , "@destroyed" , false
207218 revision . send :instance_variable_set , "@_destroyed" , false
208219 revision . send :instance_variable_set , "@marked_for_destruction" , false
209- Audited . audit_class . assign_revision_attributes ( revision , attributes )
220+ audit_class . assign_revision_attributes ( revision , attributes )
210221
211222 # Remove any association proxies so that they will be recreated
212223 # and reference the correct object for this revision. The only way
@@ -431,7 +442,7 @@ def enable_auditing
431442 # convenience wrapper around
432443 # @see Audit#as_user.
433444 def audit_as ( user , &block )
434- Audited . audit_class . as_user ( user , &block )
445+ audit_class . as_user ( user , &block )
435446 end
436447
437448 def auditing_enabled
0 commit comments