Browse Source

refactor

pull/1022/head
Kat 4 years ago
parent
commit
42f88a2672
  1. 30
      app/models/validations/date_validations.rb
  2. 51
      app/models/validations/setup_validations.rb
  3. 4
      config/locales/en.yml
  4. 2
      spec/models/validations/date_validations_spec.rb
  5. 4
      spec/models/validations/setup_validations_spec.rb

30
app/models/validations/date_validations.rb

@ -61,29 +61,13 @@ module Validations::DateValidations
end end
location_status_during_startdate = status_during_startdate(record.startdate, record.location&.location_deactivation_periods, record.location&.available_from) location_status_during_startdate = status_during_startdate(record.startdate, record.location&.location_deactivation_periods, record.location&.available_from)
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated if location_status_during_startdate.present?
record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :startdate, I18n.t("validations.setup.startdate.location_#{location_status_during_startdate[:status]}", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: location_status_during_startdate[:deactivation_date]&.to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: location_status_during_startdate[:deactivation_date].to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :activating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.location_activating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
scheme_status_during_startdate = status_during_startdate(record.startdate, record.scheme&.scheme_deactivation_periods, record.scheme&.available_from) scheme_status_during_startdate = status_during_startdate(record.startdate, record.scheme&.scheme_deactivation_periods, record.scheme&.available_from)
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated if scheme_status_during_startdate.present?
record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme_#{scheme_status_during_startdate[:status]}", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: scheme_status_during_startdate[:deactivation_date]&.to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: scheme_status_during_startdate[:deactivation_date].to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :activating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme_activating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
end end
@ -122,10 +106,10 @@ private
return if date.blank? return if date.blank?
closest_reactivation = deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date - 1.day) } if deactivation_periods.present? closest_reactivation = deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date - 1.day) } if deactivation_periods.present?
return { status: :reactivating_soon, date: closest_reactivation.reactivation_date, deactivation_date: closest_reactivation.deactivation_date } if closest_reactivation.present? return { status: "reactivating_soon", date: closest_reactivation.reactivation_date, deactivation_date: closest_reactivation.deactivation_date } if closest_reactivation.present?
return { status: :activating_soon, date: available_from } if available_from.present? && available_from > date return { status: "activating_soon", date: available_from } if available_from.present? && available_from > date
open_deactivation = deactivation_periods.deactivations_without_reactivation.first if deactivation_periods.present? open_deactivation = deactivation_periods.deactivations_without_reactivation.first if deactivation_periods.present?
return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date <= date return { status: "deactivated", date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date <= date
end end
end end

51
app/models/validations/setup_validations.rb

@ -6,45 +6,15 @@ module Validations::SetupValidations
end end
def validate_location(record) def validate_location(record)
location_status_during_startdate = status_during_startdate(record.startdate, record.location&.location_deactivation_periods, record.location&.available_from) validate_location_during_startdate(record, :location_id)
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated
record.errors.add :location_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon
record.errors.add :location_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: location_status_during_startdate[:deactivation_date].to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :activating_soon
record.errors.add :location_id, I18n.t("validations.setup.startdate.location_activating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
end end
def validate_scheme(record) def validate_scheme(record)
location_status_during_startdate = status_during_startdate(record.startdate, record.location&.location_deactivation_periods, record.location&.available_from) validate_location_during_startdate(record, :scheme_id)
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: location_status_during_startdate[:deactivation_date].to_formatted_s(:govuk_date))
end
if location_status_during_startdate.present? && location_status_during_startdate[:status] == :activating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_activating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
scheme_status_during_startdate = status_during_startdate(record.startdate, record.scheme&.scheme_deactivation_periods, record.scheme&.available_from) scheme_status_during_startdate = status_during_startdate(record.startdate, record.scheme&.scheme_deactivation_periods, record.scheme&.available_from)
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated if scheme_status_during_startdate.present?
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme_#{scheme_status_during_startdate[:status]}", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: scheme_status_during_startdate[:deactivation_date]&.to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: scheme_status_during_startdate[:deactivation_date].to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :activating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme_activating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
end end
@ -58,10 +28,17 @@ private
return if date.blank? return if date.blank?
closest_reactivation = deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date - 1.day) } if deactivation_periods.present? closest_reactivation = deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date - 1.day) } if deactivation_periods.present?
return { status: :reactivating_soon, date: closest_reactivation.reactivation_date, deactivation_date: closest_reactivation.deactivation_date } if closest_reactivation.present? return { status: "reactivating_soon", date: closest_reactivation.reactivation_date, deactivation_date: closest_reactivation.deactivation_date } if closest_reactivation.present?
return { status: :activating_soon, date: available_from } if available_from.present? && available_from > date return { status: "activating_soon", date: available_from } if available_from.present? && available_from > date
open_deactivation = deactivation_periods.deactivations_without_reactivation.first if deactivation_periods.present? open_deactivation = deactivation_periods.deactivations_without_reactivation.first if deactivation_periods.present?
return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date <= date return { status: "deactivated", date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date <= date
end
def validate_location_during_startdate(record, field)
location_status_during_startdate = status_during_startdate(record.startdate, record.location&.location_deactivation_periods, record.location&.available_from)
if location_status_during_startdate.present?
record.errors.add field, I18n.t("validations.setup.startdate.location_#{location_status_during_startdate[:status]}", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date), deactivation_date: location_status_during_startdate[:deactivation_date]&.to_formatted_s(:govuk_date))
end
end end
end end

4
config/locales/en.yml

@ -120,10 +120,10 @@ en:
before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme"
after_void_date: "Enter a tenancy start date that is after the void date" after_void_date: "Enter a tenancy start date that is after the void date"
after_major_repair_date: "Enter a tenancy start date that is after the major repair date" after_major_repair_date: "Enter a tenancy start date that is after the major repair date"
during_deactivated_location: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered." location_deactivated: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered."
location_reactivating_soon: "The location %{postcode} was deactivated on %{deactivation_date} and is not available on the date you entered. It reactivates on %{date}" location_reactivating_soon: "The location %{postcode} was deactivated on %{deactivation_date} and is not available on the date you entered. It reactivates on %{date}"
location_activating_soon: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}" location_activating_soon: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}"
during_deactivated_scheme: "%{name} was deactivated on %{date} and was not available on the day you entered" scheme_deactivated: "%{name} was deactivated on %{date} and was not available on the day you entered"
scheme_reactivating_soon: "%{name} was deactivated on %{deactivation_date} and is not available on the date you entered. It reactivates on %{date}" scheme_reactivating_soon: "%{name} was deactivated on %{deactivation_date} and is not available on the date you entered. It reactivates on %{date}"
scheme_activating_soon: "%{name} is not available until %{date}. Enter a tenancy start date after %{date}" scheme_activating_soon: "%{name} is not available until %{date}. Enter a tenancy start date after %{date}"

2
spec/models/validations/date_validations_spec.rb

@ -98,7 +98,7 @@ RSpec.describe Validations::DateValidations do
record.location = location record.location = location
date_validator.validate_startdate(record) date_validator.validate_startdate(record)
expect(record.errors["startdate"]) expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.during_deactivated_location", postcode: location.postcode, date: "4 June 2022")) .to include(match I18n.t("validations.setup.startdate.location_deactivated", postcode: location.postcode, date: "4 June 2022"))
end end
it "produces no error when tenancy start date is during an active location period" do it "produces no error when tenancy start date is during an active location period" do

4
spec/models/validations/setup_validations_spec.rb

@ -46,7 +46,7 @@ RSpec.describe Validations::SetupValidations do
record.location = location record.location = location
setup_validator.validate_scheme(record) setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"]) expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.during_deactivated_location", postcode: location.postcode, date: "4 June 2022")) .to include(match I18n.t("validations.setup.startdate.location_deactivated", postcode: location.postcode, date: "4 June 2022"))
end end
it "produces no error when tenancy start date is during an active location period" do it "produces no error when tenancy start date is during an active location period" do
@ -168,7 +168,7 @@ RSpec.describe Validations::SetupValidations do
record.location = location record.location = location
setup_validator.validate_location(record) setup_validator.validate_location(record)
expect(record.errors["location_id"]) expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.during_deactivated_location", postcode: location.postcode, date: "4 June 2022")) .to include(match I18n.t("validations.setup.startdate.location_deactivated", postcode: location.postcode, date: "4 June 2022"))
end end
it "produces no error when tenancy start date is during an active location period" do it "produces no error when tenancy start date is during an active location period" do

Loading…
Cancel
Save