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

Initialize ViewComponent::Config with defaults before framework load #1774

Merged
merged 5 commits into from
Aug 23, 2023

Conversation

boardfish
Copy link
Collaborator

@boardfish boardfish commented Jun 18, 2023

This PR introduces ViewComponent::Config.current, which should not be used directly by consumers of ViewComponent, but is used as the common object from which ViewComponent::Base and Rails.application.config.view_component will load their config once each is ready. It'll make config available from the very start with the intent of not relying upon the framework to find these options - we can manually set anything reliant on Action View, for example, within the app or engine config.

#1659 made the config object empty before ViewComponent and Rails were initialised. This would mean that code within ViewComponent that relied on configured defaults would break, resulting in issues like #1741. Since ViewComponent::Config can be loaded independently of Action View, this should hopefully no longer be the case.

If you were affected by #1741, please test against this branch to see if it fixes your issue. Thanks for your patience!

@boardfish boardfish force-pushed the fix-virtual-path-nil-viewcomponent-path branch from cb03c95 to a9fb769 Compare June 18, 2023 10:30
@boardfish boardfish marked this pull request as ready for review June 18, 2023 10:31
@boardfish boardfish linked an issue Jun 18, 2023 that may be closed by this pull request
@boardfish boardfish force-pushed the fix-virtual-path-nil-viewcomponent-path branch from 7a89729 to e4268dc Compare June 21, 2023 08:18
Copy link
Contributor

@camertron camertron left a comment

Choose a reason for hiding this comment

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

Hey @boardfish thanks for working on this. I don't understand how current get set - there doesn't appear to be any call to it?

@Spone
Copy link
Collaborator

Spone commented Jun 26, 2023

Hey @boardfish thanks for working on this. I don't understand how current get set - there doesn't appear to be any call to it?

I think the defaults class method is called here:

class_attribute :current, default: defaults, instance_predicate: false

This commit introduces `ViewComponent::Config.current`, which should not be used directly by consumers of ViewComponent, but is used as the common thread from which `ViewComponent::Base` and `Rails.application.config.view_component` will load their config once each is ready. It'll make config available from the very start with the intent of not relying upon the framework to find these options - we can manually set anything reliant on Action View, for example, within app/engine config.
@boardfish boardfish force-pushed the fix-virtual-path-nil-viewcomponent-path branch from e4268dc to a3ed95f Compare July 19, 2023 15:53
@boardfish boardfish requested a review from camertron July 19, 2023 15:59
@boardfish
Copy link
Collaborator Author

Hey folks, is there anything else you'd like me to do with this so we can get it shipped? I'm aware there are now multiple issues that this will fix.

Copy link
Collaborator

@Spone Spone left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@@ -4,7 +4,7 @@

module ViewComponent # :nodoc:
class Preview
include Rails.application.routes.url_helpers if defined?(Rails.application.routes.url_helpers)
include Rails.application.routes.url_helpers if Rails && Rails.application.present?
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need the defined? check to support non-Rails environments?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think this must've fallen through in a rebate 🧐 I'll make sure to fix it before it gets merged.

@camertron
Copy link
Contributor

@Spone looks like there's a coverage issue as well 😦

@Spone
Copy link
Collaborator

Spone commented Aug 9, 2023

Do you know what's happening with the coverage @boardfish?

@boardfish
Copy link
Collaborator Author

I think it wants ViewComponent::Base#config= to be tested, but I think that method might even be able to be removed.

@ryansdwilson
Copy link

Thank you all for your work on this! I ran into this issue today and went down a rabbit hole trying to figure it out. I really appreciate all the work you're doing to maintain VC.

@camertron camertron self-assigned this Aug 23, 2023
@camertron camertron merged commit 6979de9 into main Aug 23, 2023
37 checks passed
@camertron camertron deleted the fix-virtual-path-nil-viewcomponent-path branch August 23, 2023 22:06
@boardfish
Copy link
Collaborator Author

Thanks for wrapping this up @camertron, and sorry I haven't been able to dedicate the time to it! 😅 🙏

Copy link
Collaborator

@rmacklin rmacklin left a comment

Choose a reason for hiding this comment

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

I'll open a new PR to address this feedback since this one is already merged

@@ -4,7 +4,7 @@

module ViewComponent # :nodoc:
class Preview
include Rails.application.routes.url_helpers if defined?(Rails.application.routes.url_helpers)
include Rails.application.routes.url_helpers if defined?(Rails) && Rails.application.present?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think #1774 (review) was resolved correctly - I think ultimately this change can be reverted.

Originally @boardfish had committed

diff --git a/lib/view_component/preview.rb b/lib/view_component/preview.rb
index e80087404..3176e4ecd 100644
--- a/lib/view_component/preview.rb
+++ b/lib/view_component/preview.rb
@@ -4,7 +4,7 @@
 
 module ViewComponent # :nodoc:
   class Preview
-    include Rails.application.routes.url_helpers
+    include Rails.application.routes.url_helpers if Rails && Rails.application.present?
     include ActionView::Helpers::TagHelper
     include ActionView::Helpers::AssetTagHelper
     extend ActiveSupport::DescendantsTracker

in e4268dc before 0d6564a had landed in master. When the PR was later rebased, this change should've been dropped, as @boardfish realized in #1774 (comment).

Technically, it's possible (though admittedly weird) to have an environment where Rails.application is present even if Rails.application.routes.url_helpers is not defined - and the change introduced here would no longer handle that correctly.

Comment on lines -28 to -30
# Replaces the entire config. You shouldn't need to use this directly
# unless you're building a `ViewComponent::Config` elsewhere.
attr_writer :config
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to regenerate the YARD docs after removing this.

Comment on lines +170 to +176
# @!attribute current
# @return [ViewComponent::Config]
# Returns the current ViewComponent::Config. This is persisted against this
# class so that config options remain accessible before the rest of
# ViewComponent has loaded. Defaults to an instance of ViewComponent::Config
# with all other documented defaults set.
class_attribute :current, default: defaults, instance_predicate: false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Likewise, we need to regenerate the YARD docs to include this.

claudiob pushed a commit to claudiob/view_component that referenced this pull request Dec 22, 2023
…iewComponent#1774)

* Revert "Avoid loading ActionView::Base during initialization (ViewComponent#1659)"

This reverts commit 01510f3.

* Initialize ViewComponent::Config with defaults before framework load

This commit introduces `ViewComponent::Config.current`, which should not be used directly by consumers of ViewComponent, but is used as the common thread from which `ViewComponent::Base` and `Rails.application.config.view_component` will load their config once each is ready. It'll make config available from the very start with the intent of not relying upon the framework to find these options - we can manually set anything reliant on Action View, for example, within app/engine config.

* Only include URL helpers if app is loaded

* Get this across the finish line

---------

Co-authored-by: Cameron Dutro <camertron@gmail.com>
claudiob pushed a commit to claudiob/view_component that referenced this pull request Jan 3, 2024
…iewComponent#1774)

* Revert "Avoid loading ActionView::Base during initialization (ViewComponent#1659)"

This reverts commit 01510f3.

* Initialize ViewComponent::Config with defaults before framework load

This commit introduces `ViewComponent::Config.current`, which should not be used directly by consumers of ViewComponent, but is used as the common thread from which `ViewComponent::Base` and `Rails.application.config.view_component` will load their config once each is ready. It'll make config available from the very start with the intent of not relying upon the framework to find these options - we can manually set anything reliant on Action View, for example, within app/engine config.

* Only include URL helpers if app is loaded

* Get this across the finish line

---------

Co-authored-by: Cameron Dutro <camertron@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants