Skip to content

Commit

Permalink
Add Spree::Variant#alchemy_ingredients relation
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed May 15, 2024
1 parent 21c815e commit 3f6643f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/decorators/models/spree/spree_variant_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Spree
module SpreeVariantDecorator
def self.prepended(base)
base.include AlchemySolidus::TouchAlchemyIngredients
base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify
end

::Spree::Variant.prepend self
end
end
35 changes: 35 additions & 0 deletions spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Spree::Variant, type: :model do
it { is_expected.to have_many(:alchemy_ingredients) }

describe "cache invalidation" do
let(:page) { create(:alchemy_page) }
let(:page_version) { create(:alchemy_page_version, page: page) }
let(:element) { create(:alchemy_element, page_version: page_version) }
let!(:ingredient) { Alchemy::Ingredients::SpreeVariant.create!(element: element, role: "variant", related_object: variant) }
let(:variant) { create(:variant) }

it "invalidates the cache on update" do
travel_to 5.minutes.from_now do
current_time = Time.current
expect { variant.reload.update!(price: 30) }.to change { ingredient.reload.updated_at }.to(current_time)
expect(element.reload.updated_at).to eq(current_time)
expect(page_version.reload.updated_at).to eq(current_time)
expect(page.reload.updated_at).to eq(current_time)
end
end

it "invalidates the cache on touch" do
travel_to 5.minutes.from_now do
current_time = Time.current
expect { variant.reload.touch }.to change { ingredient.reload.updated_at }.to(current_time)
expect(element.reload.updated_at).to eq(current_time)
expect(page_version.reload.updated_at).to eq(current_time)
expect(page.reload.updated_at).to eq(current_time)
end
end
end
end

0 comments on commit 3f6643f

Please sign in to comment.