Skip to content

Commit

Permalink
🗿 Rock-conaissance, also break config
Browse files Browse the repository at this point in the history
The existing `ViewComponent::Config` object seems fit for purpose when it comes to component-local config, but I think what we want is:
- a `RailsAppConfig` object that's responsible for the things that will still need to be set against `Rails.application.config`
- a `ComponentLocalConfig` object that's effectively `ViewComponent::Config`. We should make sure that config is inheritable, and perhaps provide some kind of DSL to set it up, e.g.:

```rb
class ApplicationComponent < ViewComponent::Base
  configure do
    some_option true
    some_other_option "value"
  end
end
```
  • Loading branch information
boardfish committed Feb 15, 2025
1 parent 37e89f8 commit c7cf9a7
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 220 deletions.
1 change: 1 addition & 0 deletions lib/rails/generators/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ComponentGenerator < Rails::Generators::NamedBase
check_class_collision suffix: "Component"

class_option :inline, type: :boolean, default: false
# TODO: Settings in this file should be Rails app-local config.
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
class_option :parent, type: :string, desc: "The parent class for the generated component"
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
Expand Down
1 change: 1 addition & 0 deletions lib/rails/generators/locale/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
class_option :sidecar, type: :boolean, default: false

def create_locale_file
# TODO: This should be app-local config
if ViewComponent::Base.config.generate.distinct_locale_files
I18n.available_locales.each do |locale|
create_file destination(locale), translations_hash([locale]).to_yaml
Expand Down
1 change: 1 addition & 0 deletions lib/rails/generators/rspec/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create_test_file
private

def spec_component_path
# TODO: This is probably Rails app-local config too.
return "spec/components" unless ViewComponent::Base.config.generate.use_component_path_for_rspec_tests

configured_component_path = component_path
Expand Down
13 changes: 1 addition & 12 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "action_view"
require "active_support/configurable"
require "view_component/collection"
require "view_component/compile_cache"
require "view_component/compiler"
Expand All @@ -19,17 +18,6 @@

module ViewComponent
class Base < ActionView::Base
class << self
delegate(*ViewComponent::Config.defaults.keys, to: :config)

# Returns the current config.
#
# @return [ActiveSupport::OrderedOptions]
def config
ViewComponent::Config.current
end
end

include ViewComponent::InlineTemplate
include ViewComponent::UseHelpers
include ViewComponent::Slotable
Expand Down Expand Up @@ -542,6 +530,7 @@ def render_template_for(requested_details)
child.identifier = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path

# If Rails application is loaded, removes the first part of the path and the extension.
# FIXME: This seems like it'll be inflexible to configuration right now.
if defined?(Rails) && Rails.application
child.virtual_path = child.identifier.gsub(
/(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, ""
Expand Down
Loading

0 comments on commit c7cf9a7

Please sign in to comment.