Simplify error when tax rate can't be inherited

I changed the text to focus on the resolution: the user needs to choose
a tax category or a different calculator.

I associated the error to the model to prevent the attribute name from
being included in the error message. Alternatively, we could have
changed the name of the attribute to match the UI. But this error
affects the combination of two attributes, none of them is invalid on
its own.

I'm using Rails' default lazy lookup for error messages which results in
shorter code and a standard structure.

I also added a simple spec.
This commit is contained in:
Maikel Linke
2023-03-06 14:37:25 +11:00
committed by Mohamed ABDELLANI
parent 89eb3a1967
commit 25b7f1749c
3 changed files with 16 additions and 5 deletions

View File

@@ -60,9 +60,7 @@ class EnterpriseFee < ApplicationRecord
end
if inherits_tax_category? && PER_ORDER_CALCULATORS.include?(calculator_type)
errors.add(:inherits_tax_category,
I18n.t("activerecord.errors.models." \
"enterpise_fee.cannot_inherit_from_product_when_per_order_calculator_selected"))
errors.add(:base, :inherit_tax_requires_per_item_calculator)
throw :abort
end

View File

@@ -92,6 +92,8 @@ en:
preferred_per_unit: "Calculator Per Unit:"
errors:
models:
enterprise_fee:
inherit_tax_requires_per_item_calculator: "Inheriting the tax categeory requires a per-item calculator."
spree/user:
attributes:
email:
@@ -113,8 +115,6 @@ en:
using_producer_stock_settings_but_count_on_hand_set: "must be blank because using producer stock settings"
on_demand_but_count_on_hand_set: "must be blank if on demand"
limited_stock_but_no_count_on_hand: "must be specified because forcing limited stock"
enterpise_fee:
cannot_inherit_from_product_when_per_order_calculator_selected: "You cannot select 'Inherit From Product' when a per order calculator is selected"
# Used by active_storage_validations
errors:
messages:

View File

@@ -9,6 +9,19 @@ describe EnterpriseFee do
describe "validations" do
it { is_expected.to validate_presence_of(:name) }
it "requires a per-item calculator to inherit tax" do
subject = build(
:enterprise_fee,
inherits_tax_category: true,
calculator: Calculator::FlatRate.new
)
expect(subject.save).to eq false
expect(subject.errors.full_messages.first).to eq(
"Inheriting the tax categeory requires a per-item calculator."
)
end
end
describe "callbacks" do