Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-brousse committed Sep 12, 2021
1 parent d14b54e commit 0336865
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 26 deletions.
4 changes: 4 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Combustion.initialize! :all do
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(nil))
config.log_level = :fatal

config.cache_classes = false

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.view_component.preview_paths << Rails.root.join("test/components/previews")
config.view_component.default_preview_layout = "component_preview"

Expand Down
29 changes: 25 additions & 4 deletions lib/view_component/form/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@
module ViewComponent
module Form
module TestHelpers
def form_with(object, options = {})
ViewComponent::Form::Builder.new(object_name, object, template, options)
include ActionView::ModelNaming

# See: https://github.com/rails/rails/blob/83217025a171593547d1268651b446d3533e2019/actionview/lib/action_view/helpers/form_helper.rb#L737
def form_with(model: nil, scope: nil, **options)
if model
model = model.last if model.is_a?(Array)
scope ||= model_name_from_record_or_class(model).param_key
end

instanciate_form_builder(scope, model, options)
end

def object_name
:user
# See: https://github.com/rails/rails/blob/83217025a171593547d1268651b446d3533e2019/actionview/lib/action_view/helpers/form_helper.rb#L1561
def instanciate_form_builder(record_name, record_object = nil, **options)
case record_name
when String, Symbol
object = record_object
object_name = record_name
else
object = record_name
object_name = model_name_from_record_or_class(object).param_key if object
end

builder = options[:builder] || ViewComponent::Form::Builder
builder.new(object_name, object, template, options)
end

private

def template
lookup_context = ActionView::LookupContext.new(ActionController::Base.view_paths)

Expand Down
2 changes: 2 additions & 0 deletions spec/internal/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ActiveRecord::Base
end
2 changes: 1 addition & 1 deletion spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Rails.application.routes.draw do
# Add your own routes here, or remove this file if you don't have need for it.

mount Lookbook::Engine, at: "/" if Rails.env.development?
mount Lookbook::Engine, at: "/lookbook" if Rails.env.development? # TODO: path to "/" but broke cable path
end
5 changes: 5 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
ActiveRecord::Schema.define do
# Set up any tables you need to exist for your test suite that don't belong
# in migrations.
create_table(:users, force: true) do |t|
t.string :first_name
t.string :last_name
t.timestamps
end
end
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
# frozen_string_literal: true

require "view_component/form/test_helpers"

module ViewComponent
module Form
class LabelComponentPreview < ViewComponent::Preview
def test
render ViewComponent::Form::LabelComponent.new(form_builder, object_name, :first_name)
include ViewComponent::Form::TestHelpers

def default
# TODO: maybe stop passing object_name to components since it is accessing from form_builder (delegate in BaseComponent)
render ViewComponent::Form::LabelComponent.new(form_builder, form_builder.object_name, :first_name)
end

private

def form_builder
@form_builder ||= ViewComponent::Form::Builder.new(object_name, object, template, {})
end

def object
OpenStruct.new
end

def object_name
:user
end

def template
lookup_context = ActionView::LookupContext.new(ActionController::Base.view_paths)

if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("6.1")
ActionView::Base.new(lookup_context, {}, ActionController::Base.new)
else
ActionView::Base.new(lookup_context, {})
end
instanciate_form_builder(User.new)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "view_component/form/test_helpers"

module ViewComponent
module Form
class SubmitComponentPreview < ViewComponent::Preview
include ViewComponent::Form::TestHelpers

def default
# TODO: maybe stop passing object_name to components since it is accessing from form_builder (delegate in BaseComponent)
render ViewComponent::Form::SubmitComponent.new(form_builder, form_builder.object_name)
end

private

def form_builder
instanciate_form_builder(User.new)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "view_component/form/test_helpers"

module ViewComponent
module Form
class TextAreaComponentPreview < ViewComponent::Preview
include ViewComponent::Form::TestHelpers

def default
@object = User.new

# TODO: maybe stop passing object_name to components since it is accessing from form_builder (delegate in BaseComponent)
render_f ViewComponent::Form::TextAreaComponent, :first_name
end

def with_value
@object = User.new(first_name: "John\John")

render_f ViewComponent::Form::TextAreaComponent, :first_name
end


private

def render_f(component_klass, *args, **options)
options[:object] ||= @object

builder = instanciate_form_builder(options[:object])

render component_klass.new(builder, builder.object_name, *args, options)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "view_component/form/test_helpers"

module ViewComponent
module Form
class TextFieldComponentPreview < ViewComponent::Preview
include ViewComponent::Form::TestHelpers

def default
@object = User.new

# TODO: maybe stop passing object_name to components since it is accessing from form_builder (delegate in BaseComponent)
render_f ViewComponent::Form::TextFieldComponent, :first_name
end

def with_value
@object = User.new(first_name: "John")

render_f ViewComponent::Form::TextFieldComponent, :first_name
end


private

def render_f(component_klass, *args, **options)
options[:object] ||= @object

builder = instanciate_form_builder(options[:object])

render component_klass.new(builder, builder.object_name, *args, options)
end
end
end
end

0 comments on commit 0336865

Please sign in to comment.