diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 7ce771ac62..a95c004649 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -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 diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index eaddf968b0..46d24d9183 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -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