Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37919 from joelhawksley/controller-render-in
Render objects that respond_to render_in in controllers
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
class TestComponent < ActionView::Base | ||
include ActiveModel::Validations | ||
|
||
validates :title, presence: true | ||
delegate :render, to: :view_context | ||
|
||
def initialize(title:) | ||
@title = title | ||
end | ||
|
||
def render_in(view_context) | ||
self.class.compile | ||
@view_context = view_context | ||
validate! | ||
rendered_template | ||
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