mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Pull validations-related methods out into a separate module
This commit is contained in:
42
app/models/concerns/order_validations.rb
Normal file
42
app/models/concerns/order_validations.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'active_support/concern'
|
||||
|
||||
module OrderValidations
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
def disallow_guest_order
|
||||
return unless using_guest_checkout? && registered_email?
|
||||
|
||||
errors.add(:email, I18n.t('devise.failure.already_registered'))
|
||||
end
|
||||
|
||||
# Check that line_items in the current order are available from a newly selected distribution
|
||||
def products_available_from_new_distribution
|
||||
return if OrderCycleDistributedVariants.new(order_cycle, distributor)
|
||||
.distributes_order_variants?(self)
|
||||
|
||||
errors.add(:base, I18n.t(:spree_order_availability_error))
|
||||
end
|
||||
|
||||
# Determine if email is required (we don't want validation errors before we hit the checkout)
|
||||
def require_email
|
||||
true unless (new_record? || cart?) && !checkout_processing
|
||||
end
|
||||
|
||||
def ensure_line_items_present
|
||||
return if line_items.present?
|
||||
|
||||
errors.add(:base, Spree.t(:there_are_no_items_for_this_order))
|
||||
false
|
||||
end
|
||||
|
||||
def ensure_available_shipping_rates
|
||||
return unless shipments.empty? || shipments.any? { |shipment| shipment.shipping_rates.blank? }
|
||||
|
||||
errors.add(:base, Spree.t(:items_cannot_be_shipped))
|
||||
false
|
||||
end
|
||||
end
|
||||
@@ -8,6 +8,7 @@ require 'open_food_network/tag_rule_applicator'
|
||||
module Spree
|
||||
class Order < ApplicationRecord
|
||||
include OrderShipment
|
||||
include OrderValidations
|
||||
include Checkout
|
||||
include Balance
|
||||
include SetUnusedAddressFields
|
||||
@@ -576,20 +577,6 @@ module Spree
|
||||
|
||||
private
|
||||
|
||||
def disallow_guest_order
|
||||
return unless using_guest_checkout? && registered_email?
|
||||
|
||||
errors.add(:email, I18n.t('devise.failure.already_registered'))
|
||||
end
|
||||
|
||||
# Check that line_items in the current order are available from a newly selected distribution
|
||||
def products_available_from_new_distribution
|
||||
return if OrderCycleDistributedVariants.new(order_cycle, distributor)
|
||||
.distributes_order_variants?(self)
|
||||
|
||||
errors.add(:base, I18n.t(:spree_order_availability_error))
|
||||
end
|
||||
|
||||
def deliver_order_confirmation_email
|
||||
return if subscription.present?
|
||||
|
||||
@@ -626,23 +613,6 @@ module Spree
|
||||
self.email = user.email if user
|
||||
end
|
||||
|
||||
# Determine if email is required (we don't want validation errors before we hit the checkout)
|
||||
def require_email
|
||||
return true unless (new_record? || cart?) && !checkout_processing
|
||||
end
|
||||
|
||||
def ensure_line_items_present
|
||||
return if line_items.present?
|
||||
|
||||
errors.add(:base, Spree.t(:there_are_no_items_for_this_order)) && (return false)
|
||||
end
|
||||
|
||||
def ensure_available_shipping_rates
|
||||
return unless shipments.empty? || shipments.any? { |shipment| shipment.shipping_rates.blank? }
|
||||
|
||||
errors.add(:base, Spree.t(:items_cannot_be_shipped)) && (return false)
|
||||
end
|
||||
|
||||
def after_cancel
|
||||
shipments.each(&:cancel!)
|
||||
payments.checkout.each(&:void!)
|
||||
|
||||
Reference in New Issue
Block a user