Skip to content

feat: migrate from ActiveSupport::Autoload to Zeitwerk #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions closure_tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |gem|

gem.add_dependency 'activerecord', '>= 7.2.0'
gem.add_dependency 'with_advisory_lock', '>= 7.0.0'
gem.add_dependency 'zeitwerk', '~> 2.7'

gem.add_development_dependency 'database_cleaner'
gem.add_development_dependency 'minitest'
Expand Down
52 changes: 12 additions & 40 deletions lib/closure_tree.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
# frozen_string_literal: true

require 'active_record'
require 'zeitwerk'

module ClosureTree
extend ActiveSupport::Autoload

autoload :HasClosureTree
autoload :HasClosureTreeRoot
autoload :Support
autoload :HierarchyMaintenance
autoload :Model
autoload :Finders
autoload :HashTree
autoload :Digraphs
autoload :DeterministicOrdering
autoload :NumericDeterministicOrdering
autoload :Configuration
autoload :AdapterSupport
loader = Zeitwerk::Loader.for_gem
loader.ignore("#{__dir__}/generators")
loader.setup

module ClosureTree
def self.configure
yield configuration
ActiveSupport::Deprecation.new.warn(
Copy link
Preview

Copilot AI Jul 23, 2025

Choose a reason for hiding this comment

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

Creating a new ActiveSupport::Deprecation instance for each call is inefficient. Consider using ActiveSupport::Deprecation.warn directly or creating a shared deprecation instance.

Suggested change
ActiveSupport::Deprecation.new.warn(
ActiveSupport::Deprecation.warn(

Copilot uses AI. Check for mistakes.

'ClosureTree.configure is deprecated and will be removed in a future version. ' \
'Configuration is no longer needed.'
)
yield if block_given?
end

def self.configuration
@configuration ||= Configuration.new
end
end

ActiveSupport.on_load :active_record do
ActiveRecord::Base.extend ClosureTree::HasClosureTree
ActiveRecord::Base.extend ClosureTree::HasClosureTreeRoot
end

# Adapter injection for different database types
ActiveSupport.on_load :active_record_postgresqladapter do
prepend ClosureTree::AdapterSupport
end

ActiveSupport.on_load :active_record_mysql2adapter do
prepend ClosureTree::AdapterSupport
end

ActiveSupport.on_load :active_record_trilogyadapter do
prepend ClosureTree::AdapterSupport
end

ActiveSupport.on_load :active_record_sqlite3adapter do
prepend ClosureTree::AdapterSupport
ActiveSupport.on_load(:active_record) do
extend ClosureTree::HasClosureTree, ClosureTree::HasClosureTreeRoot
end
Comment on lines +20 to 22
Copy link
Preview

Copilot AI Jul 23, 2025

Choose a reason for hiding this comment

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

The context should be ActiveRecord::Base not the block scope. This line should be ActiveRecord::Base.extend ClosureTree::HasClosureTree, ClosureTree::HasClosureTreeRoot to properly extend ActiveRecord::Base with the modules.

Suggested change
ActiveSupport.on_load(:active_record) do
extend ClosureTree::HasClosureTree, ClosureTree::HasClosureTreeRoot
end
ActiveRecord::Base.extend ClosureTree::HasClosureTree, ClosureTree::HasClosureTreeRoot

Copilot uses AI. Check for mistakes.

11 changes: 0 additions & 11 deletions lib/closure_tree/adapter_support.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/closure_tree/configuration.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def has_closure_tree(options = {})
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?

connection_pool.release_connection
rescue StandardError => e
raise e unless ClosureTree.configuration.database_less
end

alias acts_as_tree has_closure_tree
Expand Down
7 changes: 0 additions & 7 deletions lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# frozen_string_literal: true

require 'closure_tree/support_flags'
require 'closure_tree/support_attributes'
require 'closure_tree/numeric_order_support'
require 'closure_tree/active_record_support'
require 'closure_tree/hash_tree_support'
require 'closure_tree/arel_helpers'

# This class and mixins are an effort to reduce the namespace pollution to models that act_as_tree.
module ClosureTree
class Support
Expand Down
14 changes: 0 additions & 14 deletions lib/generators/closure_tree/config_generator.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/generators/closure_tree/templates/config.rb

This file was deleted.