mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-29 21:17:17 +00:00
LineItem final_weight_volume is updated whenever quantity is changed, though can be set directly
This commit is contained in:
@@ -7,7 +7,7 @@ Spree::LineItem.class_eval do
|
||||
attr_accessible :max_quantity, :final_weight_volume
|
||||
attr_accessible :final_weight_volume, :price, :as => :api
|
||||
|
||||
before_create :inherit_units_from_variant
|
||||
before_save :calculate_final_weight_volume, unless: :final_weight_volume_changed?
|
||||
after_save :update_units
|
||||
|
||||
delegate :unit_description, to: :variant
|
||||
@@ -83,9 +83,13 @@ Spree::LineItem.class_eval do
|
||||
|
||||
private
|
||||
|
||||
def inherit_units_from_variant
|
||||
if final_weight_volume || variant.andand.unit_value
|
||||
self.final_weight_volume = final_weight_volume || ((variant.andand.unit_value) * quantity)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -97,23 +97,98 @@ module Spree
|
||||
let!(:v) { p.variants.first }
|
||||
let!(:o) { create(:order) }
|
||||
|
||||
describe "when no final_weight_volume is set" do
|
||||
let(:li) { build(:line_item, order: o, variant: v, quantity: 3) }
|
||||
context "on create" do
|
||||
context "when no final_weight_volume is set" do
|
||||
let(:li) { build(:line_item, order: o, variant: v, quantity: 3) }
|
||||
|
||||
it "initializes final_weight_volume from the variant's unit_value on create" do
|
||||
expect(li.final_weight_volume).to be nil
|
||||
li.save
|
||||
expect(li.final_weight_volume).to eq 3000
|
||||
it "initializes final_weight_volume from the variant's unit_value" do
|
||||
expect(li.final_weight_volume).to be nil
|
||||
li.save
|
||||
expect(li.final_weight_volume).to eq 3000
|
||||
end
|
||||
end
|
||||
|
||||
context "when a final_weight_volume has been set" do
|
||||
let(:li) { build(:line_item, order: o, variant: v, quantity: 3, final_weight_volume: 2000) }
|
||||
|
||||
it "uses the changed value" do
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
li.save
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when a final_weight_volume has been set" do
|
||||
let(:li) { build(:line_item, order: o, variant: v, quantity: 3, final_weight_volume: 2000) }
|
||||
context "on save" do
|
||||
let!(:li) { create(:line_item, order: o, variant: v, quantity: 3) }
|
||||
|
||||
it "uses the existing value" do
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
li.save
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
before do
|
||||
expect(li.final_weight_volume).to eq 3000
|
||||
end
|
||||
|
||||
context "when final_weight_volume is changed" do
|
||||
let(:attrs) { { final_weight_volume: 2000 } }
|
||||
|
||||
context "and quantity is not changed" do
|
||||
before do
|
||||
li.update_attributes(attrs)
|
||||
end
|
||||
|
||||
it "uses the value given" do
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
end
|
||||
end
|
||||
|
||||
context "and quantity is changed" do
|
||||
before do
|
||||
attrs.merge!( quantity: 4 )
|
||||
li.update_attributes(attrs)
|
||||
end
|
||||
|
||||
it "uses the value given" do
|
||||
expect(li.final_weight_volume).to eq 2000
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when final_weight_volume is not changed" do
|
||||
let(:attrs) { { price: 3.00 } }
|
||||
|
||||
context "and quantity is not changed" do
|
||||
before do
|
||||
li.update_attributes(attrs)
|
||||
end
|
||||
|
||||
it "does not change final_weight_volume" do
|
||||
expect(li.final_weight_volume).to eq 3000
|
||||
end
|
||||
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)
|
||||
end
|
||||
|
||||
it "calculates a final_weight_volume from the variants unit_value" do
|
||||
expect(li.final_weight_volume).to eq 4000
|
||||
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)
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,9 +23,10 @@ module Spree
|
||||
li.should be
|
||||
li.quantity.should == 1
|
||||
li.max_quantity.should == 2
|
||||
li.final_weight_volume.should == 1.0
|
||||
end
|
||||
|
||||
it "updates a variant's quantity and max quantity" do
|
||||
it "updates a variant's quantity, max quantity and final_weight_volume" do
|
||||
order.add_variant v, 1, 2
|
||||
|
||||
op.populate({variants: {v.id.to_s => {quantity: '2', max_quantity: '3'}}}, true)
|
||||
@@ -33,6 +34,7 @@ module Spree
|
||||
li.should be
|
||||
li.quantity.should == 2
|
||||
li.max_quantity.should == 3
|
||||
li.final_weight_volume.should == 2.0
|
||||
end
|
||||
|
||||
it "removes a variant" do
|
||||
|
||||
Reference in New Issue
Block a user