mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-10 23:07:47 +00:00
Add lookup of variants by semantic id
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user