Skip to content

Commit

Permalink
fix: Use Active Support Lazy Load Hooks to avoid prematurely initiali…
Browse files Browse the repository at this point in the history
…zing ActiveRecord::Base and ActiveJob::Base
  • Loading branch information
bensheldon committed Aug 6, 2024
1 parent f999e70 commit 35be70b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ def gem_version
end

def require_dependencies
require 'active_support/lazy_load_hooks'
require_relative 'patches/base'
require_relative 'handlers'
end

def patch_activejob
::ActiveJob::Base.prepend(Patches::Base) unless ::ActiveJob::Base <= Patches::Base

Handlers.subscribe

ActiveSupport.on_load(:active_job) do
::ActiveJob::Base.prepend(Patches::Base) unless ::ActiveJob::Base <= Patches::Base
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,24 @@ def gem_version
end

def patch
# The original approach taken here was to patch each individual module of interest.
# However the patches are applied too late in some applications and as a result the
# Active Record models will not have the instrumentation patches applied.
# Prepending the ActiveRecord::Base class is more consistent in applying
# the patches regardless of initialization order.
#
# Modules to prepend to ActiveRecord::Base are still grouped by the source
# module that they are defined in.
# Example: Patches::PersistenceClassMethods refers to https://github.com/rails/rails/blob/v6.1.0/activerecord/lib/active_record/persistence.rb#L10
::ActiveRecord::Base.prepend(Patches::Querying)
::ActiveRecord::Base.prepend(Patches::Persistence)
::ActiveRecord::Base.prepend(Patches::PersistenceClassMethods)
::ActiveRecord::Base.prepend(Patches::PersistenceInsertClassMethods)
::ActiveRecord::Base.prepend(Patches::TransactionsClassMethods)
::ActiveRecord::Base.prepend(Patches::Validations)

::ActiveRecord::Relation.prepend(Patches::RelationPersistence)
ActiveSupport.on_load(:active_record) do
# Modules to prepend to ActiveRecord::Base are grouped by the source
# module that they are defined in as they are included into ActiveRecord::Base
# Example: Patches::PersistenceClassMethods refers to https://github.com/rails/rails/blob/v6.1.0/activerecord/lib/active_record/persistence.rb#L10
# which is included into ActiveRecord::Base in https://github.com/rails/rails/blob/914caca2d31bd753f47f9168f2a375921d9e91cc/activerecord/lib/active_record/base.rb#L283
::ActiveRecord::Base.prepend(Patches::Querying)
::ActiveRecord::Base.prepend(Patches::Persistence)
::ActiveRecord::Base.prepend(Patches::PersistenceClassMethods)
::ActiveRecord::Base.prepend(Patches::PersistenceInsertClassMethods)
::ActiveRecord::Base.prepend(Patches::TransactionsClassMethods)
::ActiveRecord::Base.prepend(Patches::Validations)

::ActiveRecord::Relation.prepend(Patches::RelationPersistence)
end
end

def require_dependencies
require 'active_support/lazy_load_hooks'
require_relative 'patches/querying'
require_relative 'patches/persistence'
require_relative 'patches/persistence_class_methods'
Expand Down

0 comments on commit 35be70b

Please sign in to comment.