mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
05a2a939f0
We're moving away from using validations in our component framework, and feel that it's better to avoid prescribing their usage in these example classes, which exist to serve as example objects that are compatible with the render_in API.
41 lines
965 B
Ruby
41 lines
965 B
Ruby
# 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
|
|
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
|