Simplify split checkout errors

This commit is contained in:
Matt-Yorkley
2023-07-04 13:57:28 +01:00
committed by Maikel Linke
parent 69dfd53658
commit d1bcdde49f
3 changed files with 6 additions and 78 deletions

View File

@@ -50,60 +50,13 @@ class SplitCheckoutController < ::BaseController
private
def render_error
flash.now[:error] ||= I18n.t(
'split_checkout.errors.saving_failed',
messages: order_error_messages
)
flash.now[:error] ||= I18n.t('split_checkout.errors.saving_failed')
render status: :unprocessable_entity, cable_ready: cable_car.
replace("#checkout", partial("split_checkout/checkout")).
replace("#flashes", partial("shared/flashes", locals: { flashes: flash }))
end
def order_error_messages
# Remove ship_address.* errors if no shipping method is not selected
remove_ship_address_errors if no_ship_address_needed?
# 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 no_ship_address_needed?
@order.errors[:shipping_method].present? || params[:ship_address_same_as_billing] == "1"
end
def remove_ship_address_errors
@order.errors.delete("ship_address.firstname")
@order.errors.delete("ship_address.address1")
@order.errors.delete("ship_address.city")
@order.errors.delete("ship_address.phone")
@order.errors.delete("ship_address.lastname")
@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 check_payments_adjustments
@order.payments.each(&:ensure_correct_adjustment)
end

View File

@@ -2135,7 +2135,7 @@ en:
submit: Complete order
cancel: Back to Payment method
errors:
saving_failed: "Saving failed, please update the highlighted fields. %{messages}"
saving_failed: "Saving failed, please update the highlighted fields."
terms_not_accepted: Please accept Terms and Conditions
required: Field cannot be blank
invalid_number: "Please enter a valid phone number"

View File

@@ -193,38 +193,16 @@ describe "As a consumer, I want to checkout my order" do
click_on "Checkout as guest"
end
context "should show proper list of errors" do
context "should show a flash message and inline error messages" do
before do
click_button "Next - Payment method"
expect(page).to have_content "Saving failed, please update the highlighted fields."
end
it "should not display any shipping errors messages when shipping method is not selected" do
expect(page).not_to have_content "Shipping address line 1 can't be blank"
expect(page).not_to have_content "Shipping address suburb 1 can't be blank"
expect(page).not_to have_content "Shipping address postcode can't be blank"
end
it "should not display bill address phone number error message" 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
expect(page).to have_content "Saving failed, please update the highlighted fields."
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, Billing address first name can't be blank, Billing address last name " \
"can't be blank, Billing address (Street + House number) can't be blank, Billing " \
"address city can't be blank, Billing address postcode can't be blank, and " \
"Shipping method Select a shipping method"
)
end
expect(page).to have_selector ".field_with_errors"
expect(page).to have_content "can't be blank"
end
end
@@ -562,7 +540,6 @@ describe "As a consumer, I want to checkout my order" do
end
within ".flash[type='error']" do
expect(page).to have_content "Saving failed, please update the highlighted fields."
expect(page).to have_content("can't be blank", count: 3)
end
end
@@ -620,8 +597,6 @@ describe "As a consumer, I want to checkout my order" do
end
within ".flash[type='error']" do
expect(page).to have_content("Saving failed, please update the highlighted fields")
expect(page).to have_content("can't be blank", count: 7)
expect(page).to have_content("is invalid", count: 1)
end
end
end