Sha256: d917d5a2bc73723cf76a2723a4f1f5f376a0ea8c0df2c143efaa786d5038519f

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "rails"
require "view_component"

module ViewComponent
  class Engine < Rails::Engine # :nodoc:
    config.view_component = ActiveSupport::OrderedOptions.new

    initializer "view_component.set_configs" do |app|
      options = app.config.view_component

      options.show_previews = Rails.env.development? if options.show_previews.nil?

      if options.show_previews
        options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/components/previews" : nil
      end

      ActiveSupport.on_load(:view_component) do
        options.each { |k, v| send("#{k}=", v) }
      end
    end

    initializer "view_component.set_autoload_paths" do |app|
      options = app.config.view_component

      if options.show_previews && options.preview_path
        ActiveSupport::Dependencies.autoload_paths << options.preview_path
      end
    end

    initializer "view_component.eager_load_actions" do
      ActiveSupport.on_load(:after_initialize) do
        ViewComponent::Base.descendants.each(&:compile)
      end
    end

    initializer "view_component.compile_config_methods" do
      ActiveSupport.on_load(:view_component) do
        config.compile_methods! if config.respond_to?(:compile_methods!)
      end
    end

    initializer "view_component.monkey_patch_render" do
      ActiveSupport.on_load(:action_view) do
        ActionView::Base.prepend ViewComponent::RenderMonkeyPatch if Rails.version.to_f < 6.1
      end

      ActiveSupport.on_load(:action_controller) do
        ActionController::Base.prepend ViewComponent::RenderingMonkeyPatch if Rails.version.to_f < 6.1
      end
    end

    config.after_initialize do |app|
      options = app.config.view_component

      if options.show_previews
        app.routes.prepend do
          get "/rails/view_components"       => "rails/view_components#index", :internal => true
          get "/rails/view_components/*path" => "rails/view_components#previews", :internal => true
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
view_component-2.3.0 lib/view_component/engine.rb
view_component-2.2.2 lib/view_component/engine.rb
view_component-2.2.1 lib/view_component/engine.rb
view_component-2.2.0 lib/view_component/engine.rb
view_component-2.1.0 lib/view_component/engine.rb
view_component-2.0.0 lib/view_component/engine.rb