From fcb028ba1628bc14a6acbd0198bd142b29fc93f1 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:34:45 +0100 Subject: [PATCH] Refactor guidance links into a helper (#2551) * Refactor guidance links into a helper * Refactor discounted ownership guidance --- app/helpers/guidance_helper.rb | 11 ++++++++++ ...calculations_discounted_ownership.html.erb | 14 +++++-------- ...ial_calculations_shared_ownership.html.erb | 16 +++++++++----- spec/helpers/guidance_helper_spec.rb | 21 +++++++++++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 app/helpers/guidance_helper.rb create mode 100644 spec/helpers/guidance_helper_spec.rb diff --git a/app/helpers/guidance_helper.rb b/app/helpers/guidance_helper.rb new file mode 100644 index 000000000..00d699cd2 --- /dev/null +++ b/app/helpers/guidance_helper.rb @@ -0,0 +1,11 @@ +module GuidanceHelper + include GovukLinkHelper + include GovukVisuallyHiddenHelper + + def question_link(question_id, log, user) + question = log.form.get_question(question_id, log) + return "" unless question.page.routed_to?(log, user) + + "(#{govuk_link_to "Q#{question.question_number}", send("#{log.class.name.underscore}_#{question.page.id}_path", log)})".html_safe + end +end diff --git a/app/views/form/guidance/_financial_calculations_discounted_ownership.html.erb b/app/views/form/guidance/_financial_calculations_discounted_ownership.html.erb index 37a646983..39d3ec4d4 100644 --- a/app/views/form/guidance/_financial_calculations_discounted_ownership.html.erb +++ b/app/views/form/guidance/_financial_calculations_discounted_ownership.html.erb @@ -1,14 +1,10 @@ -<% grant_page = log.form.get_question("grant", log).page %> -<% mortgage_page = log.form.get_question("mortgage", log).page %> -<% discount_page = log.form.get_question("discount", log).page %> -<% deposit_page = log.form.get_question("deposit", log).page %> <%= govuk_details(summary_text: "How the financial values are calculated") do %>

- The mortgage amount <%= "(#{govuk_link_to 'Q105', send("#{log.class.name.underscore}_#{mortgage_page.id}_path", log)})".html_safe if mortgage_page.routed_to?(log, current_user) %> - cash deposit <%= "(#{govuk_link_to 'Q109', send("#{log.class.name.underscore}_#{deposit_page.id}_path", log)})".html_safe if deposit_page.routed_to?(log, current_user) %> - and grant <%= "(#{govuk_link_to 'Q102', send("#{log.class.name.underscore}_#{grant_page.id}_path", log)})".html_safe if grant_page.routed_to?(log, current_user) %> + The mortgage amount <%= question_link("mortgage", log, current_user) %> + cash deposit <%= question_link("deposit", log, current_user) %> + and grant <%= question_link("grant", log, current_user) %> added together must equal - the purchase price (<%= govuk_link_to "Q101", send("#{log.class.name.underscore}_#{log.form.get_question('value', log).page.id}_path", log) %>) - multiplied by the discount stake <%= "(#{govuk_link_to '103', send("#{log.class.name.underscore}_#{discount_page.id}_path", log)})".html_safe if discount_page.routed_to?(log, current_user) %> + the purchase price <%= question_link("value", log, current_user) %> + multiplied by the discount stake <%= question_link("discount", log, current_user) %>

<% end %> diff --git a/app/views/form/guidance/_financial_calculations_shared_ownership.html.erb b/app/views/form/guidance/_financial_calculations_shared_ownership.html.erb index 1164a3461..1fbc693d7 100644 --- a/app/views/form/guidance/_financial_calculations_shared_ownership.html.erb +++ b/app/views/form/guidance/_financial_calculations_shared_ownership.html.erb @@ -1,10 +1,16 @@ <%= govuk_details(summary_text: "How the financial values are calculated") do %>

- The mortgage amount (<%= govuk_link_to "Q92", send("#{log.class.name.underscore}_#{log.form.get_question('mortgage', log).page.id}_path", log) %>), - cash deposit (<%= govuk_link_to "Q95", send("#{log.class.name.underscore}_#{log.form.get_question('deposit', log).page.id}_path", log) %>) - and cash discount (<%= govuk_link_to "Q97", send("#{log.class.name.underscore}_#{log.form.get_question('cashdis', log).page.id}_path", log) %>) + The mortgage amount <%= question_link("mortgage", log, current_user) %>, + cash deposit <%= question_link("deposit", log, current_user) %>, + and cash discount <%= question_link("cashdis", log, current_user) %> added together must equal - the purchase price (<%= govuk_link_to "Q88", send("#{log.class.name.underscore}_#{log.form.get_question('value', log).page.id}_path", log) %>) - multiplied by the percentage equity stake (<%= govuk_link_to "Q89", send("#{log.class.name.underscore}_#{log.form.get_question('equity', log).page.id}_path", log) %>) + the purchase price <%= question_link("value", log, current_user) %> + <% stairbought_page = log.form.get_question("stairbought", log).page %> + <% equity_page = log.form.get_question("equity", log).page %> + <% if stairbought_page.routed_to?(log, current_user) %> + multiplied by the percentage bought <%= question_link("stairbought", log, current_user) %> + <% else %> + multiplied by the percentage equity stake <%= question_link("equity", log, current_user) %> + <% end %>

<% end %> diff --git a/spec/helpers/guidance_helper_spec.rb b/spec/helpers/guidance_helper_spec.rb new file mode 100644 index 000000000..c1b900fac --- /dev/null +++ b/spec/helpers/guidance_helper_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe GuidanceHelper do + describe "#question_link" do + context "when question page is routed to" do + let(:log) { create(:sales_log, :shared_ownership_setup_complete, mortgageused: 2) } + + it "returns an empty string if question is not routed to" do + expect(question_link("mortgage", log, log.assigned_to)).to eq("") + end + end + + context "when question page is not routed to" do + let(:log) { create(:sales_log, :shared_ownership_setup_complete, mortgageused: 1) } + + it "returns a link to the question with correct question number in brakets" do + expect(question_link("mortgage", log, log.assigned_to)).to eq("(Q92)") + end + end + end +end