File tree 5 files changed +63
-2
lines changed
5 files changed +63
-2
lines changed Original file line number Diff line number Diff line change 1
1
require 'react/rails/asset_variant'
2
2
require 'react/rails/engine'
3
3
require 'react/rails/railtie'
4
+ require 'react/rails/render_middleware'
4
5
require 'react/rails/version'
5
6
require 'react/rails/component_mount'
6
7
require 'react/rails/view_helper'
Original file line number Diff line number Diff line change @@ -4,7 +4,6 @@ module React
4
4
module Rails
5
5
class Railtie < ::Rails ::Railtie
6
6
config . react = ActiveSupport ::OrderedOptions . new
7
-
8
7
# Sensible defaults. Can be overridden in application.rb
9
8
config . react . variant = ( ::Rails . env . production? ? :production : :development )
10
9
config . react . addons = false
@@ -25,6 +24,7 @@ class Railtie < ::Rails::Railtie
25
24
26
25
# Include the react-rails view helper lazily
27
26
initializer "react_rails.setup_view_helpers" , group : :all do |app |
27
+ app . config . middleware . use ( ::React ::Rails ::RenderMiddleware )
28
28
app . config . react . jsx_transformer_class ||= React ::JSX ::DEFAULT_TRANSFORMER
29
29
React ::JSX . transformer_class = app . config . react . jsx_transformer_class
30
30
React ::JSX . transform_options = app . config . react . jsx_transform_options
Original file line number Diff line number Diff line change
1
+ module React
2
+ module Rails
3
+ class RenderMiddleware
4
+ HELPER_IMPLEMENTATION_KEY = "react_rails.view_helper_implementation"
5
+ def initialize ( app )
6
+ @app = app
7
+ end
8
+
9
+ def call ( env )
10
+ new_helper = React ::Rails ::ViewHelper . helper_implementation_class . new
11
+ env [ HELPER_IMPLEMENTATION_KEY ] = new_helper
12
+ @app . call ( env )
13
+ end
14
+ end
15
+ end
16
+ end
Original file line number Diff line number Diff line change @@ -7,7 +7,9 @@ module ViewHelper
7
7
mattr_accessor :helper_implementation_class
8
8
9
9
def react_component ( *args , &block )
10
- self . helper_implementation_class . new . react_component ( *args , &block )
10
+ impl_key = React ::Rails ::RenderMiddleware ::HELPER_IMPLEMENTATION_KEY
11
+ helper_obj = request . env [ impl_key ]
12
+ helper_obj . react_component ( *args , &block )
11
13
end
12
14
end
13
15
end
Original file line number Diff line number Diff line change
1
+ require 'test_helper'
2
+
3
+ # This helper implementation just counts the number of
4
+ # calls to `react_component`
5
+ class DummyHelperImplementation
6
+ attr_reader :counter
7
+ def initialize
8
+ @counter = 0
9
+ end
10
+
11
+ def react_component ( *args )
12
+ @counter += 1
13
+ end
14
+ end
15
+
16
+ class RenderMiddlewareTest < ActionDispatch ::IntegrationTest
17
+ impl_key = React ::Rails ::RenderMiddleware ::HELPER_IMPLEMENTATION_KEY
18
+
19
+ def setup
20
+ @previous_helper_implementation = React ::Rails ::ViewHelper . helper_implementation_class
21
+ React ::Rails ::ViewHelper . helper_implementation_class = DummyHelperImplementation
22
+ end
23
+
24
+ def teardown
25
+ React ::Rails ::ViewHelper . helper_implementation_class = @previous_helper_implementation
26
+ end
27
+
28
+ test "it creates a helper object and puts it in the request env" do
29
+ get '/pages/1'
30
+ helper_obj = request . env [ impl_key ]
31
+ assert ( helper_obj . is_a? ( DummyHelperImplementation ) , "It uses the view helper implementation class" )
32
+ assert_equal ( 1 , helper_obj . counter , "It uses that object during rendering" )
33
+ end
34
+
35
+ test "there's a new helper object for every request" do
36
+ get '/pages/1'
37
+ first_helper = request . env [ impl_key ]
38
+ get '/pages/1'
39
+ second_helper = request . env [ impl_key ]
40
+ assert ( first_helper != second_helper , "The helper for the second request is brand new" )
41
+ end
42
+ end
You can’t perform that action at this time.
0 commit comments