fix: add null checks for articles convert_units
Prevents division by zero exception because of a unit beeing 0. A Unit becomes also zero e.g. when a comma symbol is used Unit.new("0,9kg") == 0 fixes #1014
This commit is contained in:
parent
45e2668cea
commit
33034e66b8
2 changed files with 13 additions and 1 deletions
|
@ -211,7 +211,7 @@ class Article < ApplicationRecord
|
|||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
if fc_unit && supplier_unit && fc_unit =~ supplier_unit
|
||||
if fc_unit != 0 && supplier_unit != 0 && fc_unit && supplier_unit && fc_unit =~ supplier_unit
|
||||
conversion_factor = (supplier_unit / fc_unit).to_base.to_r
|
||||
new_price = new_article.price / conversion_factor
|
||||
new_unit_quantity = new_article.unit_quantity * conversion_factor
|
||||
|
|
|
@ -25,6 +25,18 @@ describe Article do
|
|||
expect(article.convert_units(article1)).to be false
|
||||
end
|
||||
|
||||
it 'returns false if unit = 0' do
|
||||
article1 = build(:article, supplier: supplier, unit: '1kg', price: 2, unit_quantity: 1)
|
||||
article2 = build(:article, supplier: supplier, unit: '0kg', price: 2, unit_quantity: 1)
|
||||
expect(article1.convert_units(article2)).to be false
|
||||
end
|
||||
|
||||
it 'returns false if unit becomes zero because of , symbol in unit format' do
|
||||
article1 = build(:article, supplier: supplier, unit: '0,8kg', price: 2, unit_quantity: 1)
|
||||
article2 = build(:article, supplier: supplier, unit: '0,9kg', price: 2, unit_quantity: 1)
|
||||
expect(article1.convert_units(article2)).to be false
|
||||
end
|
||||
|
||||
it 'converts from ST to KI (german foodcoops legacy)' do
|
||||
article1 = build(:article, supplier: supplier, unit: 'ST')
|
||||
article2 = build(:article, supplier: supplier, name: 'banana 10-12 St', price: 12.34, unit: 'KI')
|
||||
|
|
Loading…
Reference in a new issue