Add basic match on the root level Product Types

Currently anything but the leaf level is modelled as a
DataFoodConsortium::Connector::SKOSInstance, which isn't supported
by the connector as "hasType" for a product. The lead level is modelled
by DataFoodConsortium::Connector::SKOSConcept which is supported by
connector. On top of is `#narrowers`, `#broaders`and `#prefLabels`
aren't set making it very difficult to travers the Product Type tree.
This commit is contained in:
Gaetan Craig-Riou
2023-10-31 14:35:13 +11:00
parent 652c7a563c
commit 403386ea09
2 changed files with 46 additions and 12 deletions

View File

@@ -11,19 +11,13 @@ class SuppliedProductBuilder < DfcBuilder
id,
name: variant.product_and_full_name,
description: variant.description,
productType: product_type,
productType: product_type(variant),
quantity: QuantitativeValueBuilder.quantity(variant),
spree_product_id: variant.product.id,
image_url: variant.product&.image&.url(:product)
)
end
# OFN product categories (taxons) are currently not standardised.
# This is just a dummy value for demos.
def self.product_type
DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE
end
def self.import_variant(supplied_product)
product_id = supplied_product.spree_product_id
@@ -42,6 +36,7 @@ class SuppliedProductBuilder < DfcBuilder
end
end
# TODO fix the taxon here
def self.import_product(supplied_product)
Spree::Product.new(
name: supplied_product.name,
@@ -62,4 +57,17 @@ class SuppliedProductBuilder < DfcBuilder
QuantitativeValueBuilder.apply(supplied_product.quantity, variant.product)
variant.unit_value = variant.product.unit_value
end
def self.product_type(variant)
taxon_name = variant.product.primary_taxon&.dfc_name
return nil if taxon_name.nil?
root_product_types = DfcLoader.connector.PRODUCT_TYPES.methods(false).sort
search = root_product_types.index(taxon_name.upcase.to_sym)
return nil if search.nil?
DfcLoader.connector.PRODUCT_TYPES.public_send(root_product_types[search])
end
end

View File

@@ -7,8 +7,12 @@ describe SuppliedProductBuilder do
subject(:builder) { described_class }
let(:variant) {
build(:variant, id: 5).tap { |v| v.product.supplier_id = 7 }
build(:variant, id: 5).tap do |v|
v.product.supplier_id = 7
v.product.primary_taxon = taxon
end
}
let(:taxon) { build(:taxon, name: "Drink", dfc_name: "drink") }
describe ".supplied_product" do
it "assigns a semantic id" do
@@ -41,11 +45,33 @@ describe SuppliedProductBuilder do
expect(product.name).to eq "Apple - Granny Smith"
end
it "assigns a product type" do
product = builder.supplied_product(variant)
vegetable = DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE
context "product_type mapping" do
it "assigns a product type" do
product = builder.supplied_product(variant)
drink = DfcLoader.connector.PRODUCT_TYPES.DRINK
expect(product.productType).to eq vegetable
expect(product.productType).to eq drink
end
context "with non existing product type" do
let(:taxon) { build(:taxon, name: "other", dfc_name: "other") }
it "returns nil" do
product = builder.supplied_product(variant)
expect(product.productType).to be_nil
end
end
context "when no taxon set" do
let(:taxon) { nil }
it "returns nil" do
product = builder.supplied_product(variant)
expect(product.productType).to be_nil
end
end
end
it "assigns an image_url type" do