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

Difficulties Customising group rendering within a Rails Engine (via config.lookbook.preview_controller) #639

Open
joffotron opened this issue Sep 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@joffotron
Copy link

Describe the bug

Hi :) We are also using Lookbook inside a Common UI components Rails Engine in basically the same structure as outlined in this issue: #359

We're encountering either a bug, or a situation where something isn't quite configured right, such that config.lookbook isn't available in our engine init code.

Also something around prepend_view_path is maybe not quite right?

To Reproduce

Steps to reproduce the behavior:

We're trying to override the template used for rendering groups (via a file engines/ui/app/views/lookbook/previews/group.html.erb). We've put that file in, and made a custom Controller (see below)

Trying to follow some of the suggestions from the other issue, including the line
config.lookbook.preview_controller = 'Ui::PreviewController' causes an undefined method 'lookbook' crash on startup:

Users/joff/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8.4/lib/rails/railtie/configuration.rb:96:in `method_missing': undefined method `lookbook' for #<Rails::Engine::Configuration:0x0000000107e242f0 @root=#<Pathname:/Users/joff/src/chargefox/core/engines/ui>, @generators=#<Rails::Configuration::Generators:0x0000000107e242a0 @aliases={}, @options={:rails=>{:orm=>:active_record}, :active_record=>{:migration=>true, :timestamps=>true}}, @fallbacks={}, @templates=[], @colorize_logging=true, @api_only=false, @hidden_namespaces=[], @after_generate_callbacks=[]>, @middleware=#<Rails::Configuration::MiddlewareStackProxy:0x000000010777a580 @operations=[], @delete_operations=[]>, @javascript_path="javascript"> (NoMethodError)
	from /Users/joff/src/chargefox/core/engines/ui/lib/ui/engine.rb:8:in `<class:Engine>'

Interestingly, config.view_component.preview_controller seems to work fine.

Additionally, we have tried moving the require 'lookbook' etc lines into the engines/ui.rb top level file as the other issue showed, but this doesn't seem to make a difference.

Additionally (and this maybe might be useful to update docs around, (or maybe? a bug)..
using prepend_view_path directly in the overridden controller class didn't work, but overriding the prepend_application_view_paths method can be made to pick up the new views path
(see the code below)

Expected behavior

We can override the preview_controller such that we can pick up our alternative group template

Version numbers

Please complete the following information:

  • Lookbook: 2.3.2
  • ViewComponent: 3.14.0
  • Rails: 7.0.8.4
  • Ruby: 3.2.2

Additional context

The gemspec file for our engine contains these deps

spec.add_dependency 'lookbook', '~> 2.3', '>= 2.3.2'
spec.add_dependency 'rails', '>= 7.0.8.4'
spec.add_dependency 'view_component', '~> 3.10'

The engine.rb file is thus:

require 'lookbook'
require 'view_component'

module Ui
  class Engine < ::Rails::Engine
    isolate_namespace Ui

    config.view_component.preview_paths << Ui::Engine.root.join('app/previews')
    config.view_component.default_preview_layout = 'previews'
    config.view_component.preview_controller = 'Ui::PreviewController' # This one doesn't crash at least
    config.lookbook.preview_controller = 'Ui::PreviewController' # This is the line that crashes

    initializer 'ui.assets' do |app|
      app.config.sass.load_paths << Ui::Engine.root.join('app/components')
      app.config.assets.paths << Ui::Engine.root.join('app/components')
    end
  end
end

engines/ui/app/controllers/ui/preview_controller.rb:

module Ui
  class PreviewController < Lookbook::PreviewController
    prepend_view_path Ui::Engine.root.join('app/views') # this didn't work

	# This does though
	private
    def prepend_application_view_paths
      super
      prepend_view_path Ui::Engine.root.join('app/views')
    end
  end
end
@joffotron joffotron added the bug Something isn't working label Sep 4, 2024
@allmarkedup
Copy link
Collaborator

Hey @joffotron, thanks for this - and I appreciate all the detailed info :)

At first glance I'm not sure why trying to access the Lookbook config at start up might cause the app to crash. Some sort of load/initialization order issue maybe but hard to say without digging in a bit deeper.

Just to check - with the workarounds you've described above is the group customisation actually working for you now?

I think I need to set up a very basic Engine along the lines of the one you describe here see if I can replicate it and figure out what is going on. Unless your component library UI engine is open source so I can pull that down and poke around - I'm guessing not?

Tbh it would be good to have an simple (working) example implementation of this sort of setup to point people to if they want to do something similar so I'll try and put something together when I get a chance and see if I can debug the issue.

I'm afraid I don't have as much time as I'd like to work on Lookbook at the moment so it might take me a little while but I'll update you here if I manage to figure out what's going on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants