Skip to content

Commit 7e020af

Browse files
committed
Memo-ize FlowPresenter#start_node
`FlowPresenter#start_node` is called several times on the same instance within a single request. This meant that we were unnecessarily creating several instances of `StartNodePresenter`. An instance of `SmartAnswer::ErbRenderer` is created for each of these and an instance of `ActionView::Base` is created for each of those. We can reuse the start node presenter by memo-izing it. It's safe to do this, because a new `FlowPresenter` instance is created in every request within the `SmartAnswersController#find_smart_answer` method.
1 parent 9844a50 commit 7e020af

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

app/presenters/flow_presenter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def current_node
9595

9696
def start_node
9797
node = SmartAnswer::Node.new(@flow, @flow.name.underscore.to_sym)
98-
StartNodePresenter.new(node)
98+
@start_node ||= StartNodePresenter.new(node)
9999
end
100100

101101
def change_collapsed_question_link(question_number)

test/unit/flow_presenter_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ class FlowPresenterTest < ActiveSupport::TestCase
8181
start_node = @flow_presenter.start_node
8282
assert_instance_of StartNodePresenter, start_node
8383
end
84+
85+
test '#start_node always returns same presenter for landing page node' do
86+
start_node_1 = @flow_presenter.start_node
87+
start_node_2 = @flow_presenter.start_node
88+
assert_same start_node_1, start_node_2
89+
end
8490
end

0 commit comments

Comments
 (0)