Sha256: 2593f641a301a460e60b49ae8a820f9e38f7b7a6ab2149fedf07d99f5288003d

Contents?: true

Size: 1.52 KB

Versions: 48

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require "abstract_unit"

# This is testing the decoupling of view renderer and view context
# by allowing the controller to be used as view context. This is
# similar to the way sinatra renders templates.
module RenderContext
  class BasicController < ActionController::Base
    self.view_paths = [ActionView::FixtureResolver.new(
      "render_context/basic/hello_world.html.erb" => "<%= @value %> from <%= self.__controller_method__ %>",
      "layouts/basic.html.erb" => "?<%= yield %>?"
    )]

    # 1) Include ActionView::Context to bring the required dependencies
    include ActionView::Context

    # 2) Call _prepare_context that will do the required initialization
    before_action :_prepare_context

    def hello_world
      @value = "Hello"
      render action: "hello_world", layout: false
    end

    def with_layout
      @value = "Hello"
      render action: "hello_world", layout: "basic"
    end

    protected def __controller_method__
      "controller context!"
    end

    # 3) Set view_context to self
    private def view_context
      self
    end
  end

  class RenderContextTest < Rack::TestCase
    test "rendering using the controller as context" do
      get "/render_context/basic/hello_world"
      assert_body "Hello from controller context!"
      assert_status 200
    end

    test "rendering using the controller as context with layout" do
      get "/render_context/basic/with_layout"
      assert_body "?Hello from controller context!?"
      assert_status 200
    end
  end
end

Version data entries

48 entries across 48 versions & 2 rubygems

Version Path
jets-1.9.7 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.6 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.5 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.4 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.3 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.2 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.1 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.9.0 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.14 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.13 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.12 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.11 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.10 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.9 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.8 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.7 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.6 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.5 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.4 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb
jets-1.8.3 vendor/rails/actionpack/test/controller/new_base/render_context_test.rb