Browse Source

refactor: remove duplicated behaviour

pull/1225/head
natdeanlewissoftwire 2 years ago
parent
commit
9ef3b38886
  1. 38
      app/models/form/sales/pages/about_price_shared_ownership_max_value_check.rb
  2. 38
      app/models/form/sales/pages/about_price_shared_ownership_min_value_check.rb
  3. 42
      app/models/form/sales/pages/about_price_shared_ownership_value_check.rb
  4. 3
      app/models/form/sales/subsections/shared_ownership_scheme.rb
  5. 22
      app/models/validations/sales/soft_validations.rb
  6. 6
      config/locales/en.yml
  7. 4
      spec/models/form_handler_spec.rb

38
app/models/form/sales/pages/about_price_shared_ownership_max_value_check.rb

@ -1,38 +0,0 @@
class Form::Sales::Pages::AboutPriceSharedOwnershipMaxValueCheck < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "about_price_shared_ownership_max_value_check"
@depends_on = [
{
"purchase_price_above_soft_max?" => true,
},
]
@informative_text = {}
@title_text = {
"translation" => "soft_validations.purchase_price.max.title_text",
"arguments" => [
{
"key" => "value",
"label" => true,
"i18n_template" => "value",
},
],
}
@informative_text = {
"translation" => "soft_validations.purchase_price.max.hint_text",
"arguments" => [
{
"key" => "purchase_price_soft_max",
"label" => false,
"i18n_template" => "soft_max",
},
],
}
end
def questions
@questions ||= [
Form::Sales::Questions::AboutPriceSharedOwnershipValueCheck.new(nil, nil, self),
]
end
end

38
app/models/form/sales/pages/about_price_shared_ownership_min_value_check.rb

@ -1,38 +0,0 @@
class Form::Sales::Pages::AboutPriceSharedOwnershipMinValueCheck < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "about_price_shared_ownership_min_value_check"
@depends_on = [
{
"purchase_price_below_soft_min?" => true,
},
]
@informative_text = {}
@title_text = {
"translation" => "soft_validations.purchase_price.min.title_text",
"arguments" => [
{
"key" => "value",
"label" => true,
"i18n_template" => "value",
},
],
}
@informative_text = {
"translation" => "soft_validations.purchase_price.min.hint_text",
"arguments" => [
{
"key" => "purchase_price_soft_min",
"label" => false,
"i18n_template" => "soft_min",
},
],
}
end
def questions
@questions ||= [
Form::Sales::Questions::AboutPriceSharedOwnershipValueCheck.new(nil, nil, self),
]
end
end

42
app/models/form/sales/pages/about_price_shared_ownership_value_check.rb

@ -0,0 +1,42 @@
class Form::Sales::Pages::AboutPriceSharedOwnershipValueCheck < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "about_price_shared_ownership_value_check"
@depends_on = [
{
"purchase_price_out_of_soft_range?" => true,
},
]
@title_text = {
"translation" => "soft_validations.purchase_price.title_text",
"arguments" => [
{
"key" => "value",
"label" => true,
"i18n_template" => "value",
},
],
}
@informative_text = {
"translation" => "soft_validations.purchase_price.hint_text",
"arguments" => [
{
"key" => "purchase_price_soft_min_or_soft_max",
"label" => false,
"i18n_template" => "soft_min_or_soft_max",
},
{
"key" => "purchase_price_min_or_max_text",
"label" => false,
"i18n_template" => "min_or_max",
},
],
}
end
def questions
@questions ||= [
Form::Sales::Questions::AboutPriceSharedOwnershipValueCheck.new(nil, nil, self),
]
end
end

3
app/models/form/sales/subsections/shared_ownership_scheme.rb

@ -21,8 +21,7 @@ class Form::Sales::Subsections::SharedOwnershipScheme < ::Form::Subsection
Form::Sales::Pages::PreviousPropertyType.new(nil, nil, self),
Form::Sales::Pages::PreviousTenure.new(nil, nil, self),
Form::Sales::Pages::AboutPriceSharedOwnership.new(nil, nil, self),
Form::Sales::Pages::AboutPriceSharedOwnershipMinValueCheck.new(nil, nil, self),
Form::Sales::Pages::AboutPriceSharedOwnershipMaxValueCheck.new(nil, nil, self),
Form::Sales::Pages::AboutPriceSharedOwnershipValueCheck.new(nil, nil, self),
Form::Sales::Pages::Mortgageused.new("mortgage_used_shared_ownership", nil, self),
Form::Sales::Pages::MortgageAmount.new("mortgage_amount_shared_ownership", nil, self),
Form::Sales::Pages::MortgageLender.new("mortgage_lender_shared_ownership", nil, self),

22
app/models/validations/sales/soft_validations.rb

@ -43,17 +43,25 @@ module Validations::Sales::SoftValidations
((exdate.to_date - hodate.to_date).to_i / 365) >= 3
end
def purchase_price_below_soft_min?
def purchase_price_out_of_soft_range?
return unless value && beds && la
purchase_price_range = LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds)
purchase_price_range.present? && value < purchase_price_range.soft_min
purchase_price_range.present? && !value.between?(purchase_price_range.soft_min, purchase_price_range.soft_max)
end
def purchase_price_above_soft_max?
return unless value && beds && la
def purchase_price_min_or_max_text
value < purchase_price_range.soft_min ? "minimum" : "maximum"
end
def purchase_price_soft_min_or_soft_max
value < purchase_price_range.soft_min ? purchase_price_range.soft_min : purchase_price_range.soft_max
end
private
purchase_price_range = LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds)
purchase_price_range.present? && value > purchase_price_range.soft_max
def purchase_price_range
LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds)
end
end

6
config/locales/en.yml

@ -434,12 +434,8 @@ en:
title_text: "You told us the rent is %{brent}"
hint_text: "The maximum rent expected for this type of property in this local authority is £%{soft_max_for_period}"
purchase_price:
min:
title_text: "You told us the purchase price is %{value}"
hint_text: "The minimum purchase price expected for this type of property in this local authority is £%{soft_min}"
max:
title_text: "You told us the purchase price is %{value}"
hint_text: "The maximum purchase price expected for this type of property in this local authority is £%{soft_max}"
hint_text: "The %{min_or_max} purchase price expected for this type of property in this local authority is £%{soft_min_or_soft_max}"
retirement:
min:
title: "You told us this person is under %{age} and retired"

4
spec/models/form_handler_spec.rb

@ -52,14 +52,14 @@ RSpec.describe FormHandler do
it "is able to load a current sales form" do
form = form_handler.get_form("current_sales")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(183)
expect(form.pages.count).to eq(184)
expect(form.name).to eq("2022_2023_sales")
end
it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(183)
expect(form.pages.count).to eq(184)
expect(form.name).to eq("2021_2022_sales")
end
end

Loading…
Cancel
Save