@@ -19,6 +19,10 @@ module Auditor #:nodoc:
19
19
CALLBACKS = [ :audit_create , :audit_update , :audit_destroy ]
20
20
21
21
module ClassMethods
22
+ def audit_class
23
+ audit_class_name &.safe_constantize || Audited . audit_class
24
+ end
25
+
22
26
# == Configuration options
23
27
#
24
28
#
@@ -67,20 +71,23 @@ def audited(options = {})
67
71
68
72
class_attribute :audit_associated_with , instance_writer : false
69
73
class_attribute :audited_options , instance_writer : false
74
+ class_attribute :audit_class_name , instance_writer : false
75
+
70
76
attr_accessor :audit_version , :audit_comment
71
77
72
78
self . audited_options = options
73
79
normalize_audited_options
74
80
75
81
self . audit_associated_with = audited_options [ :associated_with ]
82
+ self . audit_class_name = audited_options [ :audit_class_name ]
76
83
77
84
if audited_options [ :comment_required ]
78
85
validate :presence_of_audit_comment
79
86
before_destroy :require_comment if audited_options [ :on ] . include? ( :destroy )
80
87
end
81
88
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
84
91
85
92
after_create :audit_create if audited_options [ :on ] . include? ( :create )
86
93
before_update :audit_update if audited_options [ :on ] . include? ( :update )
@@ -96,8 +103,8 @@ def audited(options = {})
96
103
enable_auditing
97
104
end
98
105
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
101
108
end
102
109
end
103
110
@@ -159,14 +166,14 @@ def revisions(from_version = 1)
159
166
# Returns nil for versions greater than revisions count
160
167
def revision ( version )
161
168
if version == :previous || audits . last . version >= version
162
- revision_with Audited . audit_class . reconstruct_attributes ( audits_to ( version ) )
169
+ revision_with audit_class . reconstruct_attributes ( audits_to ( version ) )
163
170
end
164
171
end
165
172
166
173
# Find the oldest revision recorded prior to the date/time provided.
167
174
def revision_at ( date_or_time )
168
175
audits = self . audits . up_until ( date_or_time )
169
- revision_with Audited . audit_class . reconstruct_attributes ( audits ) unless audits . empty?
176
+ revision_with audit_class . reconstruct_attributes ( audits ) unless audits . empty?
170
177
end
171
178
172
179
# List of attributes that are audited.
@@ -177,7 +184,7 @@ def audited_attributes
177
184
178
185
# Returns a list combined of record audits and associated audits.
179
186
def own_and_associated_audits
180
- Audited . audit_class . unscoped
187
+ audit_class . unscoped
181
188
. where ( "(auditable_type = :type AND auditable_id = :id) OR (associated_type = :type AND associated_id = :id)" ,
182
189
type : self . class . base_class . name , id : id )
183
190
. order ( created_at : :desc )
@@ -206,7 +213,7 @@ def revision_with(attributes)
206
213
revision . send :instance_variable_set , "@destroyed" , false
207
214
revision . send :instance_variable_set , "@_destroyed" , false
208
215
revision . send :instance_variable_set , "@marked_for_destruction" , false
209
- Audited . audit_class . assign_revision_attributes ( revision , attributes )
216
+ audit_class . assign_revision_attributes ( revision , attributes )
210
217
211
218
# Remove any association proxies so that they will be recreated
212
219
# and reference the correct object for this revision. The only way
@@ -431,7 +438,7 @@ def enable_auditing
431
438
# convenience wrapper around
432
439
# @see Audit#as_user.
433
440
def audit_as ( user , &block )
434
- Audited . audit_class . as_user ( user , &block )
441
+ audit_class . as_user ( user , &block )
435
442
end
436
443
437
444
def auditing_enabled
0 commit comments