Skip to main content
Warning This document has not been updated for a while now. It may be out of date.
Last updated: 14 Jun 2021

smart-answers: Refactoring existing Smart Answers

Some Smart Answer flows contain a mix of concerns: routing logic, policy/calculator logic and presentation logic.

These concerns should really be split. Routing belongs in the flow, policy/calculator logic belongs in the calculator and presentation belongs in the ERB templates. You might think of the flow as the Controller, the calculator as the Model and the ERB template as the View in an MVC architecture.

We've refactored a number of these Smart Answers and have a rough set of steps that we follow:

value_question :first_question? do
  on_response do |response|
    self.calculator = ExampleCalculator.new
    calculator.first_response = response
  end
end
  • Save the responses to questions on the calculator object using an on_response block.
value_question :subsequent_question? do
  on_response do |response|
    calculator.subsequent_response = response
  end
end

Pull request 2068 is a great example of incrementally applying the steps above to refactor the calculate-statutory-sick-pay Smart Answer. Pull requests 2095 and 1856 apply a similar refactoring to check-uk-visa and minimum-wage respectively.