Remove taxon when primary taxon is changed

We are adding taxons to the product as you change the primary taxon.
However we never remove the previous primary taxon so it forces the user
to update the taxons manually. This can be a big problem if you are bulk
updating products.

We now remove the taxon that matches the previously set primary taxon.
This commit is contained in:
Frank West
2018-06-15 11:07:39 -07:00
committed by Maikel Linke
parent 82e3016a26
commit 09534b41e9
2 changed files with 22 additions and 0 deletions

View File

@@ -38,6 +38,7 @@ Spree::Product.class_eval do
before_validation :sanitize_permalink
before_save :add_primary_taxon_to_taxons
after_touch :touch_distributors
after_save :remove_previous_primary_taxon_from_taxons
after_save :ensure_standard_variant
after_save :update_units
after_save :refresh_products_cache
@@ -245,6 +246,11 @@ Spree::Product.class_eval do
taxons << primary_taxon unless taxons.include? primary_taxon
end
def remove_previous_primary_taxon_from_taxons
return unless primary_taxon_id_changed? && primary_taxon_id_was
taxons.destroy(primary_taxon_id_was)
end
def self.all_variant_unit_option_types
Spree::OptionType.where('name LIKE ?', 'unit_%%')
end

View File

@@ -193,6 +193,22 @@ module Spree
expect { product.delete }.to change { distributor.reload.updated_at }
end
end
it "adds the primary taxon to the product's taxon list" do
taxon = create(:taxon)
product = create(:product, primary_taxon: taxon)
expect(product.taxons).to include(taxon)
end
it "removes the previous primary taxon from the taxon list" do
original_taxon = create(:taxon)
product = create(:product, primary_taxon: original_taxon)
product.primary_taxon = create(:taxon)
product.save!
expect(product.taxons).not_to include(original_taxon)
end
end
describe "scopes" do