Class: ViewComponent::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component/config.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig



215
216
217
# File 'lib/view_component/config.rb', line 215

def initialize
  @config = self.class.defaults
end

Class Attribute Details

.capture_compatibility_patch_enabledBoolean

Enables the experimental capture compatibility patch that makes ViewComponent compatible with forms, capture, and other built-ins. previews. Defaults to false.



176
177
178
# File 'lib/view_component/config.rb', line 176

def default_preview_paths
  (default_rails_preview_paths + default_rails_engines_preview_paths).uniq
end

.component_parent_classString

The parent class from which generated components will inherit. Defaults to nil. If this is falsy, generators will use ‘“ApplicationComponent”` if defined, `“ViewComponent::Base”` otherwise.



# File 'lib/view_component/config.rb', line 141

.default_preview_layoutString

A custom default layout used for the previews index page and individual previews. Defaults to nil. If this is falsy, ‘“component_preview”` is used.



# File 'lib/view_component/config.rb', line 163

.generateActiveSupport::OrderedOptions

The subset of configuration options relating to generators.

All options under this namespace default to false unless otherwise stated.

#### #sidecar

Always generate a component with a sidecar directory:

config.view_component.generate.sidecar = true

#### #stimulus_controller

Always generate a Stimulus controller alongside the component:

config.view_component.generate.stimulus_controller = true

#### #typescript

Generate TypeScript files instead of JavaScript files:

config.view_component.generate.typescript = true

#### #locale

Always generate translations file alongside the component:

config.view_component.generate.locale = true

#### #distinct_locale_files

Always generate as many translations files as available locales:

config.view_component.generate.distinct_locale_files = true

One file will be generated for each configured I18n.available_locales, falling back to [:en] when no available_locales is defined.

#### #preview

Always generate a preview alongside the component:

config.view_component.generate.preview = true

#### #preview_path

Path to generate preview:

config.view_component.generate.preview_path = "test/components/previews"

Required when there is more than one path defined in preview_paths. Defaults to ‘“”`. If this is blank, the generator will use ViewComponent.config.preview_paths if defined, `“test/components/previews”` otherwise

#### #use_component_path_for_rspec_tests

Whether to use the config.view_component_path when generating new RSpec component tests:

config.view_component.generate.use_component_path_for_rspec_tests = true

When set to true, the generator will use the view_component_path to decide where to generate the new RSpec component test. For example, if the view_component_path is app/views/components, then the generator will create a new spec file in spec/views/components/ rather than the default spec/components/.



# File 'lib/view_component/config.rb', line 32

.instrumentation_enabledBoolean

Whether ActiveSupport notifications are enabled. Defaults to false.



# File 'lib/view_component/config.rb', line 117

.preview_controllerString

The controller used for previewing components. Defaults to ViewComponentsController.



# File 'lib/view_component/config.rb', line 102

.preview_pathsArray<String>

The locations in which component previews will be looked up. Defaults to ‘[’test/components/previews’]‘ relative to your Rails root.



# File 'lib/view_component/config.rb', line 152

.preview_routeString

The entry route for component previews. Defaults to ‘“/rails/view_components”`.



# File 'lib/view_component/config.rb', line 107

.render_monkey_patch_enabledBoolean

If this is disabled, use #render_component or #render_component_to_string instead. Defaults to true.



# File 'lib/view_component/config.rb', line 129

.show_previewsBoolean

Whether component previews are enabled. Defaults to true in development and test environments.



# File 'lib/view_component/config.rb', line 147

.show_previews_sourceBoolean

Whether to display source code previews in component previews. Defaults to false.



# File 'lib/view_component/config.rb', line 112

.test_controllerString

The controller used for testing components. Can also be configured on a per-test basis using #with_controller_class. Defaults to ApplicationController.



# File 'lib/view_component/config.rb', line 157

.use_deprecated_instrumentation_nameBoolean

Whether ActiveSupport Notifications use the private name ‘“!render.view_component”` or are made more publicly available via `“render.view_component”`. Will default to false in next major version. Defaults to true.



# File 'lib/view_component/config.rb', line 122

.view_component_pathString

The path in which components, their templates, and their sidecars should be stored. Defaults to ‘“app/components”`.



# File 'lib/view_component/config.rb', line 135

Instance Attribute Details

#currentViewComponent::Config

Returns the current ViewComponent::Config. This is persisted against this class so that config options remain accessible before the rest of ViewComponent has loaded. Defaults to an instance of ViewComponent::Config with all other documented defaults set.



213
# File 'lib/view_component/config.rb', line 213

class_attribute :current, default: defaults, instance_predicate: false

Class Method Details

.default_generate_optionsObject



200
201
202
203
204
# File 'lib/view_component/config.rb', line 200

def default_generate_options
  options = ActiveSupport::OrderedOptions.new(false)
  options.preview_path = ""
  options
end

.default_preview_pathsBoolean

Enables the experimental capture compatibility patch that makes ViewComponent compatible with forms, capture, and other built-ins. previews. Defaults to false.



176
177
178
# File 'lib/view_component/config.rb', line 176

def default_preview_paths
  (default_rails_preview_paths + default_rails_engines_preview_paths).uniq
end

.default_rails_engines_preview_pathsObject



186
187
188
189
190
191
192
# File 'lib/view_component/config.rb', line 186

def default_rails_engines_preview_paths
  return [] unless defined?(Rails::Engine)

  registered_rails_engines_with_previews.map do |descendant|
    "#{descendant.root}/test/components/previews"
  end
end

.default_rails_preview_pathsObject



180
181
182
183
184
# File 'lib/view_component/config.rb', line 180

def default_rails_preview_paths
  return [] unless defined?(Rails.root) && Dir.exist?("#{Rails.root}/test/components/previews")

  ["#{Rails.root}/test/components/previews"]
end

.defaultsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/view_component/config.rb', line 13

def defaults
  ActiveSupport::OrderedOptions.new.merge!({
    generate: default_generate_options,
    preview_controller: "ViewComponentsController",
    preview_route: "/rails/view_components",
    show_previews_source: false,
    instrumentation_enabled: false,
    use_deprecated_instrumentation_name: true,
    render_monkey_patch_enabled: true,
    view_component_path: "app/components",
    component_parent_class: nil,
    show_previews: Rails.env.development? || Rails.env.test?,
    preview_paths: default_preview_paths,
    test_controller: "ApplicationController",
    default_preview_layout: nil,
    capture_compatibility_patch_enabled: false
  })
end

.registered_rails_engines_with_previewsObject



194
195
196
197
198
# File 'lib/view_component/config.rb', line 194

def registered_rails_engines_with_previews
  Rails::Engine.descendants.select do |descendant|
    defined?(descendant.root) && Dir.exist?("#{descendant.root}/test/components/previews")
  end
end