Reorder error messages to improve readability

tbs;
This commit is contained in:
Jean-Baptiste Bellet
2023-02-13 16:03:52 +01:00
parent 9a03023b6b
commit a39598d049
2 changed files with 36 additions and 0 deletions

View File

@@ -60,6 +60,9 @@ class SplitCheckoutController < ::BaseController
def order_error_messages
# Remove ship_address.* errors if no shipping method is not selected
remove_ship_address_errors if @order.errors[:shipping_method].present?
# Reorder errors to make sure the most important ones are shown first
# and finally, return the error messages to sentence
reorder_errors.map(&:full_message).to_sentence
end
def remove_ship_address_errors
@@ -71,6 +74,28 @@ class SplitCheckoutController < ::BaseController
@order.errors.delete("ship_address.zipcode")
end
def reorder_errors
@order.errors.sort_by do |e|
case e.attribute
when /email/i then 0
when /phone/i then 1
when /bill_address/i then 2 + bill_address_error_order(e)
else 20
end
end
end
def bill_address_error_order(error)
case error.attribute
when /firstname/i then 0
when /lastname/i then 1
when /address1/i then 2
when /city/i then 3
when /zipcode/i then 4
else 5
end
end
def flash_error_when_no_shipping_method_available
flash[:error] = I18n.t('split_checkout.errors.no_shipping_methods_available')
end

View File

@@ -203,6 +203,17 @@ describe "As a consumer, I want to checkout my order" do
expect(page).not_to have_content "Bill address phone can't be blank"
expect(page).to have_content "Customer phone can't be blank"
end
context "with no email filled in" do
before do
fill_in "Email", with: ""
click_button "Next - Payment method"
end
it "should display error message in the right order" do
expect(page).to have_content "Customer E-Mail can't be blank, Customer E-Mail is invalid, Customer phone can't be blank, Bill address firstname can't be blank, Bill address lastname can't be blank, Bill address address1 can't be blank, Bill address city can't be blank, Bill address zipcode can't be blank, and Shipping method Select a shipping method"
end
end
end
it "should allow visit '/checkout/details'" do