Skip to content

Commit f7544d3

Browse files
committed
refactor(controller_lifecycle) simplify per-request renderer API; fix tests for all Rails version
1 parent 05f6a3c commit f7544d3

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

lib/react/rails/component_mount.rb

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,12 @@ def react_component(name, props = {}, options = {}, &block)
4949
content_tag(html_tag, '', html_options, &block)
5050
end
5151

52-
module ControllerHelpers
53-
extend ActiveSupport::Concern
54-
55-
included do
56-
# An instance of a server renderer, for use during this request
57-
attr_accessor :__prerenderer
58-
end
59-
60-
# If you want a per-request renderer, add this method as an around-action
61-
#
62-
# @example Having one renderer instance for each controller action
63-
# around_action :per_request_react_prerenderer
64-
def per_request_react_prerenderer
65-
React::ServerRendering.with_renderer do |renderer|
66-
self.__prerenderer = renderer
67-
yield
68-
end
69-
end
70-
end
71-
7252
private
7353

54+
# If this controller has checked out a renderer, use that one.
55+
# Otherwise, use {React::ServerRendering} directly (which will check one out for this rendering).
7456
def prerender_component(component_name, props, prerender_options)
75-
renderer = @controller.try(:__prerenderer) || React::ServerRendering
57+
renderer = @controller.try(:react_rails_prerenderer) || React::ServerRendering
7658
renderer.render(component_name, props, prerender_options)
7759
end
7860
end

lib/react/rails/controller_lifecycle.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ module ControllerLifecycle
1212
attr_reader :__react_component_helper
1313
end
1414

15+
module ClassMethods
16+
# Call this in the controller to check out a prerender for the whole request.
17+
# You can access the renderer with {#react_rails_prerenderer}.
18+
def per_request_react_rails_prerenderer
19+
around_action_with_fallback = respond_to?(:around_action) ? :around_action : :around_filter
20+
public_send(around_action_with_fallback, :per_request_react_rails_prerenderer)
21+
end
22+
end
23+
1524
# Instantiate the ViewHelper implementation and call its #setup method
1625
# then let the controller action run,
1726
# then call the ViewHelper implementation's #teardown method
@@ -22,6 +31,24 @@ def use_react_component_helper
2231
yield
2332
@__react_component_helper.teardown(self)
2433
end
34+
35+
# If you want a per-request renderer, add this method as an around-action
36+
#
37+
# (`.per_request_react_rails_prerenderer` does this for you)
38+
# @example Having one renderer instance for each controller action
39+
# around_action :per_request_react_rails_prerenderer
40+
def per_request_react_rails_prerenderer
41+
React::ServerRendering.with_renderer do |renderer|
42+
@__react_rails_prerenderer = renderer
43+
yield
44+
end
45+
end
46+
47+
48+
# An instance of a server renderer, for use during this request
49+
def react_rails_prerenderer
50+
@__react_rails_prerenderer
51+
end
2552
end
2653
end
2754
end

lib/react/rails/railtie.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Railtie < ::Rails::Railtie
3838

3939
ActiveSupport.on_load(:action_controller) do
4040
include ::React::Rails::ControllerLifecycle
41-
include ::React::Rails::ComponentMount::ControllerHelpers
4241
end
4342

4443
ActiveSupport.on_load(:action_view) do

test/dummy/app/controllers/pages_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class PagesController < ApplicationController
2-
around_action :per_request_react_prerenderer
2+
per_request_react_rails_prerenderer
33

44
def show
55
@prerender = !!params[:prerender]
66
if @prerender
7-
js_context = __prerenderer.context
7+
js_context = react_rails_prerenderer.context
88
# This isn't safe for production, we're just testing the render context:
99
greeting_override = params[:greeting] || ""
1010
setup_code = "global.ctx = {}; global.ctx.greeting = '#{greeting_override}';"

test/react/rails/component_mount_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ def self.render(component_name, props, prerender_options)
9898
end
9999

100100
module DummyController
101-
def self.__prerenderer
101+
def self.react_rails_prerenderer
102102
DummyRenderer
103103
end
104104
end
105105

106-
test "it uses the controller's __prerenderer, if available" do
106+
test "it uses the controller's react_rails_prerenderer, if available" do
107107
@helper.setup(DummyController)
108108
rendered_component = @helper.react_component('Foo', {"ok" => true}, prerender: :static)
109109
assert_equal %|<div>rendered Foo with {&quot;ok&quot;:true}</div>|, rendered_component

0 commit comments

Comments
 (0)