mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
Recalculate option values on variants when product variant unit is changed
This commit is contained in:
@@ -158,7 +158,7 @@ Spree::Product.class_eval do
|
||||
if variant_unit_changed?
|
||||
option_types.delete self.class.all_variant_unit_option_types
|
||||
option_types << variant_unit_option_type if variant_unit.present?
|
||||
variants_including_master.each { |v| v.delete_unit_option_values }
|
||||
variants_including_master.each { |v| v.update_units }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -40,18 +40,6 @@ Spree::Variant.class_eval do
|
||||
values.to_sentence({ :words_connector => ", ", :two_words_connector => ", " })
|
||||
end
|
||||
|
||||
def delete_unit_option_values
|
||||
ovs = self.option_values.where(option_type_id: Spree::Product.all_variant_unit_option_types)
|
||||
self.option_values.destroy ovs
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def update_weight_from_unit_value
|
||||
self.weight = unit_value / 1000 if self.product.variant_unit == 'weight' && unit_value.present?
|
||||
end
|
||||
|
||||
def update_units
|
||||
delete_unit_option_values
|
||||
|
||||
@@ -63,6 +51,17 @@ Spree::Variant.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_weight_from_unit_value
|
||||
self.weight = unit_value / 1000 if self.product.variant_unit == 'weight' && unit_value.present?
|
||||
end
|
||||
|
||||
def delete_unit_option_values
|
||||
ovs = self.option_values.where(option_type_id: Spree::Product.all_variant_unit_option_types)
|
||||
self.option_values.destroy ovs
|
||||
end
|
||||
|
||||
def option_value_name
|
||||
if display_as.present?
|
||||
display_as
|
||||
|
||||
@@ -45,6 +45,8 @@ feature %q{
|
||||
product.on_hand.should == 5
|
||||
product.description.should == "A description..."
|
||||
product.group_buy.should be_false
|
||||
product.master.option_values.map(&:name).should == ['5kg']
|
||||
product.master.options_text.should == "5kg"
|
||||
|
||||
# Distributors
|
||||
visit spree.product_distributions_admin_product_path(product)
|
||||
|
||||
@@ -442,24 +442,34 @@ module Spree
|
||||
p.update_attributes!(name: 'foo')
|
||||
end
|
||||
|
||||
it "removes the related option values from all its variants" do
|
||||
it "removes the related option values from all its variants and replaces them" do
|
||||
ot = Spree::OptionType.find_by_name 'unit_weight'
|
||||
v = create(:variant, product: p)
|
||||
v = create(:variant, unit_value: 1, product: p)
|
||||
p.reload
|
||||
|
||||
expect {
|
||||
v.option_values.map(&:name).include?("1L").should == false
|
||||
v.option_values.map(&:name).include?("1g").should == true
|
||||
expect {
|
||||
p.update_attributes!(variant_unit: 'volume', variant_unit_scale: 0.001)
|
||||
}.to change(v.option_values(true), :count).by(-1)
|
||||
}.to change(p.master.option_values(true), :count).by(0)
|
||||
v.reload
|
||||
v.option_values.map(&:name).include?("1L").should == true
|
||||
v.option_values.map(&:name).include?("1g").should == false
|
||||
end
|
||||
|
||||
it "removes the related option values from its master variant" do
|
||||
it "removes the related option values from its master variant and replaces them" do
|
||||
ot = Spree::OptionType.find_by_name 'unit_weight'
|
||||
p.master.update_attributes!(unit_value: 1)
|
||||
p.reload
|
||||
|
||||
expect {
|
||||
p.master.option_values.map(&:name).include?("1L").should == false
|
||||
p.master.option_values.map(&:name).include?("1g").should == true
|
||||
expect {
|
||||
p.update_attributes!(variant_unit: 'volume', variant_unit_scale: 0.001)
|
||||
}.to change(p.master.option_values(true), :count).by(-1)
|
||||
}.to change(p.master.option_values(true), :count).by(0)
|
||||
p.reload
|
||||
p.master.option_values.map(&:name).include?("1L").should == true
|
||||
p.master.option_values.map(&:name).include?("1g").should == false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -264,13 +264,13 @@ module Spree
|
||||
|
||||
it "removes option value associations for unit option types" do
|
||||
expect {
|
||||
@v.delete_unit_option_values
|
||||
@v.send(:delete_unit_option_values)
|
||||
}.to change(@v.option_values, :count).by(-1)
|
||||
end
|
||||
|
||||
it "does not delete option values" do
|
||||
expect {
|
||||
@v.delete_unit_option_values
|
||||
@v.send(:delete_unit_option_values)
|
||||
}.to change(Spree::OptionValue, :count).by(0)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user