Merge pull request #8784 from apricot12/7398-Weight_field-on-non-weight-variants

Convert variant.weight into decimal if integer or 0.0 if nil
This commit is contained in:
Filipe
2022-01-31 18:55:31 +00:00
committed by GitHub
2 changed files with 38 additions and 0 deletions

View File

@@ -79,6 +79,8 @@ module Spree
after_create :create_stock_items
after_create :set_position
before_save :convert_variant_weight_to_decimal
around_destroy :destruction
# default variant scope only lists non-deleted variants
@@ -243,5 +245,9 @@ module Spree
self.unit_value = 1.0
end
def convert_variant_weight_to_decimal
self.weight = weight.to_d
end
end
end

View File

@@ -525,6 +525,38 @@ module Spree
expect(product.master).to be_valid
end
end
context "when the product's unit is non-weight" do
before do
product.update_attribute :variant_unit, 'volume'
product.reload
variant.reload
end
it "sets weight to decimal before save if it's integer" do
variant.weight = 1
variant.save!
expect(variant.weight).to eq 1.0
end
it "sets weight to 0.0 before save if it's nil" do
variant.weight = nil
variant.save!
expect(variant.weight).to eq 0.0
end
it "sets weight to 0.0 if input is a non numerical string" do
variant.weight = "BANANAS!"
variant.save!
expect(variant.weight).to eq 0.0
end
it "sets weight to correct decimal value if input is numerical string" do
variant.weight = "2"
variant.save!
expect(variant.weight).to eq 2.0
end
end
end
describe "unit value/description" do