From c221b5b448569771678279216360460e066095a7 Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Wed, 29 May 2019 13:03:54 -0600 Subject: `RenderingHelper` supports rendering objects that `respond_to?` `:render_in` Co-authored-by: Natasha Umer Co-authored-by: Aaron Patterson Co-authored-by: Shawn Allen Co-authored-by: Emily Plummer Co-authored-by: Diana Mounter Co-authored-by: John Hawthorn Co-authored-by: Nathan Herald Co-authored-by: Zaid Zawaideh Co-authored-by: Zach Ahn --- actionview/test/lib/test_component.rb | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 actionview/test/lib/test_component.rb (limited to 'actionview/test/lib') diff --git a/actionview/test/lib/test_component.rb b/actionview/test/lib/test_component.rb new file mode 100644 index 0000000000..493b9487b1 --- /dev/null +++ b/actionview/test/lib/test_component.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +class TestComponent < ActionView::Base + include ActiveModel::Validations + + validates :content, :title, presence: true + 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? + validate! + rendered_template + end + + def self.template + <<~'erb' + <%= content %> (<%= render(plain: "Inline render") %>) + 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 -- cgit v1.2.3