mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
[wip] Use a custom message format for associated errors
This is much simpler. Multiple messages get combined onto one line though, which is not perfect. And.. it still seems to duplicate the errors. Weird, it's fine on my frontend though.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user