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 sure annotate_rendered_view_with_filenames is configured #130

Merged
merged 3 commits into from
Mar 19, 2024

Conversation

etiennebarrie
Copy link
Member

This fixes two problems with #122.

First the configuration is properly copied (we needed to add our on_load hook in an after_initialize so that it's added after the configuration is copied from the Rails application to ActionView::Base). Replaces #128.

Second, we only do this if the configuration exists, keeping compatibility with 6.0. Fixes #129.

Action View is setup to copy the configuration from the application after
initialization is finished, using `ActiveSupport.on_load(:action_view)`
to make sure it only happens when `ActionView::Base` is loaded.

We need to add our on_load hook after that, so we're also putting it in
an after_initialize. Typically apps load all Rails railties before
requiring gems with Bundler, so our after_initialize should always run
after the one from Action View.
@etiennebarrie etiennebarrie merged commit 8942d6b into main Mar 19, 2024
31 checks passed
@etiennebarrie etiennebarrie deleted the fix-annotate_rendered_view_with_filenames-config branch March 19, 2024 13:22

config.after_initialize do
Copy link
Member

Choose a reason for hiding this comment

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

Reverting this change I still get the test passing

Copy link
Member

Choose a reason for hiding this comment

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

I fixed the test.

Copy link
Member Author

Choose a reason for hiding this comment

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

The test was working fine. When running test_helper, the first time Rails wasn't defined and lib/better_html.rb wouldn't require the railtie:

require "better_html/railtie" if defined?(Rails)

But then when the specific test file would be loaded, we would make sure the railtie is loaded on its own. Since the actual config-copying code only runs when ActionView::Base is loaded, and that only happens later in the test with this hack, the initializer code copying the config executed and the test was checking that the config was indeed copied

_ = ActionView::Base

Still your commit makes it better because the railtie will always be loaded before the dummy application is initialized, the way it's done in a real Rails app. In other words the tests don't rely on the fact that it all happens in an on_load(:action_view). 👍

@rafaelfranca
Copy link
Member

Since nobody was able to explain to me why the old code didn't work, just that it didn't work, the reason is because action_view configuration, differently of the other frameworks are only copied in after_initialize blocks

@etiennebarrie
Copy link
Member Author

The PR description and the commit message in ea19da1 tried to explain that.

It's more complicated than that, the config from application to ActionView::Base is not really only copied after initialize, but the copy itself is setup via a on_load hook in the after_initialize.

Active Record does:

  • initializer:
    • after_initialize: copy config
    • on_load copy config but not all of them

While Action View does:

  • after_initialize:
    • on_load: copy config

So for this gem here, not only do we have to copy the config after initialize, we have to copy the config in an on_load hook in the after initialize so that it runs after the hook that copies the configs. That's why the old code didn't work.

@iainbeeston
Copy link
Contributor

@etiennebarrie Thanks for fixing this and sorry for breaking rails 6 in the first place

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.

Rails 6.0 incompatible: annotate_rendered_view_with_filenames
4 participants