-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from mamhoff/add-ingredients-relations
Add ingredients relations
- Loading branch information
Showing
9 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters