Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document view components support (#38656)
- Loading branch information
1 parent
99b607e
commit c82a919
Showing
7 changed files
with
32 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class TestComponent < ActionView::Base | ||
delegate :render, to: :view_context | ||
|
||
def initialize(title:) | ||
@title = title | ||
end | ||
|
||
def render_in(view_context) | ||
self.class.compile | ||
@view_context = view_context | ||
rendered_template | ||
class TestComponent | ||
def render_in(_view_context) | ||
"Hello, World!" | ||
end | ||
|
||
def format | ||
:html | ||
end | ||
|
||
def self.template | ||
<<~'erb' | ||
<span title="<%= title %>">(<%= render(plain: "Inline render") %>)</span> | ||
erb | ||
end | ||
|
||
def self.compile | ||
@compiled ||= nil | ||
return if @compiled | ||
|
||
class_eval( | ||
"def rendered_template; @output_buffer = ActionView::OutputBuffer.new; " + | ||
ActionView::Template::Handlers::ERB.erb_implementation.new(template, trim: true).src + | ||
"; end" | ||
) | ||
|
||
@compiled = true | ||
end | ||
|
||
private | ||
attr_reader :title, :view_context | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class TestComponent < ActionView::Base | ||
delegate :render, to: :view_context | ||
|
||
def initialize(title:) | ||
@title = title | ||
end | ||
|
||
# Entrypoint for rendering. Called by ActionView::RenderingHelper#render. | ||
# | ||
# Returns ActionView::OutputBuffer. | ||
def render_in(view_context, &block) | ||
self.class.compile | ||
@view_context = view_context | ||
@content = view_context.capture(&block) if block_given? | ||
rendered_template | ||
end | ||
|
||
def self.template | ||
<<~'erb' | ||
<span title="<%= title %>"><%= content %> (<%= render(plain: "Inline render") %>)</span> | ||
erb | ||
class TestComponent | ||
def render_in(_view_context) | ||
"Hello, World!" | ||
end | ||
|
||
def self.compile | ||
@compiled ||= nil | ||
return if @compiled | ||
|
||
class_eval( | ||
"def rendered_template; @output_buffer = ActionView::OutputBuffer.new; " + | ||
ActionView::Template::Handlers::ERB.erb_implementation.new(template, trim: true).src + | ||
"; end" | ||
) | ||
|
||
@compiled = true | ||
end | ||
|
||
private | ||
attr_reader :content, :title, :view_context | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters