Class: ViewComponent::Config

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @config = self.class.defaults
end

Class Attribute Details

.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.

Returns:

  • (String)


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

.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.

Returns:

  • (String)


133
134
135
136
137
# File 'lib/view_component/config.rb', line 133

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

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

.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

#### #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

Returns:

  • (ActiveSupport::OrderedOptions)


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

.instrumentation_enabledBoolean

Whether ActiveSupport notifications are enabled. Defaults to ‘false`.

Returns:

  • (Boolean)


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

.preview_controllerString

The controller used for previewing components. Defaults to ‘ViewComponentsController`.

Returns:

  • (String)


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

.preview_pathObject

Deprecated.

Use #preview_paths instead. Will be removed in v3.0.0.



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

.preview_pathsArray<String>

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

Returns:

  • (Array<String>)


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

.preview_routeString

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

Returns:

  • (String)


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

.render_monkey_patch_enabledBoolean

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

Returns:

  • (Boolean)

    Whether the #render method should be monkey patched.



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

.show_previewsBoolean

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

Returns:

  • (Boolean)


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

.show_previews_sourceBoolean

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

Returns:

  • (Boolean)


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

.test_controllerString

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

Returns:

  • (String)


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

.view_component_pathString

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

Returns:

  • (String)


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

Class Method Details

.default_preview_pathsString

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

Returns:

  • (String)


133
134
135
136
137
# File 'lib/view_component/config.rb', line 133

def default_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
# File 'lib/view_component/config.rb', line 13

def defaults
  ActiveSupport::OrderedOptions.new.merge!({
    generate: ActiveSupport::OrderedOptions.new(false),
    preview_controller: "ViewComponentsController",
    preview_route: "/rails/view_components",
    show_previews_source: false,
    instrumentation_enabled: false,
    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
  })
end

Instance Method Details

#preview_pathObject



144
145
146
# File 'lib/view_component/config.rb', line 144

def preview_path
  preview_paths
end

#preview_path=(new_value) ⇒ Object



148
149
150
151
# File 'lib/view_component/config.rb', line 148

def preview_path=(new_value)
  ViewComponent::Deprecation.warn("`preview_path` will be removed in v3.0.0. Use `preview_paths` instead.")
  self.preview_paths = Array.wrap(new_value)
end