diff --git a/app/models/concerns/nested_calculator_validation.rb b/app/models/concerns/nested_calculator_validation.rb index b6cf2abc1b..cddd3c506a 100644 --- a/app/models/concerns/nested_calculator_validation.rb +++ b/app/models/concerns/nested_calculator_validation.rb @@ -4,48 +4,9 @@ module NestedCalculatorValidation extend ActiveSupport::Concern included do - validates_associated :calculator - after_validation :set_custom_error_messages, if: :calculator_errors? - end - - def calculator_errors? - calculator.errors.any? - end - - def set_custom_error_messages - add_custom_error_messages - delete_generic_error_message - delete_preferred_value_errors - end - - def add_custom_error_messages - calculator.errors.messages&.each do |attribute, msgs| - msgs.each do |msg| - errors.add(:base, "#{localize_calculator_attributes[attribute]} #{msg}") - end - end - end - - def delete_generic_error_message - errors.delete(:calculator) if errors[:calculator] && errors[:calculator][0] == "is invalid" - end - - def delete_preferred_value_errors - calculator.preferences.each do |k, _v| - errors.delete("calculator.preferred_#{k}".to_sym ) - end - end - - def localize_calculator_attributes - { - preferred_amount: I18n.t('spree.amount'), - preferred_flat_percent: I18n.t('spree.flat_percent'), - preferred_first_item: I18n.t('spree.first_item'), - preferred_additional_item: I18n.t('spree.additional_item'), - preferred_max_items: I18n.t('spree.max_items'), - preferred_normal_amount: I18n.t('spree.normal_amount'), - preferred_discount_amount: I18n.t('spree.discount_amount'), - preferred_minimal_amount: I18n.t('spree.minimal_amount'), + validates_associated :calculator, message: ->(class_obj, obj) { + # Include all error messages from object + obj[:value].errors.full_messages.join("; ") } end end diff --git a/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index 964065b605..90d227b840 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -6,7 +6,6 @@ module Spree class PaymentMethod < ApplicationRecord include CalculatedAdjustments include PaymentMethodDistributors - include NestedCalculatorValidation acts_as_taggable acts_as_paranoid @@ -18,6 +17,7 @@ module Spree validates :name, presence: true validate :distributor_validation + include NestedCalculatorValidation after_initialize :init diff --git a/config/locales/en.yml b/config/locales/en.yml index 1d00c22551..d08c02fce5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -79,6 +79,17 @@ en: orders_close_at: Close date variant_override: count_on_hand: "On Hand" + spree/calculator: + preferred_flat_percent: "Flat Percent" + preferred_amount: "Amount" + preferred_first_item: "First Item" + preferred_additional_item: "Additional Item Cost" + preferred_max_items: "Max Items" + preferred_minimal_amount: "Minimal Amount" + preferred_normal_amount: "Normal Amount" + preferred_discount_amount: "Discount Amount" + preferred_unit_from_list: "Unit From List" + preferred_per_unit: "Per Unit" errors: messages: calculator_preferred_value_error: "has an invalid input. Please use only numbers. For example: 10, 5.5, -20" diff --git a/spec/models/concerns/nested_calculator_validation_spec.rb b/spec/models/concerns/nested_calculator_validation_spec.rb index d6e9551a5b..843d965da0 100644 --- a/spec/models/concerns/nested_calculator_validation_spec.rb +++ b/spec/models/concerns/nested_calculator_validation_spec.rb @@ -27,7 +27,8 @@ shared_examples "a parent model that has a Calculator" do |parent_name| end it "adds custom error messages to base" do - expect(invalid_parent.errors[:base]).to include(/^Amount: Invalid input/) + error_messages = invalid_parent.errors.full_messages + expect(error_messages).to include(/^Calculator Amount has an invalid input./) end it "has the correct number of errors messages" do @@ -57,7 +58,8 @@ shared_examples "a parent model that has a Calculator" do |parent_name| end it "adds custom error messages to base" do - expect(localized_parent.errors[:base]).to include(/Amount has an invalid format/) + error_messages = localized_parent.errors.full_messages + expect(error_messages).to include(/^Calculator Amount has an invalid format./) end end end