Skip to content

Commit

Permalink
Avoid loading ViewComponent::Base during initialization
Browse files Browse the repository at this point in the history
It forces ActionView::Base to load, which is against Rails' engine
guidelines https://edgeguides.rubyonrails.org/engines.html#load-and-configuration-hooks
  • Loading branch information
jdelStrother committed Sep 22, 2022
1 parent 50f0bf0 commit ca7e7b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class << self
delegate(*ViewComponent::Config.defaults.keys, to: :config)

def config
@config ||= ViewComponent::Config.defaults
@config ||= ActiveSupport::OrderedOptions.new
end
attr_writer :config
end

include ViewComponent::ContentAreas
Expand Down
9 changes: 4 additions & 5 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require "rails"
require "view_component/base"
require "view_component/config"

module ViewComponent
class Engine < Rails::Engine # :nodoc:
config.view_component = ViewComponent::Base.config
config.view_component = ViewComponent::Config.defaults

rake_tasks do
load "view_component/rails/tasks/view_component.rake"
Expand All @@ -14,9 +14,6 @@ class Engine < Rails::Engine # :nodoc:
initializer "view_component.set_configs" do |app|
options = app.config.view_component

%i[generate preview_controller preview_route show_previews_source].each do |config_option|
options[config_option] ||= ViewComponent::Base.public_send(config_option)
end
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
options.render_monkey_patch_enabled = true if options.render_monkey_patch_enabled.nil?
options.show_previews = Rails.env.development? || Rails.env.test? if options.show_previews.nil?
Expand All @@ -40,6 +37,8 @@ class Engine < Rails::Engine # :nodoc:

initializer "view_component.enable_instrumentation" do |app|
ActiveSupport.on_load(:view_component) do
Base.config = app.config.view_component

if app.config.view_component.instrumentation_enabled.present?
# :nocov:
ViewComponent::Base.prepend(ViewComponent::Instrumentation)
Expand Down

0 comments on commit ca7e7b7

Please sign in to comment.