Skip to content

Add layout attribute POC #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ jobs:
- ruby_version: "head"
rails_version: "main"
mode: "capture_patch_enabled"
- ruby_version: "head"
rails_version: "main"
mode: "capture_patch_enabled"
layout_patch: "true"

env:
BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails_version }}.gemfile
steps:
Expand All @@ -92,6 +97,7 @@ jobs:
RAILS_VERSION: ${{ matrix.rails_version }}
RUBY_VERSION: ${{ matrix.ruby_version }}
CAPTURE_PATCH_ENABLED: ${{ matrix.mode == 'capture_patch_enabled' && 'true' || 'false' }}
RENDER_LAYOUT_PATCH_ENABLED: ${{ matrix.layout_patch == 'true' || 'false' }}
- name: Upload coverage results
uses: actions/[email protected]
if: always()
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
lear ---
layout: default
title: Changelog
nav_order: 5
Expand Down
7 changes: 7 additions & 0 deletions lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def defaults
instrumentation_enabled: false,
use_deprecated_instrumentation_name: true,
render_monkey_patch_enabled: true,
render_layout_monkey_patch_enabled: false,
view_component_path: "app/components",
component_parent_class: nil,
show_previews: Rails.env.development? || Rails.env.test?,
Expand Down Expand Up @@ -132,6 +133,12 @@ def defaults
# `#render_component_to_string` instead.
# Defaults to `true`.

# @!attribute render_layout_monkey_patch_enabled
# @return [Boolean] Whether the #render method should be monkey patched.
# If this is disabled, use `#render_component` or
# `#render_component_to_string` instead.
# Defaults to `true`.

# @!attribute view_component_path
# @return [String]
# The path in which components, their templates, and their sidecars should
Expand Down
11 changes: 11 additions & 0 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Engine < Rails::Engine # :nodoc:
end
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
options.render_monkey_patch_enabled = true if options.render_monkey_patch_enabled.nil?
options.render_layout_monkey_patch_enabled = false if options.render_layout_monkey_patch_enabled.nil?
options.show_previews = (Rails.env.development? || Rails.env.test?) if options.show_previews.nil?

if options.show_previews
Expand Down Expand Up @@ -111,6 +112,16 @@ class Engine < Rails::Engine # :nodoc:
# :nocov:
end

initializer "view_component.monkey_patch_render_layout" do |app|
next if Rails.version.to_f <= 6.1 || !app.config.view_component.render_layout_monkey_patch_enabled

ActiveSupport.on_load(:action_view) do
require "view_component/render_layout_monkey_patch.rb"
ActionView::Base.prepend ViewComponent::RenderLayoutMonkeyPatch
end
# :nocov:
end

initializer "view_component.include_render_component" do |_app|
next if Rails.version.to_f >= 6.1

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

module ViewComponent
module RenderLayoutMonkeyPatch # :nodoc:
def render(options = {}, locals = {}, &block)
if locals[:layout].blank? || locals[:layout].is_a?(Symbol) || locals[:layout].is_a?(String)
super(options, locals, &block)
else
render(locals[:layout]) do
options.render_in(self, &block)
end
end
end
end
end





3 changes: 3 additions & 0 deletions test/sandbox/app/components/layout_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="layout__container">
<%= content %>
</div>
5 changes: 5 additions & 0 deletions test/sandbox/app/components/layout_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class LayoutComponent < ViewComponent::Base

end
1 change: 1 addition & 0 deletions test/sandbox/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
config.view_component.render_monkey_patch_enabled = true
config.view_component.render_layout_monkey_patch_enabled = ENV["RENDER_LAYOUT_PATCH_ENABLED"] == "true"
config.view_component.show_previews_source = true
config.view_component.test_controller = "IntegrationExamplesController"
config.view_component.capture_compatibility_patch_enabled = ENV["CAPTURE_PATCH_ENABLED"] == "true"
Expand Down
9 changes: 9 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,4 +1255,13 @@ def test_render_anonymous_component_without_template
render_inline(mock_component.new)
end
end

def test_layout_attribute
if Rails.version.to_f >= 6.1 && ViewComponent::Base.config.render_layout_monkey_patch_enabled
render_inline(MyComponent.new, layout: LayoutComponent.new)

assert_selector("div", text: "hello,world!")
assert_selector(".layout__container")
end
end
end
Loading