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

Returns a new instance of Config.



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

def initialize
  @config = self.class.defaults
end

Class Attribute Details

.generateActiveSupport::OrderedOptions

The subset of configuration options relating to generators.

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

#### ‘#path`

Where to put generated components. Defaults to ‘app/components`:

config.view_component.generate.path = "lib/components"

#### ‘#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.previews.paths` if defined, `“test/components/previews”` otherwise

#### ‘#use_component_path_for_rspec_tests`

Whether to use ‘config.generate.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 `path` to decide where to generate the new RSpec component test. For example, if the `path` is `app/views/components`, then the generator will create a new spec file in `spec/views/components/` rather than the default `spec/components/`.

Returns:

  • (ActiveSupport::OrderedOptions)


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


.instrumentation_enabledBoolean

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

Returns:

  • (Boolean)


131
132
133
# File 'lib/view_component/config.rb', line 131

def default_preview_paths
  (default_rails_preview_paths + default_rails_engines_preview_paths).uniq
end

.previewsActiveSupport::OrderedOptions

The subset of configuration options relating to previews.

#### ‘#controller`

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

config.view_component.previews.controller = "MyPreviewController"

#### ‘#route`

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

config.view_component.previews.route = "/my_previews"

#### ‘#enabled`

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

config.view_component.previews.enabled = false

#### ‘#default_layout`

A custom default layout used for the previews index page and individual previews. Defaults to ‘nil`:

config.view_component.previews.default_layout = "preview_layout"

Returns:

  • (ActiveSupport::OrderedOptions)


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


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.



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

class_attribute :current, default: defaults, instance_predicate: false

Class Method Details

.default_generate_optionsObject



155
156
157
158
159
160
# File 'lib/view_component/config.rb', line 155

def default_generate_options
  options = ActiveSupport::OrderedOptions.new(false)
  options.preview_path = ""
  options.path = "app/components"
  options
end

.default_preview_pathsBoolean

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

Returns:

  • (Boolean)


131
132
133
# File 'lib/view_component/config.rb', line 131

def default_preview_paths
  (default_rails_preview_paths + default_rails_engines_preview_paths).uniq
end

.default_previews_optionsObject



162
163
164
165
166
167
168
169
170
# File 'lib/view_component/config.rb', line 162

def default_previews_options
  options = ActiveSupport::OrderedOptions.new
  options.controller = "ViewComponentsController"
  options.route = "/rails/view_components"
  options.enabled = Rails.env.development? || Rails.env.test?
  options.default_layout = nil
  options.paths = default_preview_paths
  options
end

.default_rails_engines_preview_pathsObject



141
142
143
144
145
146
147
# File 'lib/view_component/config.rb', line 141

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



135
136
137
138
139
# File 'lib/view_component/config.rb', line 135

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

def defaults
  ActiveSupport::OrderedOptions.new.merge!({
    generate: default_generate_options,
    previews: default_previews_options,
    instrumentation_enabled: false
  })
end

.registered_rails_engines_with_previewsObject



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

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