Add lookup of variants by semantic id

This commit is contained in:
Maikel Linke
2024-09-12 11:05:36 +10:00
parent c948efd9ce
commit 95e620a78b
2 changed files with 18 additions and 0 deletions

View File

@@ -170,6 +170,11 @@ module Spree
select("spree_variants.id") })
end
def self.linked_to(semantic_id)
includes(:semantic_links).references(:semantic_links)
.where(semantic_links: { semantic_id: }).first
end
def tax_category
super || TaxCategory.find_by(is_default: true)
end

View File

@@ -488,6 +488,19 @@ RSpec.describe Spree::Variant do
end
end
describe ".linked_to" do
let!(:variant_unlinked) { create(:variant) }
let!(:variant_linked) { create(:variant, semantic_links: [link]) }
let!(:variant_linked_unrelated) { create(:variant, semantic_links: [unrelated_link]) }
let(:link) { SemanticLink.new(semantic_id: "#my_precious") }
let(:unrelated_link) { SemanticLink.new(semantic_id: "#other") }
it "finds a variant by link" do
expect(Spree::Variant.linked_to("#my_precious"))
.to eq variant_linked
end
end
describe "generating the product and variant name" do
let(:product) { variant.product }