mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Move validations to state transitions
This commit is contained in:
@@ -37,10 +37,7 @@ class SplitCheckoutController < ::BaseController
|
||||
clear_invalid_payments
|
||||
redirect_to_step
|
||||
else
|
||||
if @shipping_method_id.blank?
|
||||
@order.errors.add(:base, "no_shipping_method_selected")
|
||||
end
|
||||
flash.now[:error] = "#{I18n.t('split_checkout.errors.global')}"
|
||||
flash.now[:error] = I18n.t('split_checkout.errors.global')
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
@@ -60,7 +57,6 @@ class SplitCheckoutController < ::BaseController
|
||||
|
||||
def update_order
|
||||
return unless params[:order]
|
||||
return if @order.state == "address" && params[:shipping_method_id].blank?
|
||||
|
||||
@order.update(order_params) && advance_order_state
|
||||
end
|
||||
|
||||
@@ -75,7 +75,9 @@ module Spree
|
||||
|
||||
before_transition to: :delivery, do: :create_proposed_shipments
|
||||
before_transition to: :delivery, do: :ensure_available_shipping_rates
|
||||
before_transition to: :payment, do: :validate_shipping_method!
|
||||
before_transition to: :payment, do: :create_tax_charge!
|
||||
before_transition to: :confirmation, do: :validate_payment_method!
|
||||
|
||||
after_transition to: :complete, do: :finalize!
|
||||
after_transition to: :resumed, do: :after_resume
|
||||
@@ -135,6 +137,24 @@ module Spree
|
||||
updated_at: Time.zone.now,
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_shipping_method!
|
||||
return unless user && Flipper.enabled?(:split_checkout, user)
|
||||
return if shipping_method.present?
|
||||
|
||||
errors.add :shipping_method, I18n.t('split_checkout.errors.select_a_shipping_method')
|
||||
throw :halt
|
||||
end
|
||||
|
||||
def validate_payment_method!
|
||||
return unless user && Flipper.enabled?(:split_checkout, user)
|
||||
return if payments.any?
|
||||
|
||||
errors.add :payment_method, I18n.t('split_checkout.errors.select_a_payment_method')
|
||||
throw :halt
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user