Skip to content

Commit 0c77042

Browse files
committed
feat(ViewHelper) add lifecycle hooks
1 parent 2a86210 commit 0c77042

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

lib/react/rails/component_mount.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
module React
22
module Rails
3+
# This is the default view helper implementation.
4+
# It just inserts HTML into the DOM (see {#react_component}).
5+
#
6+
# You can extend this class or provide your own implementation
7+
# by assigning it to `config.react.view_helper_implementation`.
38
class ComponentMount
49
include ActionView::Helpers::TagHelper
510
include ActionView::Helpers::TextHelper
611
attr_accessor :output_buffer
712

13+
# RenderMiddleware calls these hooks
14+
# You can use them in custom helper implementations
15+
def setup(env)
16+
end
17+
18+
def teardown(env)
19+
end
20+
821
# Render a UJS-type HTML tag annotated with data attributes, which
922
# are used by react_ujs to actually instantiate the React component
1023
# on the client.

lib/react/rails/controller_renderer.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class React::Rails::ControllerRenderer
55

66
attr_accessor :output_buffer
77

8-
def self.call(*args, &block)
9-
new.call(*args, &block)
8+
attr_reader :request
9+
def initialize(options={})
10+
@request = options[:request]
1011
end
1112

1213
def call(name, options, &block)

lib/react/rails/railtie.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class Railtie < ::Rails::Railtie
3838

3939
initializer "react_rails.add_component_renderer", group: :all do |app|
4040
ActionController::Renderers.add :component do |component_name, options|
41-
html = ::React::Rails::ControllerRenderer.call(component_name, options)
41+
renderer = ::React::Rails::ControllerRenderer.new(request: request)
42+
html = renderer.call(component_name, options)
4243
render_options = options.merge(inline: html)
4344
render(render_options)
4445
end

lib/react/rails/render_middleware.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ def initialize(app)
88

99
def call(env)
1010
new_helper = React::Rails::ViewHelper.helper_implementation_class.new
11+
new_helper.setup(env)
1112
env[HELPER_IMPLEMENTATION_KEY] = new_helper
12-
@app.call(env)
13+
response = @app.call(env)
14+
new_helper.teardown(env)
15+
response
1316
end
1417
end
1518
end

lib/react/rails/view_helper.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module React
22
module Rails
33
module ViewHelper
44
# This class will be used for inserting tags into HTML.
5-
# It should implement react_component(name, props, options &block)
5+
# It should implement:
6+
# - #setup(env)
7+
# - #teardown(env)
8+
# - #react_component(name, props, options &block)
69
# The default is {React::Rails::ComponentMount}
710
mattr_accessor :helper_implementation_class
811

test/react/rails/render_middleware_test.rb

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
# This helper implementation just counts the number of
44
# calls to `react_component`
55
class DummyHelperImplementation
6-
attr_reader :counter
6+
attr_reader :events
77
def initialize
8-
@counter = 0
8+
@events = []
9+
end
10+
11+
def setup(env)
12+
@events << :setup
13+
end
14+
15+
def teardown(env)
16+
@events << :teardown
917
end
1018

1119
def react_component(*args)
12-
@counter += 1
20+
@events << :react_component
1321
end
1422
end
1523

@@ -29,7 +37,13 @@ def teardown
2937
get '/pages/1'
3038
helper_obj = request.env[impl_key]
3139
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")
40+
end
41+
42+
test "it calls setup and teardown methods" do
43+
get '/pages/1'
44+
helper_obj = request.env[impl_key]
45+
lifecycle_steps = [:setup, :react_component, :teardown]
46+
assert_equal(lifecycle_steps, helper_obj.events)
3347
end
3448

3549
test "there's a new helper object for every request" do

0 commit comments

Comments
 (0)