Skip to content
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

Don't skip the first namespace for controller paths #80

Merged
merged 1 commit into from
Jun 15, 2023

Conversation

elia
Copy link
Member

@elia elia commented Jun 15, 2023

Summary

Autoload paths are meant to list autoload roots and not the first level of the namespace.

E.g. having the following controller in lib/controllers/solidus_foo:

lib/controllers/solidus_foo/spree/my_controller.rb

The autoload will be triggered by a constant reference to Spree::MyController targeting spree/my_controller.rb inside lib/controllers/solidus_foo/. This means the autoload path needs to be lib/controllers/solidus_foo/.

See also https://edgeguides.rubyonrails.org/classic_to_zeitwerk_howto.html#globs-in-config-autoload-paths

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@elia elia self-assigned this Jun 15, 2023
@elia
Copy link
Member Author

elia commented Jun 15, 2023

@DanielePalombo @mamhoff can you confirm if fixes the issue for your projects?

@elia elia marked this pull request as ready for review June 15, 2023 13:19
@@ -110,7 +110,7 @@ def enable_solidus_engine_support(engine)
decorators_path = root.join("lib/decorators/#{engine}")
controllers_path = root.join("lib/controllers/#{engine}")
config.autoload_paths += decorators_path.glob('*')
config.autoload_paths += controllers_path.glob('*')
config.autoload_paths << controllers_path if controller_path.exist?
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, but should the line 112 (just above) not receive the same treatment?

Copy link
Member Author

@elia elia Jun 15, 2023

Choose a reason for hiding this comment

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

Good call, I'd expect that folder to have an additional layer and be similar to app/ in that regard, eg. lib/decorators/solidus_foo/models/spree/product/my_decorator.rb lib/decorators/frontend/models/spree/product/my_decorator.rb

An example from auth devise:

lib/decorators/frontend/controllers/spree/checkout_controller_decorator.rb

Autoload paths are meant to list autoload roots and not the first
level of the namespace.

E.g. having the following controller in lib/controllers/solidus_foo:

    lib/controllers/solidus_foo/spree/my_controller.rb

The autoload will be triggered by a constant reference to
`Spree::MyController` targeting `spree/my_controller.rb` inside
`lib/controllers/solidus_foo/`. This means the autoload path needs to
be `lib/controllers/solidus_foo/`.
@elia elia force-pushed the elia/autoload-paths-fix branch from 36d8d26 to 45f1a6a Compare June 15, 2023 14:37
Copy link
Contributor

@DanielePalombo DanielePalombo left a comment

Choose a reason for hiding this comment

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

LGTM.

@elia elia requested a review from kennyadsl June 15, 2023 14:52
@kennyadsl kennyadsl merged commit eaeb281 into master Jun 15, 2023
@kennyadsl kennyadsl deleted the elia/autoload-paths-fix branch June 15, 2023 14:57
@kennyadsl
Copy link
Member

Released in 0.9.3

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.

Autoloading controllers in extension lib stopped working
4 participants