Skip to content

Commit

Permalink
Don't exclude classes that don't inherit from AR in 7.1 at class load…
Browse files Browse the repository at this point in the history
… time

from PR feedback; instead of doing the version check at runtime, do it
at class load time, i.e. when the constant is defined
  • Loading branch information
argvniyx-enroute committed Apr 24, 2024
1 parent 50c4e72 commit fb6b937
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/journaled/audit_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ module Journaled
module AuditLog
extend ActiveSupport::Concern

DEFAULT_EXCLUDED_CLASSES = %w(
Delayed::Job
PaperTrail::Version
ActiveStorage::Attachment
ActiveStorage::Blob
ActiveRecord::InternalMetadata
ActiveRecord::SchemaMigration
).freeze
DEFAULT_EXCLUDED_CLASSES =
if Gem::Version.new(Rails.version) < Gem::Version.new('7.1')
%w(
Delayed::Job
PaperTrail::Version
ActiveStorage::Attachment
ActiveStorage::Blob
ActiveRecord::InternalMetadata
ActiveRecord::SchemaMigration
)
else
# ActiveRecord::InternalMetadata and SchemaMigration do not inherit from
# ActiveRecord::Base in Rails 7.1 so we do not need to exclude them.
%w(
Delayed::Job
PaperTrail::Version
ActiveStorage::Attachment
ActiveStorage::Blob
)
end.freeze

mattr_accessor(:default_ignored_columns) { %i(created_at updated_at) }
mattr_accessor(:default_stream_name) { Journaled.default_stream_name }
Expand Down Expand Up @@ -54,7 +66,7 @@ def without_audit_logging
private

def zeitwerk_exclude!(name)
name.constantize.skip_audit_log if Object.const_defined?(name) && !independent_class_in_7_1?(name)
name.constantize.skip_audit_log if Object.const_defined?(name)
Rails.autoloaders.main.on_load(name) { |klass, _path| klass.skip_audit_log }
end

Expand All @@ -63,14 +75,6 @@ def classic_exclude!(name)
rescue NameError
nil
end

def independent_class_in_7_1?(name)
return false if Gem::Version.new(Rails.version) < Gem::Version.new('7.1')

# These classes do not inherit from ActiveRecord::Base in 7.1
# so calling `skip_audit_log` on them will raise.
%w(ActiveRecord::InternalMetadata ActiveRecord::SchemaMigration).include?(name)
end
end

Config = Struct.new(:enabled, :ignored_columns, :enqueue_opts) do
Expand Down

0 comments on commit fb6b937

Please sign in to comment.