Skip to content

Commit 7f9888f

Browse files
authored
Merge pull request #109 from tvdeyen/touch-taxons
Touch taxons individually after product update
2 parents 378f7ca + ca588fd commit 7f9888f

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

app/decorators/models/spree/spree_product_decorator.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ def self.prepended(base)
77
base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify
88
end
99

10+
private
11+
12+
# Overwritten Solidus' default behavior
13+
#
14+
# The Solidus implementation did not trigger `touch` on taxons, but
15+
# updated the `updated_at` timestamp in an `update_all`.
16+
#
17+
# Since we want to invalidate ingredient spree taxons cache as well
18+
# we need to use `touch` here and use the `after_touch` callback of
19+
# Spree::Taxon
20+
#
21+
def touch_taxons
22+
taxons.each(&:touch)
23+
end
24+
1025
::Spree::Product.prepend self
1126
end
1227
end

spec/models/spree/product_spec.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,40 @@
99
let(:page) { create(:alchemy_page) }
1010
let(:page_version) { create(:alchemy_page_version, page: page) }
1111
let(:element) { create(:alchemy_element, page_version: page_version) }
12-
let!(:ingredient) { Alchemy::Ingredients::SpreeProduct.create!(element: element, role: "product", related_object: product) }
1312
let(:product) { create(:product) }
1413

15-
it "invalidates the cache on update" do
16-
travel_to 5.minutes.from_now do
17-
current_time = Time.current
18-
expect { product.reload.update!(name: "New name") }.to change { ingredient.reload.updated_at }.to(current_time)
19-
expect(element.reload.updated_at).to eq(current_time)
20-
expect(page_version.reload.updated_at).to eq(current_time)
21-
expect(page.reload.updated_at).to eq(current_time)
14+
context "if assigned to ingredient spree taxon" do
15+
let!(:ingredient) { Alchemy::Ingredients::SpreeProduct.create!(element: element, role: "product", related_object: product) }
16+
17+
it "invalidates the cache on update" do
18+
travel_to 5.minutes.from_now do
19+
current_time = Time.current
20+
expect { product.reload.update!(name: "New name") }.to change { ingredient.reload.updated_at }.to(current_time)
21+
expect(element.reload.updated_at).to eq(current_time)
22+
expect(page_version.reload.updated_at).to eq(current_time)
23+
expect(page.reload.updated_at).to eq(current_time)
24+
end
25+
end
26+
27+
it "invalidates the cache on touch" do
28+
travel_to 5.minutes.from_now do
29+
current_time = Time.current
30+
expect { product.reload.touch }.to change { ingredient.reload.updated_at }.to(current_time)
31+
expect(element.reload.updated_at).to eq(current_time)
32+
expect(page_version.reload.updated_at).to eq(current_time)
33+
expect(page.reload.updated_at).to eq(current_time)
34+
end
2235
end
2336
end
2437

25-
it "invalidates the cache on touch" do
26-
travel_to 5.minutes.from_now do
27-
current_time = Time.current
28-
expect { product.reload.touch }.to change { ingredient.reload.updated_at }.to(current_time)
29-
expect(element.reload.updated_at).to eq(current_time)
30-
expect(page_version.reload.updated_at).to eq(current_time)
31-
expect(page.reload.updated_at).to eq(current_time)
38+
context "if assigned to taxon that is assigned to ingredient spree taxon" do
39+
let(:taxon) { create(:taxon) }
40+
let(:product) { create(:product, taxons: [taxon]) }
41+
42+
let!(:ingredient) { Alchemy::Ingredients::SpreeTaxon.create!(element: element, role: "taxon", related_object: taxon) }
43+
44+
it "touches ingredient spree taxons elements" do
45+
expect { product.reload.touch }.to change { element.reload.updated_at }
3246
end
3347
end
3448
end

0 commit comments

Comments
 (0)