Skip to content

Conversation

@fcatuhe
Copy link
Contributor

@fcatuhe fcatuhe commented Dec 15, 2025

Summary

  • Fix MoneyRails::ActionViewExtension not being available during eager loading (config.eager_load = true)
  • Use Ruby's autoload to define the constant immediately at require time, deferring file loading until first access

Problem

When a Rails app has config.eager_load = true (production), referencing MoneyRails::ActionViewExtension raises:

uninitialized constant MoneyRails::ActionViewExtension

This happens because the extension is only loaded inside an ActiveSupport.on_load(:action_view) callback. During eager loading, app files (like decorators) that include MoneyRails::ActionViewExtension are loaded before ActionView triggers the callback.

Solution

Use autoload to register the constant immediately while deferring the actual file load:

module MoneyRails
  autoload :ActionViewExtension, 'money-rails/helpers/action_view_extension'
end

The on_load(:action_view) callback still includes the helper into ActionView::Base, but no longer needs to handle the require.

Test plan

  • Verified existing test suite passes
  • Can be tested by creating a decorator that includes MoneyRails::ActionViewExtension in an app with config.eager_load = true

Fixes #614

Use autoload to define the constant immediately at require time,
allowing apps with `config.eager_load = true` to reference
MoneyRails::ActionViewExtension before ActionView loads.

Fixes RubyMoney#614
Copy link
Member

@sunny sunny left a comment

Choose a reason for hiding this comment

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

Awesome fix. Can you add a CHANGELOG entry as well? 🙏🏻

@fcatuhe
Copy link
Contributor Author

fcatuhe commented Dec 16, 2025

Sure, pushed the CHANGELOG entry with similar convention. Thanks!

@fcatuhe
Copy link
Contributor Author

fcatuhe commented Dec 16, 2025

@sunny, I'm happy to add a test if you'd like. I'm thinking a simple autoload verification in spec/helpers/action_view_extension_spec.rb:

describe 'MoneyRails::ActionViewExtension' do
  describe 'autoload' do
    it 'is registered via autoload for eager loading compatibility' do
      expect(MoneyRails.autoload?(:ActionViewExtension)).to eq('money-rails/helpers/action_view_extension')
    end
  end
end

This would catch if someone accidentally removes the autoload line. It doesn't test actual eager loading behavior (that would require a subprocess with eager_load = true), but it documents the fix and guards against regression.

Let me know if you'd like me to add this.

@sunny
Copy link
Member

sunny commented Dec 16, 2025

Yeah, would love that! It’ll make sure future refactors think about the eager-loading use-case.

@fcatuhe
Copy link
Contributor Author

fcatuhe commented Dec 16, 2025

Done and ready for review.

Copy link
Member

@yukideluxe yukideluxe left a comment

Choose a reason for hiding this comment

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

Thanks!

@yukideluxe yukideluxe merged commit f2530be into RubyMoney:main Dec 16, 2025
4 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.

uninitialized constant MoneyRails::ActionViewExtension after upgrading to Rails 6.1

3 participants