Browse Source

move code

pull/683/head
Dushan Despotovic 3 years ago
parent
commit
66939b3661
  1. 15
      app/models/validations/date_validations.rb
  2. 27
      app/models/validations/setup_validations.rb
  3. 39
      spec/models/validations/date_validations_spec.rb
  4. 73
      spec/models/validations/setup_validations_spec.rb

15
app/models/validations/date_validations.rb

@ -34,6 +34,21 @@ module Validations::DateValidations
if record.startdate < first_collection_start_date || record.startdate > second_collection_end_date
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end
# if record.voiddate.present?
# if (record.startdate.to_date - record.voiddate.to_date).to_i.abs > 730
# record.errors.add :startdate, I18n.t("validations.setup.startdate.voiddate_difference")
# end
# end
if record.scheme_id.present?
scheme_end_date = Scheme.find(record.scheme_id).end_date
if scheme_end_date.present?
if record.startdate > scheme_end_date
record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date")
end
end
end
end
def validate_sale_completion_date(record)

27
app/models/validations/setup_validations.rb

@ -5,33 +5,6 @@ module Validations::SetupValidations
end
end
def validate_startdate(record)
if record.startdate > Time.zone.today
record.errors.add :startdate, I18n.t("validations.setup.startdate.today_or_earlier")
end
if record.voiddate.present?
if (record.startdate.to_date - record.voiddate.to_date).to_i.abs > 730
record.errors.add :startdate, I18n.t("validations.setup.startdate.voiddate_difference")
end
end
if record.mrcdate.present?
if (record.startdate.to_date - record.mrcdate.to_date).to_i.abs > 730
record.errors.add :startdate, I18n.t("validations.setup.startdate.mrcdate_difference")
end
end
if record.scheme_id.present?
scheme_end_date = Scheme.find(record.scheme_id).end_date
if scheme_end_date.present?
if record.startdate > scheme_end_date
record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date")
end
end
end
end
private
def intermediate_product_rent_type?(record)

39
spec/models/validations/date_validations_spec.rb

@ -5,6 +5,8 @@ RSpec.describe Validations::DateValidations do
let(:validator_class) { Class.new { include Validations::DateValidations } }
let(:record) { FactoryBot.create(:case_log) }
let(:scheme) { FactoryBot.create(:scheme, end_date: Time.zone.today - 5.days)}
let(:scheme_no_end_date) { FactoryBot.create(:scheme, end_date: nil)}
describe "tenancy start date" do
it "cannot be before the first collection window start date" do
@ -30,6 +32,43 @@ RSpec.describe Validations::DateValidations do
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "validates that the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 3.days
record.scheme = scheme
setup_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.before_scheme_end_date"))
end
it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 30.days
record.scheme = scheme
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "produces no startdate error for scheme end dates when the chosen scheme does not have an end date" do
record.startdate = Time.zone.today
record.scheme = scheme_no_end_date
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
# it "validates that tenancy start date is less than 730 days away from the void date" do
# record.startdate = Time.zone.today
# record.voiddate = Time.zone.today - 3.years
# setup_validator.validate_startdate(record)
# expect(record.errors["startdate"])
# .to include(match I18n.t("validations.setup.startdate.voiddate_difference"))
# end
# it "produces no error tenancy start date is less than 730 days away from the void date" do
# record.startdate = Time.zone.today
# record.voiddate = Time.zone.today - 6.months
# setup_validator.validate_startdate(record)
# expect(record.errors["startdate"]).to be_empty
# end
end
describe "major repairs date" do

73
spec/models/validations/setup_validations_spec.rb

@ -30,77 +30,4 @@ RSpec.describe Validations::SetupValidations do
expect(record.errors["irproduct_other"]).to be_empty
end
end
context "when a user is setting up a supported housing log" do
describe "#validate_startdate" do
let(:record) { FactoryBot.create(:case_log, needstype: 2) }
let(:scheme) { FactoryBot.create(:scheme, end_date: Time.zone.today - 5.days)}
let(:scheme_no_end_date) { FactoryBot.create(:scheme, end_date: nil)}
it "validates that the tenancy start date must be today or earlier" do
record.startdate = Time.zone.today + 3.days
setup_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.today_or_earlier"))
end
it "produces no error if the tenancy start date is today or earlier" do
record.startdate = Time.zone.today
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "validates that the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 3.days
record.scheme = scheme
setup_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.before_scheme_end_date"))
end
it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 30.days
record.scheme = scheme
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "produces no startdate error for scheme end dates when the chosen scheme does not have an end date" do
record.startdate = Time.zone.today
record.scheme = scheme_no_end_date
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "validates that tenancy start date is less than 730 days away from the void date" do
record.startdate = Time.zone.today
record.voiddate = Time.zone.today - 3.years
setup_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.voiddate_difference"))
end
it "produces no error tenancy start date is less than 730 days away from the void date" do
record.startdate = Time.zone.today
record.voiddate = Time.zone.today - 6.months
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
it "validates that tenancy start date is less than 730 days away from the major repairs date" do
record.startdate = Time.zone.today
record.mrcdate = Time.zone.today - 3.years
setup_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.mrcdate_difference"))
end
it "produces no error when tenancy start date is less than 730 days away from the major repairs date" do
record.startdate = Time.zone.today
record.mrcdate = Time.zone.today - 6.months
setup_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
end
end
end

Loading…
Cancel
Save