diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 5460ecc94d..40642d12cb 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -87,6 +87,7 @@ class Enterprise < ActiveRecord::Base before_validation :set_unused_address_fields after_validation :geocode_address + after_touch :touch_distributors after_create :relate_to_owners_enterprises # TODO: Later versions of devise have a dedicated after_confirmation callback, so use that after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? } @@ -469,4 +470,8 @@ class Enterprise < ActiveRecord::Base def initialize_permalink self.permalink = Enterprise.find_available_permalink(name) end + + def touch_distributors + Enterprise.distributing_product(self.supplied_products).each(&:touch) + end end diff --git a/spec/models/enterprise_caching_spec.rb b/spec/models/enterprise_caching_spec.rb index 65ca32bb21..047fc6ab02 100644 --- a/spec/models/enterprise_caching_spec.rb +++ b/spec/models/enterprise_caching_spec.rb @@ -5,6 +5,7 @@ describe Enterprise do describe "is touched when a(n)" do let(:enterprise) { create(:distributor_enterprise, updated_at: 1.week.ago) } let(:taxon) { create(:taxon) } + let(:supplier2) { create(:supplier_enterprise) } describe "with a supplied product" do let(:product) { create(:simple_product, supplier: enterprise) } @@ -28,16 +29,47 @@ describe Enterprise do it "touches enterprise when a producer property on that product changes" do expect { producer_property.save! }.to change { enterprise.reload.updated_at } end + + it "touches enterprise when the supplier of a product changes" do + expect { + product.update_attributes!(supplier: supplier2) + }.to change { enterprise.updated_at } + end end describe "with a distributed product" do let(:product) { create(:simple_product) } let(:oc) { create(:simple_order_cycle, distributors: [enterprise], variants: [product.variants.first]) } + let(:supplier) { product.supplier } let!(:classification) { create(:classification, taxon: taxon, product: product) } + let(:property) { product.product_properties.last } + let(:producer_property) { supplier.producer_properties.last } - it "touches enterprise when a classification on that product changes" do - oc - expect { classification.save! }.to change { enterprise.reload.updated_at } + before do + product.set_property 'Organic', 'NASAA 12345' + supplier.set_producer_property 'Biodynamic', 'ASDF 4321' + end + + context "with an order cycle" do + before { oc } + + it "touches enterprise when a classification on that product changes" do + expect { classification.save! }.to change { enterprise.reload.updated_at } + end + + it "touches enterprise when a property on that product changes" do + expect { property.save! }.to change { enterprise.reload.updated_at } + end + + it "touches enterprise when a producer property on that product changes" do + expect { producer_property.save! }.to change { enterprise.reload.updated_at } + end + + it "touches enterprise when the supplier of a product changes" do + expect { + product.update_attributes!(supplier: supplier2) + }.to change { enterprise.reload.updated_at } + end end it "touches enterprise when the product's variant is added to order cycle" do