Skip to content

Commit

Permalink
Add gem configuration, with base component customization
Browse files Browse the repository at this point in the history
  • Loading branch information
joelzwarrington committed Apr 5, 2024
1 parent 280c902 commit 1514d60
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/components/view_component/form/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ViewComponent
module Form
class BaseComponent < ViewComponent::Base
class BaseComponent < ViewComponent::Form.configuration.parent_component.constantize
class << self
attr_accessor :default_options
end
Expand Down
10 changes: 10 additions & 0 deletions lib/view_component/form.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# frozen_string_literal: true

require "view_component"
require_relative "form/configuration"
require "zeitwerk"

module ViewComponent
module Form
class << self
def configuration
@configuration ||= Configuration.new
end

def configure
yield configuration
end
end
end
end

Expand Down
13 changes: 13 additions & 0 deletions lib/view_component/form/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module ViewComponent
module Form
class Configuration
attr_accessor :parent_component

def initialize
@parent_component = "ViewComponent::Base"
end
end
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/components/application_form_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class ApplicationFormComponent < ViewComponent::Base
end
5 changes: 5 additions & 0 deletions spec/internal/config/initializers/view_component_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

ViewComponent::Form.configure do |config|
config.parent_component = "ApplicationFormComponent"
end
8 changes: 8 additions & 0 deletions spec/view_component/form/base_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def name
it { expect(component.object_errors?).to be(false) }
end
end

describe "parent_component" do
subject { described_class }

context "without configured parent_component" do
it { is_expected.to be < ViewComponent::Base }
end
end
end
8 changes: 8 additions & 0 deletions spec/view_component/form/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,12 @@
it { expect(builder.send(:validation_context)).to eq(:create) }
end
end

describe "base component parent" do
subject(:field) { described_class.new(object_name, object, template, options).send(:component_klass, :text_field) }

it "is configured via initializer" do
expect(field.ancestors).to include(ApplicationFormComponent)
end
end
end
11 changes: 11 additions & 0 deletions spec/view_component/form/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe ViewComponent::Form::Configuration do
subject(:configuration) { described_class.new }

describe "defaults" do
it do
expect(configuration).to have_attributes(parent_component: "ViewComponent::Base")
end
end
end
4 changes: 4 additions & 0 deletions spec/view_component/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
expect(ViewComponent::Form::VERSION).not_to be_nil
end

it "is configurable" do
expect { |block| described_class.configure(&block) }.to yield_with_args(ViewComponent::Form::Configuration)
end

if ENV.fetch("VIEW_COMPONENT_FORM_USE_ACTIONTEXT", "false") == "true"
it "loads ActionText" do
expect(defined?(ActionView::Helpers::Tags::ActionText)).to eq("constant")
Expand Down

0 comments on commit 1514d60

Please sign in to comment.