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

Defer modification of ActionController::Base to when it is loaded #250

Merged

Conversation

cjlarose
Copy link
Member

The railtie for this gem adds an initializer that loads the class ActionController::Base in order to add a before_action to it.

The initializer references the ActionController::Base constant directly, which, if it isn't already loaded, will be loaded at that moment. loading that class causes other initializers run, including, for example, the initializer defined by actiontext's engine, as well as various initializers defined by third-party gems in a given application.

The effect is that adding the config gem to a Rails application that didn't have it causes that application to load ActionController::Base (and possibly a bunch of related stuff) during initialization, even in contexts where usage of ActionController::Base isn't expected, like when launching a console through rails console.

The solution here is to use ActiveSupport::LazyLoadHooks to add a hook that runs only after the ActionController::Base has been loaded.

The block passed to `ActiveSupport.on_load` is executed with the
receiver set to the loaded class.
@rdubya
Copy link
Contributor

rdubya commented Oct 17, 2019

Seems like a good idea to me, but can you look into the failing travis build and see if it is related to your change? It doesn't seem like it would be, but I don't see those failures in the last couple of commits to master.

@pkuczynski
Copy link
Member

I agree, and it seems like a very good change! We can merge it and release it as soon as the build is green and CHANGELOG entry is added. @cjlarose can u have a look at that?

@pkuczynski pkuczynski added this to the 2.0.1 milestone Oct 17, 2019
@cjlarose
Copy link
Member Author

I'll take a look and add the changelog entry. Thanks!

The current version of sprockets is 4.0.0, but the Rails 4.2 app used
in the tests is incompatible with that version of sprockets.
@cjlarose
Copy link
Member Author

The test failures ended up being unrelated to my change, but I fixed it anyway.

The most recent build on master happened to install sprockets version 3.7.2 when installing the sample Rails 4.2 application. But when my PR ran a month later, sprockets had published a new major version (4.0.0). For whatever reason, the sample Rails 4.2 application in this project is incompatible with sprockets 4.0.0 (not surprising, considering it's a major version release). All I did was modify the gemfile for the Rails 4.2 application so that it specifies that we want sprockets 3.7.x.

@cjlarose cjlarose force-pushed the defer-actioncontroller-initialization branch from c45d936 to 0c37b8d Compare October 18, 2019 00:01
@rdubya
Copy link
Contributor

rdubya commented Oct 18, 2019

Awesome, thanks for finding/fixing the sprockets issue.

@jrafanie
Copy link
Contributor

@cjlarose I didn't see your fix for tests with rails 4.2 with sprockets 4 so I extracted a single PR to do it, #254

@@ -23,7 +23,7 @@ def preload
# Development environment should reload settings on every request
if ::Rails.env.development?
initializer :config_reload_on_development do
ActionController::Base.class_eval do
ActiveSupport.on_load :action_controller_base do
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@amatsuda @Fryguy which version is better? This one or the one from #258?

Copy link
Member

Choose a reason for hiding this comment

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

I think this version is better. I don't think the class_eval is needed because on_load takes care of it being in the right context.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's what I thought too! Then I will merge this one!

pkuczynski
pkuczynski previously approved these changes Jan 6, 2020
@pkuczynski
Copy link
Member

Thank you @cjlarose!

@pkuczynski pkuczynski merged commit 0ce94f0 into rubyconfig:master Jan 6, 2020
@pkuczynski pkuczynski mentioned this pull request Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants