Skip to content

Defer the inclusion until after ActiveRecord has been fully loaded, improving booting performance and following Rails best practices#118

Merged
ledermann merged 1 commit intoledermann:masterfrom
shashankdass:master_lazy-load-activerecordbase-includes
Aug 9, 2025
Merged

Defer the inclusion until after ActiveRecord has been fully loaded, improving booting performance and following Rails best practices#118
ledermann merged 1 commit intoledermann:masterfrom
shashankdass:master_lazy-load-activerecordbase-includes

Conversation

@shashankdass
Copy link
Contributor

Issue Description
The current implementation references ActiveRecord on gem load which triggers premature loading of Active Record. This can significantly slow down application boot time, especially in development and test environments.

Solution
Use Rails recommended ActiveSupport.on_load(:active_record) hook to defer module inclusion until after ActiveRecord is fully loaded. This improves boot performance and avoids potential load order issues in edge cases.

@ledermann ledermann requested a review from Copilot August 8, 2025 05:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR defers the loading of Rails Settings components until ActiveRecord is fully loaded, improving application boot performance by avoiding premature ActiveRecord initialization. This follows Rails best practices for gem loading order.

Key changes:

  • Wraps ActiveRecord extensions in ActiveSupport.on_load(:active_record) hook
  • Moves rails-settings/setting_object require inside the deferred loading block

def self.has_settings(*args, &block)
RailsSettings::Configuration.new(*args.unshift(self), &block)
ActiveSupport.on_load(:active_record) do
require 'rails-settings/setting_object'
Copy link

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require statement for 'rails-settings/setting_object' is moved inside the on_load block, but there's no explanation for why this specific require needs to be deferred while others (lines 11-12) remain outside. Consider documenting why this require depends on ActiveRecord being loaded or evaluating if other requires should also be moved.

Suggested change
require 'rails-settings/setting_object'

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should be deleted, because it's already in line 1

Copy link
Owner

@ledermann ledermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!

Before merging, the duplicated require should be removed.

def self.has_settings(*args, &block)
RailsSettings::Configuration.new(*args.unshift(self), &block)
ActiveSupport.on_load(:active_record) do
require 'rails-settings/setting_object'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should be deleted, because it's already in line 1

…mproving booting performance and following Rails best practices
@shashankdass shashankdass force-pushed the master_lazy-load-activerecordbase-includes branch from 8c36728 to 0d3da71 Compare August 8, 2025 17:45
@shashankdass
Copy link
Contributor Author

Updated the PR. Removing the duplicate.

@shashankdass shashankdass requested a review from ledermann August 8, 2025 17:47
Copy link
Owner

@ledermann ledermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ledermann ledermann merged commit 8a18af8 into ledermann:master Aug 9, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants