heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Custom Component raise an error with rails 7.1 and zeitwerk #1827

Closed thomasdarde closed 8 months ago

thomasdarde commented 8 months ago

Precheck

Environment

Current behavior

rails zeitwerk:check
W, [2023-12-31T11:53:59.701948 #97489]  WARN -- Skylight: [SKYLIGHT] [6.0.1] Running Skylight in development mode. No data will be reported until you deploy your app.
(To disable this message for all local apps, run `skylight disable_dev_warning`.)
Hold on, I am eager loading the application.
bin/rails aborted!
NameError: uninitialized constant Components::HelpInfoComponent (NameError)

    parent.const_get(cname, false)
          ^^^^^^^^^^
Did you mean?  FileDropComponent

Tasks: TOP => zeitwerk:check
(See full trace by running task with --trace)
module HelpInfoComponent
  def help_info(wrapper_options = nil)
    @help_info ||= if options[:help_info].present?

      data = {"data-controller": "bs-popover", "data-bs-placement": "top", "data-bs-trigger": "hover", "data-bs-content": options[:help_info]}
      html_options = {class: "far fa-question-circle pe-1", title: "Infos", role: "button", tabindex: 0}.merge(data)
      content_tag(:a, nil, html_options)
    end
  end

  def has_help_info?
    help_info.present?
  end
end

Rails.application.config.to_prepare do
  SimpleForm::Inputs::Base.send(:include, HelpInfoComponent)
end

Expected behavior

The code should load or a new way of initializing components should be documented

thomasdarde commented 8 months ago

This issue is linked to lib/ being now autloaded in Rails 7.1

Adding the following lines to the beginning of the component file (lib/components/help_info_component.rb in my case) is apparently sufficient to fix the issue, maybe there is a better way of handling this.

# This declaration is to avoid an issue with zeitwerk
module Components
  class HelpInfoComponent
  end
end