bridgetownrb / bridgetown

A next-generation progressive site generator & fullstack framework, powered by Ruby
https://www.bridgetownrb.com
MIT License
1.13k stars 114 forks source link

`ViewComponentHelpers.allow_rails_helpers` doesn't work #665

Closed joemasilotti closed 1 year ago

joemasilotti commented 1 year ago

When using Rails helpers the :tag option only works when the server automatically reloads. But not on first launch.

Bridgetown Version: bridgetown 1.1.0 "Belmont"

To Reproduce

  1. Follow the official ViewComponent tutorial
  2. Enable :tag helper from Rails
  3. Use the tag helper in a component

Current behavior If starting the app cold it crashes with the error produced below.

Note that if the tag helper is not used, then the server is started, then the tag helper is used it works as expected.

Expected behavior The tag helper should not raise an error.

Computing environment (please complete the following information):

Additional context

[Bridgetown]           Starting: Bridgetown v1.1.0 (codename "Belmont")
[Server] * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
[Server] * PID: 99909
[Frontend] touch frontend/styles/jit-refresh.css
[Frontend] yarn run esbuild-dev
[Frontend] yarn run v1.22.19
[Frontend] $ node esbuild.config.js --watch
[Frontend] esbuild: frontend bundling started...
[Frontend] esbuild: frontend bundling complete!
[Frontend] esbuild: entrypoints processed:
[Frontend]          - index.EVFXU2NE.js: 78B
[Frontend]          - index.ETTOPBPI.css: 16.87KB
[Bridgetown]        Environment: development
[Bridgetown]             Source: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src
[Bridgetown]        Destination: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/output
[Bridgetown]     Custom Plugins: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/plugins
[Bridgetown]         Generating…
[Bridgetown]         Pagination: disabled. Enable in site config with pagination:\n enabled: true
[Bridgetown]   Conversion error: Bridgetown::Converters::ERBTemplates encountered an error while converting `index.erb'
[Bridgetown] Stopping auxiliary processes...
[Bridgetown]   Exception raised: NameError
[Bridgetown] undefined local variable or method `tag' for #<SiteComponent:0x00000001147bdff8 @view_context=#<Bridgetown::ERBView layout=default resource=index.erb>, @__vc_original_view_context=#<Bridgetown::ERBView layout=default resource=index.erb>, @output_buffer="", @lookup_context={"variants"=>[]}, @view_renderer=nil, @view_flow=nil, @virtual_path="/Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src/_components/site_component", @__vc_variant=nil, @current_template=nil, @__vc_content_evaluated=false, @__vc_render_in_block=nil, @helpers=#<Bridgetown::RubyTemplateView::Helpers:0x00000001147bd788 @view=#<SiteComponent:0x00000001147bdff8 ...>, @site=#<Bridgetown::Site "title"=>"Masilotti.com", "tagline"=>"Joe Masilotti’s blog on Ruby on Rails, iOS, Turbo Native, and automated testing.", "email"=>"joe@masilotti.com", "description"=>"A blog from Joe Masilotti, an independent developer who's passionate about clean, testable code.", "twitter"=>"joemasilotti", "github"=>"joemasilotti">, @context=#<struct Bridgetown::RubyTemplateView::Helpers::Context registers={:site=>#<Bridgetown::Site "title"=>"Masilotti.com", "tagline"=>"Joe Masilotti’s blog on Ruby on Rails, iOS, Turbo Native, and automated testing.", "email"=>"joe@masilotti.com", "description"=>"A blog from Joe Masilotti, an independent developer who's passionate about clean, testable code.", "twitter"=>"joemasilotti", "github"=>"joemasilotti">}>>>
Did you mean?  tap
[Bridgetown]                  1: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-view-component-1.0.0/lib/bridgetown-view-component/bridgetown/view_component_helpers.rb:52:in `method_missing'
[Bridgetown]                  2: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src/_components/site_component.rb:7:in `call'
[Bridgetown]                  3: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-2.74.1/lib/view_component/compiler.rb:112:in `render_template_for'
[Bridgetown]                  4: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-2.74.1/lib/view_component/base.rb:131:in `render_in'
[Bridgetown]                  5: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-core-1.1.0/lib/bridgetown-core/ruby_template_view.rb:31:in `render'
[Bridgetown]          Backtrace: Use the --trace option for complete information.
joemasilotti commented 1 year ago

Turns out you need to allow the Rails helpers before including the Bridgetown helpers.

This works:

class SiteComponent < ViewComponent::Base
  Bridgetown::ViewComponentHelpers.allow_rails_helpers :tag
  include Bridgetown::ViewComponentHelpers
end

This does not work:

class SiteComponent < ViewComponent::Base
  include Bridgetown::ViewComponentHelpers
  Bridgetown::ViewComponentHelpers.allow_rails_helpers :tag
end
jaredcwhite commented 1 year ago

@joemasilotti Ah, that makes sense. Maybe there's a way we can document this better or otherwise make it more obvious how it works?

joemasilotti commented 1 year ago

Done! #687.