Skip to content

Commit

Permalink
Implement a separate error message for when no date fields are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraghiorghisor-tw committed Mar 21, 2024
1 parent dabb1a3 commit 9093fec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 10 additions & 6 deletions app/controllers/admin/schedulings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 15 additions & 1 deletion spec/controllers/admin/schedulings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""],
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9093fec

Please sign in to comment.