From 2357232468256a1c5898b29dca31323b28b0deca Mon Sep 17 00:00:00 2001 From: Laura Ghiorghisor Date: Thu, 21 Mar 2024 14:13:29 +0000 Subject: [PATCH] Implement a separate error message for when no date fields are provided --- app/controllers/admin/schedulings_controller.rb | 16 ++++++++++------ .../admin/schedulings_controller_spec.rb | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/schedulings_controller.rb b/app/controllers/admin/schedulings_controller.rb index 5448ba762..b9123de75 100644 --- a/app/controllers/admin/schedulings_controller.rb +++ b/app/controllers/admin/schedulings_controller.rb @@ -9,7 +9,7 @@ def new def create redirect_to admin_country_path(@country.slug) and return unless can_schedule_edition? - if has_valid_datetime_input_values? + if datetime_input_errors.empty? squashed_params = squash_multiparameter_scheduled_publication_time_attribute(scheduling_params) @edition.scheduled_publication_time = squashed_params[:scheduled_publication_time] @@ -21,7 +21,7 @@ def create end else @edition.errors.delete(:scheduled_publication_time) - @edition.errors.add(:scheduled_publication_time, "format is invalid") + @edition.errors.add(:scheduled_publication_time, datetime_input_errors[:scheduled_publication_time]) flash.now[:alert] = "We had some problems saving: #{@edition.errors.full_messages.join(', ')}." render "new" end @@ -47,17 +47,21 @@ def squash_multiparameter_scheduled_publication_time_attribute(params) params end - def has_valid_datetime_input_values? + def datetime_input_errors + errors = {} year, month, day, hour, minute = scheduling_params.to_h.sort.map { |_, v| v } - return false unless year.match?(/^\d{4}$/) && month.match?(/^\d{1,2}$/) && day.match?(/^\d{1,2}$/) \ + + return errors.merge!({ scheduled_publication_time: "cannot be blank" }) if [year, month, day].all?(&:blank?) + return errors.merge!({ scheduled_publication_time: "is not in the correct format" }) unless year.match?(/^\d{4}$/) && month.match?(/^\d{1,2}$/) && day.match?(/^\d{1,2}$/) \ && hour.match?(/^\d{1,2}$/) && minute.match?(/^\d{1,2}$/) begin Time.zone.local(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i) rescue ArgumentError - return false + return errors.merge!({ scheduled_publication_time: "is not in the correct format" }) end - true + + errors end def scheduling_params diff --git a/spec/controllers/admin/schedulings_controller_spec.rb b/spec/controllers/admin/schedulings_controller_spec.rb index f23c2a077..59cf16178 100644 --- a/spec/controllers/admin/schedulings_controller_spec.rb +++ b/spec/controllers/admin/schedulings_controller_spec.rb @@ -65,6 +65,20 @@ end context "invalid params" do + it "displays a validation error if all datetime fields are blank" do + params = { + "scheduled_publication_time(1i)" => "", + "scheduled_publication_time(2i)" => "", + "scheduled_publication_time(3i)" => "", + "scheduled_publication_time(4i)" => "", + "scheduled_publication_time(5i)" => "", + } + + post :create, params: { edition_id: @edition.id, scheduling: params } + + expect(response.body).to include "Scheduled publication time cannot be blank." + end + [ ["scheduled_publication_time", "1", ""], ["scheduled_publication_time", "2", ""], @@ -95,7 +109,7 @@ post :create, params: { edition_id: @edition.id, scheduling: params } - expect(response.body).to match(/Scheduled publication time format is invalid/) + expect(response.body).to match(/Scheduled publication time is not in the correct format/) end end