Skip to content

Zeitwerk should ignore preview files when previews are disabled #25

@palkan

Description

@palkan

Discussed in #24

Originally posted by dhnaranjo September 23, 2022
Hello! So I'm trying to do the following:

  • keeping Preview files in app/components/component_name
  • building previewed objects using FactoryBot and Faker
  • including FactoryBot::Syntax::Methods in ApplicationViewComponentPreview so I can call build instead of FactoryBot.build
  • excluding FactoryBot from my Production environment

The result is that when my app directory is eager loaded in Production it gets all upset that FactoryBot::Syntax::Methods doesn't exist. To fix this I put together an initializer looks a lil something like this:

# config/initializers/zeitwerk_view_component_previews.rb

unless Rails.env.development? || Rails.env.test?
  Rails.autoloaders.each do |autoloader|
    autoloader.ignore(
      Rails.application.config.view_component.preview_paths.flat_map { Pathname(_1).glob("**/*preview.rb") }
    )
  end
end

I think this is a pretty good idea to include something like this here so other folks can avoid this particular dumb trap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions