Doesn't work with View Component >= 3.9.0
Opened this issue · 3 comments
Attempting to upgrade the view_component
gem and running into an issue.
Bridgetown] Exception raised: NoMethodError
[Bridgetown] undefined method `resource' for #<IconComponent:0x00000001113f4398 #... >>>
[Bridgetown] 1: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-view-component-2.0.0/lib/bridgetown-view-component/bridgetown/view_component_helpers.rb:54:in `method_missing'
[Bridgetown] 2: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-routes-1.3.4/lib/bridgetown-routes/view_helpers.rb:19:in `roda_app'
[Bridgetown] 3: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-routes-1.3.4/lib/bridgetown-routes/view_helpers.rb:23:in `request'
[Bridgetown] 4: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-view-component-2.0.0/lib/bridgetown-view-component/bridgetown/view_component_helpers.rb:52:in `method_missing'
[Bridgetown] 5: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-3.12.1/lib/view_component/base.rb:321:in `maybe_escape_html'
[Bridgetown] 6: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-3.12.1/lib/view_component/base.rb:336:in `safe_render_template_for'
[Bridgetown] 7: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-3.12.1/lib/view_component/base.rb:109:in `render_in'
[Bridgetown] 8: /Users/drbragg/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-core-1.3.4/lib/bridgetown-core/ruby_template_view.rb:35:in `render'
...
Doing a bit of digging I found a change in VC 3.9 that might(?) be a source of the problem: ViewComponent/view_component#1950 specifically the changes to lib/view_component/base.rb:310
.
It looks like when request
is called it's calling the request
method in bridgetown-routes/lib/bridgetown-routes/view_helpers.rb:22
which looks at view.resource&.roda_app
. In my case the "view" in question appears to be the component instance.
If I add
def resource
nil
end
to the BaseComponent
everything boots and renders as normal. Not 100% sure if this is the correct work around it just works for me.
Versions
bridgetown: 1.3.4
bridgetown-view-component: 2.0
view_component: >= 3.9.0
config/initializers.rb
Bridgetown.configure do |config|
init :"bridgetown-view-component" do
ViewComponent::Base.config.view_component_path = "src/_components"
end
init :ssr
init :"bridgetown-routes"
end
Let me know if there's any other information I can provide that might be helpful.
I'm inclined to just copy the patch over from citizensadvice/design-system#3362. Any further thoughts @DRBragg and @davidrapson?
Unfortunately we've hit a new error with view component 3.15+ which we're trialing a patch for here citizensadvice/design-system#3503
Not sure how robust that patch is as I'd sidestepped an issue with Bridgetown::ViewComponentHelpers
being included which had a different error.
I think the direction of stubbing in a controller/request object is along the right lines as I think increasingly view component is relying on that being present, which is not the case in a bridgetown context, but still not convinced I've got the right angle on a patch.
I'd be happy to put together a reduced test case with the latest version if that would help.
TBH I haven't touched this is quite some time. If I have some free time in the next week or so I'll take another look at it but until I do I lack update to date enough knowledge to have an informed opinion