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

Make rails engines zeitwerk autoloader friendly #205

Merged
merged 2 commits into from
Apr 28, 2023

Conversation

jrafanie
Copy link
Member

@jrafanie jrafanie commented Apr 20, 2023

  • Rename constants that aren't really acronyms to avoid having to specify this manually
  • Use the namespaced lib/manageiq/consumption.rb instead of lib/manageiq-consumption.rb as the former is zeitwerk friendly and both are acceptable for bundler

Note, this is an attempt to minimize the changes in the engine but more changes may be needed when we drop the old loader and switch to zeitwerk. This change should be mergeable today without changing to zeitwerk and is an example of what I'll be doing in other engines.

These aren't widely known acronyms, so Zeitwerk and rails need to be told about
them.  We should avoid using acronyms and default to using the "rails"
convention unless it's warranted to specify acronyms.  All other usages of the
same acronyms would need to be enforced in locations so it's not without side
effects.

It's just not worth it here.
There is no need create a file called lib/my-gem.rb.
Bundler will automatically treat this as a namespace due to the hyphen, so it
will try a my-gem require first and then fallback on my/gem.

See:
https://github.com/rubygems/rubygems/blob/a223274e4627e8a76112415c0e38f8a45a42f97e/bundler/lib/bundler/runtime.rb#L72-L73

Because we're moving towards zeitwerk, ruby files should be named based on their
namespace/class names.

1) Moving / renaming based on a namespaced gem name works automatically with
bundler.
2) Zeitwerk knows to ignore this file, so we don't need to.
@jrafanie
Copy link
Member Author

cc @agrare

@Fryguy
Copy link
Member

Fryguy commented Apr 20, 2023

Asid, but semi-related, do we even "use" this in the core app - we've wanted to remove this repo for a while, and theres a small subset that's actually used that we should move to the core repo.

@jrafanie
Copy link
Member Author

Asid, but semi-related, do we even "use" this in the core app - we've wanted to remove this repo for a while, and theres a small subset that's actually used that we should move to the core repo.

yup, I can see it used here:

app/models/chargeback.rb:      plan = ManageIQ::Showback::PricePlan.find_or_create_by(:description => rate.description,
app/models/chargeback_rate_detail.rb:    showback_rate = ManageIQ::Showback::Rate.find_or_create_by(:entity      => entity,
spec/models/chargeback_configured_system_spec.rb:    ManageIQ::Showback::InputMeasure.seed
spec/models/chargeback_container_project_spec.rb:    ManageIQ::Showback::InputMeasure.seed
spec/models/chargeback_vm_spec.rb:    ManageIQ::Showback::InputMeasure.seed

I'd love to remove it or the unused parts but I'm just not very confident in how it works to do it.

@jrafanie
Copy link
Member Author

Note, if we go the route of eager loading with zeitwerk, there are two additional changes we can do later on:

diff --git a/lib/manageiq/consumption.rb b/lib/manageiq/consumption.rb
index b0823f2..3e0a410 100644
--- a/lib/manageiq/consumption.rb
+++ b/lib/manageiq/consumption.rb
@@ -1,2 +1,5 @@
 require "manageiq/showback/engine"
 require "manageiq/showback/version"
+
+module ManageIQ::Consumption
+end
diff --git a/lib/manageiq/showback/engine.rb b/lib/manageiq/showback/engine.rb
index 34dcbbb..dd804ef 100644
--- a/lib/manageiq/showback/engine.rb
+++ b/lib/manageiq/showback/engine.rb
@@ -6,6 +6,7 @@ module ManageIQ
       isolate_namespace ManageIQ::Showback

       config.autoload_paths << root.join('lib').to_s
+      config.eager_load_paths << root.join('lib').to_s

       def self.vmdb_plugin?
         true

@miq-bot
Copy link
Member

miq-bot commented Apr 25, 2023

Checked commits jrafanie/manageiq-consumption@702dd87~...d883ad5 with ruby 2.6.10, rubocop 1.28.2, haml-lint 0.35.0, and yamllint
14 files checked, 0 offenses detected
Everything looks fine. ⭐

@jrafanie
Copy link
Member Author

This is ready for review/merge.

@kbrock
Copy link
Member

kbrock commented Apr 28, 2023

fun: bin/console had require "manageiq/consumption"
it is as if they knew

@kbrock kbrock merged commit 377567a into ManageIQ:master Apr 28, 2023
@kbrock kbrock added the rails7 label Apr 28, 2023
@kbrock kbrock assigned kbrock and unassigned Fryguy Apr 28, 2023
@jrafanie jrafanie deleted the zeitwerk branch January 31, 2024 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants