mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix specs to work with unit_value stored in base units, scale units correctly for output
This commit is contained in:
@@ -44,20 +44,31 @@ Spree::Variant.class_eval do
|
||||
'volume' => {0.001 => 'mL', 1.0 => 'L', 1000000.0 => 'ML'}}
|
||||
|
||||
if unit_value.present?
|
||||
value = unit_value
|
||||
value = value.to_i if value == value.to_i
|
||||
|
||||
if %w(weight volume).include? self.product.variant_unit
|
||||
unit = units[self.product.variant_unit][self.product.variant_unit_scale]
|
||||
unit = units[self.product.variant_unit].select { |scale, unit_name|
|
||||
unit_value / scale > 1
|
||||
}.to_a.last
|
||||
unit = units[self.product.variant_unit].first if unit.nil?
|
||||
|
||||
unit_scale = unit[0]
|
||||
unit_name = unit[1]
|
||||
|
||||
value = unit_value / unit_scale
|
||||
|
||||
else
|
||||
unit = self.product.variant_unit_name
|
||||
unit = unit.pluralize if value > 1
|
||||
value = unit_value
|
||||
unit_name = self.product.variant_unit_name
|
||||
unit_name = unit_name.pluralize if value > 1
|
||||
end
|
||||
|
||||
value = value.to_i if value == value.to_i
|
||||
|
||||
else
|
||||
value = unit = nil
|
||||
value = unit_name = nil
|
||||
end
|
||||
|
||||
[value, unit]
|
||||
[value, unit_name]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -150,7 +150,7 @@ module Spree
|
||||
[[1.0, 'g'], [1000.0, 'kg'], [1000000.0, 'T']].each do |scale, unit|
|
||||
p = double(:product, variant_unit: 'weight', variant_unit_scale: scale)
|
||||
v.stub(:product) { p }
|
||||
v.stub(:unit_value) { 100 }
|
||||
v.stub(:unit_value) { 100 * scale }
|
||||
v.send(:option_value_value_unit).should == [100, unit]
|
||||
end
|
||||
end
|
||||
@@ -159,7 +159,7 @@ module Spree
|
||||
[[0.001, 'mL'], [1.0, 'L'], [1000000.0, 'ML']].each do |scale, unit|
|
||||
p = double(:product, variant_unit: 'volume', variant_unit_scale: scale)
|
||||
v.stub(:product) { p }
|
||||
v.stub(:unit_value) { 100 }
|
||||
v.stub(:unit_value) { 100 * scale }
|
||||
v.send(:option_value_value_unit).should == [100, unit]
|
||||
end
|
||||
end
|
||||
@@ -186,9 +186,6 @@ module Spree
|
||||
v.stub(:unit_value) { nil }
|
||||
v.send(:option_value_value_unit).should == [nil, nil]
|
||||
end
|
||||
|
||||
it "switches units upwards when outside the base scale"
|
||||
it "switches units downwards when outside the base scale"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user