Browse Source

Use pre exhisting method to format values

pull/1525/head
Jack S 3 years ago
parent
commit
69419089ed
  1. 18
      app/helpers/interruption_screen_helper.rb
  2. 13
      app/models/form/lettings/pages/max_rent_value_check.rb
  3. 6
      app/models/form/lettings/pages/min_rent_value_check.rb
  4. 12
      app/models/form/lettings/pages/net_income_value_check.rb
  5. 1
      app/models/form/sales/pages/about_price_value_check.rb
  6. 16
      app/models/form/sales/pages/discounted_sale_value_check.rb
  7. 5
      app/models/form/sales/pages/shared_ownership_deposit_value_check.rb
  8. 5
      app/models/log.rb
  9. 5
      app/models/sales_log.rb
  10. 104
      spec/helpers/interruption_screen_helper_spec.rb
  11. 4
      spec/models/form/lettings/pages/max_rent_value_check_spec.rb
  12. 4
      spec/models/form/lettings/pages/min_rent_value_check_spec.rb
  13. 2
      spec/models/form/lettings/pages/net_income_value_check_spec.rb
  14. 2
      spec/models/form/sales/pages/about_price_value_check_spec.rb
  15. 4
      spec/models/form/sales/pages/discounted_sale_value_check_spec.rb
  16. 2
      spec/models/form/sales/pages/shared_ownership_deposit_value_check_spec.rb

18
app/helpers/interruption_screen_helper.rb

@ -1,6 +1,4 @@
module InterruptionScreenHelper module InterruptionScreenHelper
include MoneyFormattingHelper
def display_informative_text(informative_text, log) def display_informative_text(informative_text, log)
return "" unless informative_text["arguments"] return "" unless informative_text["arguments"]
@ -34,14 +32,12 @@ module InterruptionScreenHelper
private private
def get_value_from_argument(log, argument) def get_value_from_argument(log, argument)
value = if argument["label"] if argument["label"]
log.form.get_question(argument["key"], log).answer_label(log).downcase log.form.get_question(argument["key"], log).answer_label(log).downcase
elsif argument["arguments_for_key"] elsif argument["arguments_for_key"]
log.public_send(argument["key"], argument["arguments_for_key"]) log.public_send(argument["key"], argument["arguments_for_key"])
else else
log.public_send(argument["key"]) log.public_send(argument["key"])
end end
argument["money"] ? format_as_currency(value) : value
end end
end end

13
app/models/form/lettings/pages/max_rent_value_check.rb

@ -5,16 +5,21 @@ class Form::Lettings::Pages::MaxRentValueCheck < ::Form::Page
@depends_on = [{ "rent_in_soft_max_range?" => true }] @depends_on = [{ "rent_in_soft_max_range?" => true }]
@title_text = { @title_text = {
"translation" => "soft_validations.rent.outside_range_title", "translation" => "soft_validations.rent.outside_range_title",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "brent", "money" => true }], "arguments" => [
{
"key" => "brent",
"label" => true,
"i18n_template" => "brent",
},
],
} }
@informative_text = { @informative_text = {
"translation" => "soft_validations.rent.max_hint_text", "translation" => "soft_validations.rent.max_hint_text",
"arguments" => [ "arguments" => [
{ {
"key" => "soft_max_for_period", "key" => "field_formatted_as_currency",
"label" => false, "arguments_for_key" => "soft_max_for_period",
"i18n_template" => "soft_max_for_period", "i18n_template" => "soft_max_for_period",
"money" => true,
}, },
], ],
} }

6
app/models/form/lettings/pages/min_rent_value_check.rb

@ -9,17 +9,15 @@ class Form::Lettings::Pages::MinRentValueCheck < ::Form::Page
"key" => "brent", "key" => "brent",
"label" => true, "label" => true,
"i18n_template" => "brent", "i18n_template" => "brent",
"money" => true,
}], }],
} }
@informative_text = { @informative_text = {
"translation" => "soft_validations.rent.min_hint_text", "translation" => "soft_validations.rent.min_hint_text",
"arguments" => [ "arguments" => [
{ {
"key" => "soft_min_for_period", "key" => "field_formatted_as_currency",
"label" => false, "arguments_for_key" => "soft_min_for_period",
"i18n_template" => "soft_min_for_period", "i18n_template" => "soft_min_for_period",
"money" => true,
}, },
], ],
} }

12
app/models/form/lettings/pages/net_income_value_check.rb

@ -7,8 +7,16 @@ class Form::Lettings::Pages::NetIncomeValueCheck < ::Form::Page
@informative_text = { @informative_text = {
"translation" => "soft_validations.net_income.hint_text", "translation" => "soft_validations.net_income.hint_text",
"arguments" => [ "arguments" => [
{ "key" => "ecstat1", "label" => true, "i18n_template" => "ecstat1", "money" => true }, {
{ "key" => "earnings", "label" => true, "i18n_template" => "earnings", "money" => true }, "key" => "field_formatted_as_currency",
"arguments_for_key" => "ecstat1",
"i18n_template" => "ecstat1",
},
{
"key" => "field_formatted_as_currency",
"arguments_for_key" => "earnings",
"i18n_template" => "earnings",
},
], ],
} }
end end

1
app/models/form/sales/pages/about_price_value_check.rb

@ -13,7 +13,6 @@ class Form::Sales::Pages::AboutPriceValueCheck < ::Form::Page
"key" => "value", "key" => "value",
"label" => true, "label" => true,
"i18n_template" => "value", "i18n_template" => "value",
"money" => true,
}, },
], ],
} }

16
app/models/form/sales/pages/discounted_sale_value_check.rb

@ -4,11 +4,23 @@ class Form::Sales::Pages::DiscountedSaleValueCheck < ::Form::Page
@depends_on = depends_on @depends_on = depends_on
@title_text = { @title_text = {
"translation" => "soft_validations.discounted_sale_value.title_text", "translation" => "soft_validations.discounted_sale_value.title_text",
"arguments" => [{ "key" => "value_with_discount", "label" => false, "i18n_template" => "value_with_discount", "money" => true }], "arguments" => [
{
"key" => "field_formatted_as_currency",
"arguments_for_key" => "value_with_discount",
"i18n_template" => "value_with_discount",
},
],
} }
@informative_text = { @informative_text = {
"translation" => "soft_validations.discounted_sale_value.informative_text", "translation" => "soft_validations.discounted_sale_value.informative_text",
"arguments" => [{ "key" => "mortgage_deposit_and_grant_total", "label" => false, "i18n_template" => "mortgage_deposit_and_grant_total", "money" => true }], "arguments" => [
{
"key" => "field_formatted_as_currency",
"arguments_for_key" => "mortgage_deposit_and_grant_total",
"i18n_template" => "mortgage_deposit_and_grant_total",
},
],
} }
@person_index = person_index @person_index = person_index
@depends_on = [ @depends_on = [

5
app/models/form/sales/pages/shared_ownership_deposit_value_check.rb

@ -11,10 +11,9 @@ class Form::Sales::Pages::SharedOwnershipDepositValueCheck < ::Form::Page
"translation" => "soft_validations.shared_ownership_deposit.title_text", "translation" => "soft_validations.shared_ownership_deposit.title_text",
"arguments" => [ "arguments" => [
{ {
"key" => "expected_shared_ownership_deposit_value", "key" => "field_formatted_as_currency",
"label" => false, "arguments_for_key" => "expected_shared_ownership_deposit_value",
"i18n_template" => "expected_shared_ownership_deposit_value", "i18n_template" => "expected_shared_ownership_deposit_value",
"money" => true,
}, },
], ],
} }

5
app/models/log.rb

@ -126,6 +126,11 @@ class Log < ApplicationRecord
end end
end end
def field_formatted_as_currency(field_name)
field_value = public_send(field_name)
format_as_currency(field_value)
end
private private
def plural_gender_for_person(person_num) def plural_gender_for_person(person_num)

5
app/models/sales_log.rb

@ -323,11 +323,6 @@ class SalesLog < Log
format_as_currency(soft_min) format_as_currency(soft_min)
end end
def field_formatted_as_currency(field_name)
field_value = public_send(field_name)
format_as_currency(field_value)
end
def should_process_uprn_change? def should_process_uprn_change?
uprn_changed? && saledate && saledate.year >= 2023 uprn_changed? && saledate && saledate.year >= 2023
end end

104
spec/helpers/interruption_screen_helper_spec.rb

@ -143,15 +143,15 @@ RSpec.describe InterruptionScreenHelper do
informative_text_hash = { informative_text_hash = {
"arguments" => [ "arguments" => [
{ {
"key" => "retirement_age_for_person", "key" => "field_formatted_as_currency",
"arguments_for_key" => 1, "arguments_for_key" => "brent",
"i18n_template" => "argument", "i18n_template" => "argument",
}, },
], ],
} }
allow(lettings_log).to receive(:retirement_age_for_person) allow(lettings_log).to receive(:field_formatted_as_currency)
display_informative_text(informative_text_hash, lettings_log) display_informative_text(informative_text_hash, lettings_log)
expect(lettings_log).to have_received(:retirement_age_for_person).with(1) expect(lettings_log).to have_received(:field_formatted_as_currency).with("brent")
end end
it "returns the correct text" do it "returns the correct text" do
@ -160,42 +160,12 @@ RSpec.describe InterruptionScreenHelper do
"translation" => translation, "translation" => translation,
"arguments" => [ "arguments" => [
{ {
"key" => "retirement_age_for_person", "key" => "field_formatted_as_currency",
"arguments_for_key" => 1, "arguments_for_key" => "brent",
"i18n_template" => "argument", "i18n_template" => "argument",
}, },
], ],
} }
expect(display_informative_text(informative_text_hash, lettings_log)).to eq(I18n.t(translation, argument: lettings_log.retirement_age_for_person(1)))
end
end
context "when money not set" do
it "does not format the value" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument" }],
}
expect(display_informative_text(informative_text_hash, lettings_log)).to eq("You said this: 12345.0")
end
end
context "when money set to false" do
it "does not format the value" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument", money: false }],
}
expect(display_informative_text(informative_text_hash, lettings_log)).to eq("You said this: 12345.0")
end
end
context "when money set to true" do
it "formats the value as money with currency and 2 decimals" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument", "money" => true }],
}
expect(display_informative_text(informative_text_hash, lettings_log)).to eq("You said this: £12,345.00") expect(display_informative_text(informative_text_hash, lettings_log)).to eq("You said this: £12,345.00")
end end
end end
@ -225,6 +195,38 @@ RSpec.describe InterruptionScreenHelper do
expect(display_title_text(title_text, lettings_log)) expect(display_title_text(title_text, lettings_log))
.to eq(I18n.t("test.title_text.one_argument", argument: lettings_log.form.get_question("ecstat1", lettings_log).answer_label(lettings_log).downcase)) .to eq(I18n.t("test.title_text.one_argument", argument: lettings_log.form.get_question("ecstat1", lettings_log).answer_label(lettings_log).downcase))
end end
context "when and argument is given with a key and arguments for the key" do
it "makes the correct method call" do
title_text = {
"arguments" => [
{
"key" => "field_formatted_as_currency",
"arguments_for_key" => "brent",
"i18n_template" => "argument",
},
],
}
allow(lettings_log).to receive(:field_formatted_as_currency)
display_title_text(title_text, lettings_log)
expect(lettings_log).to have_received(:field_formatted_as_currency).with("brent")
end
it "returns the correct text" do
translation = "test.title_text.one_argument"
title_text = {
"translation" => translation,
"arguments" => [
{
"key" => "field_formatted_as_currency",
"arguments_for_key" => "brent",
"i18n_template" => "argument",
},
],
}
expect(display_title_text(title_text, lettings_log)).to eq("You said this: £12,345.00")
end
end
end end
context "when title text is not defined" do context "when title text is not defined" do
@ -238,35 +240,5 @@ RSpec.describe InterruptionScreenHelper do
expect(display_title_text("", lettings_log)).to eq("") expect(display_title_text("", lettings_log)).to eq("")
end end
end end
context "when money not set" do
it "does not format the value" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument" }],
}
expect(display_title_text(informative_text_hash, lettings_log)).to eq("You said this: 12345.0")
end
end
context "when money set to false" do
it "does not format the value" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument", money: false }],
}
expect(display_title_text(informative_text_hash, lettings_log)).to eq("You said this: 12345.0")
end
end
context "when money set to true" do
it "formats the value as money with currency and 2 decimals" do
informative_text_hash = {
"translation" => "test.title_text.one_argument",
"arguments" => [{ "key" => "brent", "label" => true, "i18n_template" => "argument", "money" => true }],
}
expect(display_title_text(informative_text_hash, lettings_log)).to eq("You said this: £12,345.00")
end
end
end end
end end

4
spec/models/form/lettings/pages/max_rent_value_check_spec.rb

@ -28,10 +28,10 @@ RSpec.describe Form::Lettings::Pages::MaxRentValueCheck, type: :model do
end end
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ "arguments" => [{ "i18n_template" => "brent", "key" => "brent", "label" => true, "money" => true }], "translation" => "soft_validations.rent.outside_range_title" }) expect(page.title_text).to eq({ "arguments" => [{ "i18n_template" => "brent", "key" => "brent", "label" => true }], "translation" => "soft_validations.rent.outside_range_title" })
end end
it "has the correct informative_text" do it "has the correct informative_text" do
expect(page.informative_text).to eq({ "arguments" => [{ "i18n_template" => "soft_max_for_period", "key" => "soft_max_for_period", "label" => false, "money" => true }], "translation" => "soft_validations.rent.max_hint_text" }) expect(page.informative_text).to eq({ "arguments" => [{ "arguments_for_key" => "soft_max_for_period", "i18n_template" => "soft_max_for_period", "key" => "field_formatted_as_currency" }], "translation" => "soft_validations.rent.max_hint_text" })
end end
end end

4
spec/models/form/lettings/pages/min_rent_value_check_spec.rb

@ -35,13 +35,13 @@ RSpec.describe Form::Lettings::Pages::MinRentValueCheck, type: :model do
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ expect(page.title_text).to eq({
"translation" => "soft_validations.rent.outside_range_title", "translation" => "soft_validations.rent.outside_range_title",
"arguments" => [{ "i18n_template" => "brent", "key" => "brent", "label" => true, "money" => true }], "arguments" => [{ "i18n_template" => "brent", "key" => "brent", "label" => true }],
}) })
end end
it "has the correct informative_text" do it "has the correct informative_text" do
expect(page.informative_text).to eq({ expect(page.informative_text).to eq({
"arguments" => [{ "i18n_template" => "soft_min_for_period", "key" => "soft_min_for_period", "label" => false, "money" => true }], "arguments" => [{ "arguments_for_key" => "soft_min_for_period", "i18n_template" => "soft_min_for_period", "key" => "field_formatted_as_currency" }],
"translation" => "soft_validations.rent.min_hint_text", "translation" => "soft_validations.rent.min_hint_text",
}) })
end end

2
spec/models/form/lettings/pages/net_income_value_check_spec.rb

@ -33,7 +33,7 @@ RSpec.describe Form::Lettings::Pages::NetIncomeValueCheck, type: :model do
it "has the correct informative_text" do it "has the correct informative_text" do
expect(page.informative_text).to eq({ expect(page.informative_text).to eq({
"arguments" => [{ "i18n_template" => "ecstat1", "key" => "ecstat1", "label" => true, "money" => true }, { "i18n_template" => "earnings", "key" => "earnings", "label" => true, "money" => true }], "arguments" => [{ "arguments_for_key" => "ecstat1", "i18n_template" => "ecstat1", "key" => "field_formatted_as_currency" }, { "arguments_for_key" => "earnings", "i18n_template" => "earnings", "key" => "field_formatted_as_currency" }],
"translation" => "soft_validations.net_income.hint_text", "translation" => "soft_validations.net_income.hint_text",
}) })
end end

2
spec/models/form/sales/pages/about_price_value_check_spec.rb

@ -32,7 +32,7 @@ RSpec.describe Form::Sales::Pages::AboutPriceValueCheck, type: :model do
end end
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ "arguments" => [{ "i18n_template" => "value", "key" => "value", "label" => true, "money" => true }], "translation" => "soft_validations.purchase_price.title_text" }) expect(page.title_text).to eq({ "arguments" => [{ "i18n_template" => "value", "key" => "value", "label" => true }], "translation" => "soft_validations.purchase_price.title_text" })
end end
it "has the correct informative_text" do it "has the correct informative_text" do

4
spec/models/form/sales/pages/discounted_sale_value_check_spec.rb

@ -27,14 +27,14 @@ RSpec.describe Form::Sales::Pages::DiscountedSaleValueCheck, type: :model do
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ expect(page.title_text).to eq({
"translation" => "soft_validations.discounted_sale_value.title_text", "translation" => "soft_validations.discounted_sale_value.title_text",
"arguments" => [{ "key" => "value_with_discount", "label" => false, "i18n_template" => "value_with_discount", "money" => true }], "arguments" => [{ "arguments_for_key" => "value_with_discount", "i18n_template" => "value_with_discount", "key" => "field_formatted_as_currency" }],
}) })
end end
it "has the correct informative_text" do it "has the correct informative_text" do
expect(page.informative_text).to eq({ expect(page.informative_text).to eq({
"translation" => "soft_validations.discounted_sale_value.informative_text", "translation" => "soft_validations.discounted_sale_value.informative_text",
"arguments" => [{ "key" => "mortgage_deposit_and_grant_total", "label" => false, "i18n_template" => "mortgage_deposit_and_grant_total", "money" => true }], "arguments" => [{ "arguments_for_key" => "mortgage_deposit_and_grant_total", "i18n_template" => "mortgage_deposit_and_grant_total", "key" => "field_formatted_as_currency" }],
}) })
end end

2
spec/models/form/sales/pages/shared_ownership_deposit_value_check_spec.rb

@ -34,7 +34,7 @@ RSpec.describe Form::Sales::Pages::SharedOwnershipDepositValueCheck, type: :mode
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ expect(page.title_text).to eq({
"translation" => "soft_validations.shared_ownership_deposit.title_text", "translation" => "soft_validations.shared_ownership_deposit.title_text",
"arguments" => [{ "i18n_template" => "expected_shared_ownership_deposit_value", "key" => "expected_shared_ownership_deposit_value", "label" => false, "money" => true }], "arguments" => [{ "arguments_for_key" => "expected_shared_ownership_deposit_value", "i18n_template" => "expected_shared_ownership_deposit_value", "key" => "field_formatted_as_currency" }],
}) })
end end

Loading…
Cancel
Save