From 6b1426d0c6453a144cc307bc52cf46fce88988b4 Mon Sep 17 00:00:00 2001 From: James Wu Date: Tue, 24 Jan 2023 16:25:44 +0900 Subject: [PATCH] Add numericality validation for PriceSack --- app/models/calculator/price_sack.rb | 5 +++++ spec/models/calculator/price_sack_spec.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/models/calculator/price_sack.rb b/app/models/calculator/price_sack.rb index 910b663b1d..d337e77b2f 100644 --- a/app/models/calculator/price_sack.rb +++ b/app/models/calculator/price_sack.rb @@ -14,6 +14,11 @@ module Calculator :preferred_normal_amount, :preferred_discount_amount + validates :preferred_minimal_amount, + :preferred_normal_amount, + :preferred_discount_amount, + numericality: { message: :calculator_preferred_value_error } + def self.description I18n.t(:price_sack) end diff --git a/spec/models/calculator/price_sack_spec.rb b/spec/models/calculator/price_sack_spec.rb index 0034b20963..c9b5272c33 100644 --- a/spec/models/calculator/price_sack_spec.rb +++ b/spec/models/calculator/price_sack_spec.rb @@ -12,6 +12,21 @@ describe Calculator::PriceSack do end let(:line_item) { build_stubbed(:line_item, price: price, quantity: 2) } + it do + should validate_numericality_of(:preferred_minimal_amount). + with_message("Invalid input. Please use only numbers. For example: 10, 5.5, -20") + end + + it do + should validate_numericality_of(:preferred_normal_amount). + with_message("Invalid input. Please use only numbers. For example: 10, 5.5, -20") + end + + it do + should validate_numericality_of(:preferred_discount_amount). + with_message("Invalid input. Please use only numbers. For example: 10, 5.5, -20") + end + context 'when the order amount is below preferred minimal' do let(:price) { 2 }