Skip to content

Commit fb62842

Browse files
trevorturkcrmne
andauthored
Fix Zeitwerk eager loading for railtie.rb (#486)
## Summary Completes the fix from PR #59 to prevent crashes during eager loading in non-Rails contexts. ## Problem ``` uninitialized constant RubyLLM::Rails (NameError) class Railtie < Rails::Railtie ``` Occurs when Zeitwerk eager loads (e.g., in gems that depend on ruby_llm but don't use Rails). ## Root Cause **PR #59** added conditional require: `require 'ruby_llm/railtie' if defined?(Rails::Railtie)` This is insufficient because Zeitwerk's eager loading happens *before* that check runs. When Zeitwerk loads `railtie.rb`, the class definition fails because `Rails::Railtie` doesn't exist. ## Solution Two complementary fixes (both required per Zeitwerk documentation): **1. Wrap class definition** in `if defined?(Rails::Railtie)`: ```ruby if defined?(Rails::Railtie) module RubyLLM class Railtie < Rails::Railtie # ... end end end ``` **2. Ignore from Zeitwerk**: ```ruby loader.ignore("#{__dir__}/ruby_llm/railtie.rb") ``` ## Evidence - **Zeitwerk issue #143**: Maintainer confirms conditional loading pattern - **Zeitwerk docs**: Use `loader.ignore()` for "files not following conventions" - **Real-world example**: github/secure_headers uses same pattern ## Testing **Before**: `Zeitwerk::Loader.eager_load_all` → ❌ NameError **After**: `Zeitwerk::Loader.eager_load_all` → ✅ Success Thank you for reviewing! Co-authored-by: Carmine Paolino <[email protected]>
1 parent c1d2aec commit fb62842

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

lib/ruby_llm.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
loader.ignore("#{__dir__}/tasks")
3131
loader.ignore("#{__dir__}/generators")
32+
loader.ignore("#{__dir__}/ruby_llm/railtie.rb")
3233
loader.setup
3334

3435
# A delightful Ruby interface to modern AI language models.

lib/ruby_llm/railtie.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
# frozen_string_literal: true
22

3-
module RubyLLM
4-
# Rails integration for RubyLLM
5-
class Railtie < Rails::Railtie
6-
initializer 'ruby_llm.inflections' do
7-
ActiveSupport::Inflector.inflections(:en) do |inflect|
8-
inflect.acronym 'RubyLLM'
3+
if defined?(Rails::Railtie)
4+
module RubyLLM
5+
# Rails integration for RubyLLM
6+
class Railtie < Rails::Railtie
7+
initializer 'ruby_llm.inflections' do
8+
ActiveSupport::Inflector.inflections(:en) do |inflect|
9+
inflect.acronym 'RubyLLM'
10+
end
911
end
10-
end
1112

12-
initializer 'ruby_llm.active_record' do
13-
ActiveSupport.on_load :active_record do
14-
if RubyLLM.config.use_new_acts_as
15-
require 'ruby_llm/active_record/acts_as'
16-
::ActiveRecord::Base.include RubyLLM::ActiveRecord::ActsAs
17-
else
18-
require 'ruby_llm/active_record/acts_as_legacy'
19-
::ActiveRecord::Base.include RubyLLM::ActiveRecord::ActsAsLegacy
13+
initializer 'ruby_llm.active_record' do
14+
ActiveSupport.on_load :active_record do
15+
if RubyLLM.config.use_new_acts_as
16+
require 'ruby_llm/active_record/acts_as'
17+
::ActiveRecord::Base.include RubyLLM::ActiveRecord::ActsAs
18+
else
19+
require 'ruby_llm/active_record/acts_as_legacy'
20+
::ActiveRecord::Base.include RubyLLM::ActiveRecord::ActsAsLegacy
2021

21-
Rails.logger.warn(
22-
"\n!!! RubyLLM's legacy acts_as API is deprecated and will be removed in RubyLLM 2.0.0. " \
23-
"Please consult the migration guide at https://rubyllm.com/upgrading-to-1-7/\n"
24-
)
22+
Rails.logger.warn(
23+
"\n!!! RubyLLM's legacy acts_as API is deprecated and will be removed in RubyLLM 2.0.0. " \
24+
"Please consult the migration guide at https://rubyllm.com/upgrading-to-1-7/\n"
25+
)
26+
end
2527
end
2628
end
27-
end
2829

29-
rake_tasks do
30-
load 'tasks/ruby_llm.rake'
30+
rake_tasks do
31+
load 'tasks/ruby_llm.rake'
32+
end
3133
end
3234
end
3335
end

0 commit comments

Comments
 (0)