aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test
diff options
context:
space:
mode:
authorJoel Hawksley <joel@hawksley.org>2019-05-29 13:03:54 -0600
committerJoel Hawksley <joel@hawksley.org>2019-06-12 16:31:01 -0600
commitc221b5b448569771678279216360460e066095a7 (patch)
tree6941280b09123cc0eddc1da12454e3d3e07cefea /actionview/test
parent60af9db3745f994e8a4bd7afe9dfa6ea8be7bb7c (diff)
downloadrails-c221b5b448569771678279216360460e066095a7.tar.gz
rails-c221b5b448569771678279216360460e066095a7.tar.bz2
rails-c221b5b448569771678279216360460e066095a7.zip
`RenderingHelper` supports rendering objects that `respond_to?` `:render_in`
Co-authored-by: Natasha Umer <natashau@github.com> Co-authored-by: Aaron Patterson <tenderlove@github.com> Co-authored-by: Shawn Allen <shawnbot@github.com> Co-authored-by: Emily Plummer <emplums@github.com> Co-authored-by: Diana Mounter <broccolini@github.com> Co-authored-by: John Hawthorn <jhawthorn@github.com> Co-authored-by: Nathan Herald <myobie@github.com> Co-authored-by: Zaid Zawaideh <zawaideh@github.com> Co-authored-by: Zach Ahn <engineering@zachahn.com>
Diffstat (limited to 'actionview/test')
-rw-r--r--actionview/test/lib/test_component.rb46
-rw-r--r--actionview/test/template/render_test.rb17
2 files changed, 63 insertions, 0 deletions
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'
+ <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
diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb
index 2235a7816f..c82264a170 100644
--- a/actionview/test/template/render_test.rb
+++ b/actionview/test/template/render_test.rb
@@ -2,6 +2,8 @@
require "abstract_unit"
require "controller/fake_models"
+require "test_component"
+require "active_model/validations"
class TestController < ActionController::Base
end
@@ -670,6 +672,21 @@ module RenderTestCases
def test_render_throws_exception_when_no_extensions_passed_to_register_template_handler_function_call
assert_raises(ArgumentError) { ActionView::Template.register_template_handler CustomHandler }
end
+
+ def test_render_component
+ assert_equal(
+ %(<span title="my title">Hello, World! (Inline render)</span>),
+ @view.render(TestComponent.new(title: "my title")) { "Hello, World!" }.strip
+ )
+ end
+
+ def test_render_component_with_validation_error
+ error = assert_raises(ActiveModel::ValidationError) do
+ @view.render(TestComponent.new(title: "my title")).strip
+ end
+
+ assert_match "Content can't be blank", error.message
+ end
end
class CachedViewRenderTest < ActiveSupport::TestCase