diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index ee4384b460..9db9ae1269 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -7,7 +7,7 @@ Spree::LineItem.class_eval do attr_accessible :max_quantity, :final_weight_volume, :price attr_accessible :final_weight_volume, :price, :as => :api - before_save :calculate_final_weight_volume, unless: :final_weight_volume_changed? + before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed? after_save :update_units delegate :unit_description, to: :variant @@ -88,12 +88,10 @@ Spree::LineItem.class_eval do private def calculate_final_weight_volume - if quantity_changed? - if final_weight_volume.present? - self.final_weight_volume = final_weight_volume * quantity / quantity_was - elsif variant.andand.unit_value - self.final_weight_volume = ((variant.andand.unit_value) * quantity) - end + if final_weight_volume.present? && quantity_was > 0 + self.final_weight_volume = final_weight_volume * quantity / quantity_was + elsif variant.andand.unit_value.present? + self.final_weight_volume = variant.andand.unit_value * quantity end end end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index ecbd3ae610..6bb18fafa3 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -165,27 +165,57 @@ module Spree end context "and quantity is changed" do - context "and a final_weight_volume has been set" do - before do - expect(expect(li.final_weight_volume).to eq 3000) - attrs.merge!( quantity: 4 ) - li.update_attributes(attrs) + context "from > 0" do + context "and a final_weight_volume has been set" do + before do + expect(li.final_weight_volume).to eq 3000 + attrs.merge!( quantity: 4 ) + li.update_attributes(attrs) + end + + it "scales the final_weight_volume based on the change in quantity" do + expect(li.final_weight_volume).to eq 4000 + end end - it "calculates a final_weight_volume from the variants unit_value" do - expect(li.final_weight_volume).to eq 4000 + context "and a final_weight_volume has not been set" do + before do + li.update_attributes(final_weight_volume: nil) + attrs.merge!( quantity: 1 ) + li.update_attributes(attrs) + end + + it "calculates a final_weight_volume from the variants unit_value" do + expect(li.final_weight_volume).to eq 1000 + end end end - context "and a final_weight_volume has not been set" do - before do - li.update_attributes(final_weight_volume: nil) - attrs.merge!( quantity: 1 ) - li.update_attributes(attrs) + context "from 0" do + before { li.update_attributes(quantity: 0) } + + context "and a final_weight_volume has been set" do + before do + expect(li.final_weight_volume).to eq 0 + attrs.merge!( quantity: 4 ) + li.update_attributes(attrs) + end + + it "recalculates a final_weight_volume from the variants unit_value" do + expect(li.final_weight_volume).to eq 4000 + end end - it "calculates a final_weight_volume from the variants unit_value" do - expect(li.final_weight_volume).to eq 1000 + context "and a final_weight_volume has not been set" do + before do + li.update_attributes(final_weight_volume: nil) + attrs.merge!( quantity: 1 ) + li.update_attributes(attrs) + end + + it "calculates a final_weight_volume from the variants unit_value" do + expect(li.final_weight_volume).to eq 1000 + end end end end @@ -196,7 +226,7 @@ module Spree describe "generating the full name" do let(:li) { LineItem.new } - context "when display_name is blank" do + context "when display_name is blank" do before do li.stub(:unit_to_display) { 'unit_to_display' } li.stub(:display_name) { '' } @@ -223,7 +253,7 @@ module Spree li.stub(:unit_to_display) { '10kg' } li.stub(:display_name) { '10kg Box' } end - + it "returns display_name" do li.full_name.should == '10kg Box' end @@ -234,7 +264,7 @@ module Spree li.stub(:unit_to_display) { '1 Loaf' } li.stub(:display_name) { 'Spelt Sourdough' } end - + it "returns unit_to_display" do li.full_name.should == 'Spelt Sourdough (1 Loaf)' end