Browse Source

Update errors

pull/981/head
Kat 4 years ago
parent
commit
7cc21aef4c
  1. 19
      app/controllers/locations_controller.rb
  2. 3
      config/locales/en.yml
  3. 39
      spec/requests/locations_controller_spec.rb

19
app/controllers/locations_controller.rb

@ -172,10 +172,23 @@ private
month = params[:location]["deactivation_date(2i)"] month = params[:location]["deactivation_date(2i)"]
year = params[:location]["deactivation_date(1i)"] year = params[:location]["deactivation_date(1i)"]
if [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200)
Date.new(year.to_i, month.to_i, day.to_i) if [day, month, year].any?(&:blank?) || !Date.valid_date?(year.to_i, month.to_i, day.to_i) || !year.to_i.between?(2000, 2200)
set_deactivation_date_errors(day, month, year)
else else
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered")) Date.new(year.to_i, month.to_i, day.to_i)
end
end
def set_deactivation_date_errors(day, month, year)
if [day, month, year].any?(&:blank?)
{day:, month:, year:}.each do |period, value|
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s )) if value.blank?
end
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
elsif !year.to_i.between?(2000, 2200)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
end end
end end
end end

3
config/locales/en.yml

@ -315,7 +315,8 @@ en:
location: location:
deactivation_date: deactivation_date:
not_selected: "Select one of the options" not_selected: "Select one of the options"
not_entered: "Enter a date" not_entered: "Enter a %{period}"
invalid: "Enter a valid date"
soft_validations: soft_validations:
net_income: net_income:

39
spec/requests/locations_controller_spec.rb

@ -1278,21 +1278,48 @@ RSpec.describe LocationsController, type: :request do
end end
end end
context "when the date is not entered" do context "when the date is not selected" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "", "deactivation_date(1i)": ""} }} let(:params) { { location: { "deactivation_date": "" } }}
it "displays the new page with an error message" do it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered")) expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_selected"))
end end
end end
context "when the date is not selected" do context "when invalid date is entered" do
let(:params) { { location: { "deactivation_date": "" } }} let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "44", "deactivation_date(1i)": "2022"} }}
it "displays the new page with an error message" do it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_selected")) expect(page).to have_content(I18n.t("validations.location.deactivation_date.invalid"))
end
end
context "when the day is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022"} }}
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "day"))
end
end
context "when the month is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "", "deactivation_date(1i)": "2022"} }}
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "month"))
end
end
context "when the year is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "2", "deactivation_date(1i)": ""} }}
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "year"))
end end
end end
end end

Loading…
Cancel
Save