Skip to content

Commit cdab571

Browse files
authored
Merge pull request #147 from AlchemyCMS/use-relatable-resource
Use Alchemy::RelatableResource
2 parents 503a875 + b9154e4 commit cdab571

12 files changed

Lines changed: 40 additions & 111 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ jobs:
2121
- "3.4"
2222
- "4.0"
2323
alchemy:
24-
- "8.1-stable"
25-
- "8.2-stable"
24+
- "8.3-stable"
2625
- "main"
2726
solidus:
2827
- "v4.5"

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gem "solidus_core", github: "solidusio/solidus", branch: solidus_branch
55
gem "solidus_backend", github: "solidusio/solidus", branch: solidus_branch
66
gem "solidus_frontend", github: "solidusio/solidus_frontend", branch: "main"
77

8-
alchemy_branch = ENV.fetch("ALCHEMY_BRANCH", "8.2-stable")
8+
alchemy_branch = ENV.fetch("ALCHEMY_BRANCH", "8.3-stable")
99
gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: alchemy_branch
1010
gem "alchemy-devise", github: "AlchemyCMS/alchemy-devise", branch: alchemy_branch
1111

alchemy-solidus.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
1414
gem.require_paths = ["lib"]
1515
gem.version = Alchemy::Solidus::VERSION
1616

17-
gem.add_dependency("alchemy_cms", [">= 8.1.0.a", "< 9"])
17+
gem.add_dependency("alchemy_cms", [">= 8.3.0", "< 9"])
1818
gem.add_dependency("solidus_api", [">= 4.0.0", "< 5"])
1919
gem.add_dependency("solidus_core", [">= 4.0.0", "< 5"])
2020
gem.add_dependency("solidus_backend", [">= 4.0.0", "< 5"])

app/jobs/alchemy/solidus/invalidate_elements_cache_job.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ class InvalidateElementsCacheJob < BaseJob
44
queue_as :default
55

66
def perform(model_name, id)
7+
Alchemy::Deprecation.warn <<~WARN
8+
Alchemy::Solidus::InvalidateElementsCacheJob is deprecated and will be removed in 9.0.
9+
Please use Alchemy::InvalidateElementsCacheJob instead.
10+
WARN
11+
712
now = Time.current
813

914
element_ids = model(model_name)

app/patches/models/alchemy/solidus/spree_product_patch.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ module Alchemy
44
module Solidus
55
module SpreeProductPatch
66
def self.prepended(base)
7-
# Solidus runs touch callbacks on product after_save
8-
base.after_touch :touch_alchemy_ingredients
97
base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify
108
end
119

@@ -24,13 +22,9 @@ def touch_taxons
2422
taxons.each(&:touch)
2523
end
2624

27-
# Touch all elements that have this product assigned to one of its ingredients.
28-
# This cascades to all parent elements and pages as well.
29-
def touch_alchemy_ingredients
30-
InvalidateElementsCacheJob.perform_later("SpreeProduct", id)
31-
end
32-
3325
::Spree::Product.prepend self
3426
end
3527
end
28+
29+
::Spree::Product.include RelatableResource
3630
end

app/patches/models/alchemy/solidus/spree_taxon_patch.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@ module Alchemy
44
module Solidus
55
module SpreeTaxonPatch
66
def self.prepended(base)
7-
base.after_update :touch_alchemy_ingredients
8-
base.after_touch :touch_alchemy_ingredients
97
base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeTaxon", as: :related_object, dependent: :nullify
108
end
119

1210
::Spree::Taxon.prepend self
13-
14-
private
15-
16-
# Touch all elements that have this taxon assigned to one of its ingredients.
17-
# This cascades to all parent elements and pages as well.
18-
def touch_alchemy_ingredients
19-
InvalidateElementsCacheJob.perform_later("SpreeTaxon", id)
20-
end
2111
end
2212
end
13+
::Spree::Taxon.include RelatableResource
2314
end

app/patches/models/alchemy/solidus/spree_variant_patch.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@ module Alchemy
44
module Solidus
55
module SpreeVariantPatch
66
def self.prepended(base)
7-
base.after_update :touch_alchemy_ingredients
8-
base.after_touch :touch_alchemy_ingredients
97
base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify
108
end
119

1210
::Spree::Variant.prepend self
13-
14-
private
15-
16-
# Touch all elements that have this variant assigned to one of its ingredients.
17-
# This cascades to all parent elements and pages as well.
18-
def touch_alchemy_ingredients
19-
InvalidateElementsCacheJob.perform_later("SpreeVariant", id)
20-
end
2111
end
2212
end
13+
14+
::Spree::Variant.include RelatableResource
2315
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FactoryBot.define do
2+
%w[
3+
product
4+
taxon
5+
variant
6+
].each do |ingredient|
7+
factory :"alchemy_ingredient_spree_#{ingredient}", class: "Alchemy::Ingredients::Spree#{ingredient.classify}" do
8+
role { ingredient }
9+
type { "Alchemy::Ingredients::Spree#{ingredient.classify}" }
10+
association :element, name: ingredient, factory: :alchemy_element
11+
end
12+
end
13+
end

spec/models/spree/product_spec.rb

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
# frozen_string_literal: true
22

33
require "rails_helper"
4+
require "alchemy/test_support/relatable_resource_examples"
45

56
RSpec.describe Spree::Product, type: :model do
67
it { is_expected.to have_many(:alchemy_ingredients) }
78

8-
describe "cache invalidation" do
9-
let(:page) { create(:alchemy_page) }
10-
let(:page_version) { create(:alchemy_page_version, page: page) }
11-
let(:element) { create(:alchemy_element, page_version: page_version) }
12-
let(:product) { create(:product) }
13-
14-
context "if assigned to ingredient spree product" 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-
expect {
19-
product.update!(name: "New name")
20-
}.to enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeProduct", product.id)
21-
end
22-
23-
it "invalidates the cache on touch" do
24-
expect {
25-
product.touch
26-
}.to enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeProduct", product.id)
27-
end
28-
end
29-
30-
context "if assigned to taxon that is assigned to ingredient spree taxon" do
31-
let(:taxon) { create(:taxon) }
32-
let!(:product) { create(:product, taxons: [taxon]) }
33-
34-
let!(:ingredient) { Alchemy::Ingredients::SpreeTaxon.create!(element: element, role: "taxon", related_object: taxon) }
35-
36-
it "touches ingredient spree taxons elements" do
37-
expect {
38-
product.touch
39-
}.to enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeTaxon", taxon.id).and(
40-
enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeProduct", product.id)
41-
)
42-
end
43-
end
44-
end
9+
it_behaves_like "a relatable resource",
10+
resource_factory_name: "product",
11+
ingredient_factory_name: "alchemy_ingredient_spree_product"
4512
end

spec/models/spree/taxon_spec.rb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
# frozen_string_literal: true
22

33
require "rails_helper"
4+
require "alchemy/test_support/relatable_resource_examples"
45

56
RSpec.describe Spree::Taxon, type: :model do
67
it { is_expected.to have_many(:alchemy_ingredients) }
78

8-
describe "cache invalidation" do
9-
let(:page) { create(:alchemy_page) }
10-
let(:page_version) { create(:alchemy_page_version, page: page) }
11-
let(:element) { create(:alchemy_element, page_version: page_version) }
12-
let!(:ingredient) { Alchemy::Ingredients::SpreeTaxon.create!(element: element, role: "taxon", related_object: taxon) }
13-
let(:taxon) { create(:taxon) }
14-
15-
it "invalidates the cache on update" do
16-
expect {
17-
taxon.update!(name: "New name")
18-
}.to enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeTaxon", taxon.id)
19-
end
20-
21-
it "invalidates the cache on touch" do
22-
expect {
23-
taxon.touch
24-
}.to enqueue_job(Alchemy::Solidus::InvalidateElementsCacheJob).with("SpreeTaxon", taxon.id)
25-
end
26-
end
9+
it_behaves_like "a relatable resource",
10+
resource_factory_name: "taxon",
11+
ingredient_factory_name: "alchemy_ingredient_spree_taxon"
2712
end

0 commit comments

Comments
 (0)