Skip to content

Commit

Permalink
Merge pull request #108 from mamhoff/add-ingredients-relations
Browse files Browse the repository at this point in the history
Add ingredients relations
  • Loading branch information
tvdeyen authored May 15, 2024
2 parents 03f8f6d + 3f6643f commit 02a5a92
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/decorators/models/spree/spree_product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

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

::Spree::Product.prepend self
end
end
12 changes: 12 additions & 0 deletions app/decorators/models/spree/spree_taxon_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

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

::Spree::Taxon.prepend self
end
end
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
18 changes: 18 additions & 0 deletions app/models/alchemy_solidus/touch_alchemy_ingredients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module AlchemySolidus
module TouchAlchemyIngredients
extend ActiveSupport::Concern

included do
after_update :touch_alchemy_ingredients
after_touch :touch_alchemy_ingredients
end

private

def touch_alchemy_ingredients
alchemy_ingredients.each(&:touch)
end
end
end
1 change: 1 addition & 0 deletions lib/alchemy/solidus/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module Alchemy
module Solidus
class Engine < ::Rails::Engine
include SolidusSupport::EngineExtensions
engine_name "alchemy_solidus"

config.to_prepare do
Expand Down
35 changes: 35 additions & 0 deletions spec/models/spree/product_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::Product, 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::SpreeProduct.create!(element: element, role: "product", related_object: product) }
let(:product) { create(:product) }

it "invalidates the cache on update" do
travel_to 5.minutes.from_now do
current_time = Time.current
expect { product.reload.update!(name: "New name") }.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 { product.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
35 changes: 35 additions & 0 deletions spec/models/spree/taxon_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::Taxon, 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::SpreeTaxon.create!(element: element, role: "taxon", related_object: taxon) }
let(:taxon) { create(:taxon) }

it "invalidates the cache on update" do
travel_to 5.minutes.from_now do
current_time = Time.current
expect { taxon.reload.update!(name: "New name") }.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 { taxon.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
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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Alchemy::TestSupport::IntegrationHelpers, type: :feature
config.include Alchemy::TestSupport::CapybaraHelpers, type: :feature
config.include ActiveSupport::Testing::TimeHelpers, type: :model

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down

0 comments on commit 02a5a92

Please sign in to comment.