mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Per review, match on the product type URI instead of name
Fox export only
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
class AddDfcNameToSpreeTaxons < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
add_column :spree_taxons, :dfc_name, :string
|
||||
add_column :spree_taxons, :dfc_id, :string
|
||||
end
|
||||
end
|
||||
|
||||
@@ -881,7 +881,7 @@ ActiveRecord::Schema[7.0].define(version: 20231003000823494) do
|
||||
t.string "meta_title", limit: 255
|
||||
t.string "meta_description", limit: 255
|
||||
t.string "meta_keywords", limit: 255
|
||||
t.string "dfc_name"
|
||||
t.string "dfc_id"
|
||||
t.index ["parent_id"], name: "index_taxons_on_parent_id"
|
||||
t.index ["permalink"], name: "index_taxons_on_permalink"
|
||||
t.index ["taxonomy_id"], name: "index_taxons_on_taxonomy_id"
|
||||
|
||||
@@ -61,16 +61,15 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
end
|
||||
|
||||
def self.product_type(variant)
|
||||
taxon_name = variant.product.primary_taxon&.dfc_name
|
||||
taxon_dfc_id = variant.product.primary_taxon&.dfc_id
|
||||
|
||||
return nil if taxon_name.nil?
|
||||
return nil if taxon_dfc_id.nil?
|
||||
|
||||
populate_product_types if PRODUCT_TYPES.empty?
|
||||
|
||||
name = taxon_name.downcase.gsub(" ", "_").to_sym
|
||||
return nil if PRODUCT_TYPES[name].nil?
|
||||
return nil if PRODUCT_TYPES[taxon_dfc_id].nil?
|
||||
|
||||
call_dfc_product_type(PRODUCT_TYPES[name])
|
||||
call_dfc_product_type(PRODUCT_TYPES[taxon_dfc_id])
|
||||
end
|
||||
|
||||
def self.populate_product_types
|
||||
@@ -83,10 +82,12 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
def self.record_type(stack, product_type)
|
||||
name = product_type.to_s
|
||||
current_stack = stack.dup.push(name)
|
||||
PRODUCT_TYPES[name.downcase.to_sym] = current_stack
|
||||
|
||||
type = call_dfc_product_type(current_stack)
|
||||
|
||||
id = type.semanticId
|
||||
PRODUCT_TYPES[id] = current_stack
|
||||
|
||||
# Narrower product types are defined as class method on the current product type object
|
||||
narrowers = type.methods(false).sort
|
||||
|
||||
@@ -111,8 +112,8 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
|
||||
def self.taxon(supplied_product)
|
||||
# We use english locale, might need to make this configurable
|
||||
dfc_name = supplied_product.productType.prefLabels[:en].downcase
|
||||
taxon = Spree::Taxon.find_by(dfc_name: )
|
||||
dfc_id = supplied_product.productType.prefLabels[:en].downcase
|
||||
taxon = Spree::Taxon.find_by(dfc_id: )
|
||||
|
||||
return taxon if taxon.present?
|
||||
|
||||
|
||||
@@ -17,7 +17,14 @@ describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml",
|
||||
:base_product,
|
||||
id: 90_000, supplier: enterprise, name: "Apple", description: "Red",
|
||||
variants: [variant],
|
||||
primary_taxon: build(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable"),
|
||||
primary_taxon: non_local_vegetable
|
||||
)
|
||||
}
|
||||
let(:non_local_vegetable) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Non Local Vegetable",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "AR") }
|
||||
|
||||
@@ -29,7 +29,14 @@ describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc:
|
||||
:product_with_image,
|
||||
id: 90_000, supplier: enterprise, name: "Apple", description: "Round",
|
||||
variants: [variant],
|
||||
primary_taxon: build(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable"),
|
||||
primary_taxon: non_local_vegetable
|
||||
)
|
||||
}
|
||||
let(:non_local_vegetable) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Non Local Vegetable",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "APP") }
|
||||
|
||||
@@ -15,9 +15,20 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1) }
|
||||
let(:taxon) { build(:taxon, name: "Processed Vegetable", dfc_name: "processed vegetable") }
|
||||
let(:taxon) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Processed Vegetable",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#processed-vegetable"
|
||||
)
|
||||
}
|
||||
|
||||
let!(:non_local_vegetable) {
|
||||
create(:taxon, name: "Non Local Vegetable", dfc_name: "non local vegetable")
|
||||
create(
|
||||
:taxon,
|
||||
name: "Non Local Vegetable",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
|
||||
)
|
||||
}
|
||||
|
||||
before { login_as user }
|
||||
@@ -173,7 +184,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto
|
||||
|
||||
put "Update SuppliedProduct" do
|
||||
let!(:drink_taxon) {
|
||||
create(:taxon, name: "Drink", dfc_name: "drink")
|
||||
create(:taxon, name: "Drink", dfc_id: "drink")
|
||||
}
|
||||
|
||||
consumes "application/json"
|
||||
|
||||
@@ -12,7 +12,13 @@ describe SuppliedProductBuilder do
|
||||
v.product.primary_taxon = taxon
|
||||
end
|
||||
}
|
||||
let(:taxon) { build(:taxon, name: "Drink", dfc_name: "drink") }
|
||||
let(:taxon) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Drink",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#drink"
|
||||
)
|
||||
}
|
||||
|
||||
describe ".supplied_product" do
|
||||
it "assigns a semantic id" do
|
||||
@@ -55,7 +61,13 @@ describe SuppliedProductBuilder do
|
||||
end
|
||||
|
||||
context "with second level product type" do
|
||||
let(:taxon) { build(:taxon, name: "Soft Drink", dfc_name: "Soft drink") }
|
||||
let(:taxon) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Soft Drink",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#soft-drink"
|
||||
)
|
||||
}
|
||||
|
||||
it "assigns a second level product type" do
|
||||
soft_drink = DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK
|
||||
@@ -65,7 +77,13 @@ describe SuppliedProductBuilder do
|
||||
end
|
||||
|
||||
context "with leaf level product type" do
|
||||
let(:taxon) { build(:taxon, name: "Lemonade", dfc_name: "lemonade") }
|
||||
let(:taxon) {
|
||||
build(
|
||||
:taxon,
|
||||
name: "Lemonade",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#lemonade"
|
||||
)
|
||||
}
|
||||
|
||||
it "assigns a leaf level product type" do
|
||||
lemonade = DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK.LEMONADE
|
||||
@@ -75,7 +93,7 @@ describe SuppliedProductBuilder do
|
||||
end
|
||||
|
||||
context "with non existing product type" do
|
||||
let(:taxon) { build(:taxon, name: "other", dfc_name: "other") }
|
||||
let(:taxon) { build(:taxon, name: "other", dfc_id: "other") }
|
||||
|
||||
it "returns nil" do
|
||||
expect(product.productType).to be_nil
|
||||
@@ -117,7 +135,7 @@ describe SuppliedProductBuilder do
|
||||
)
|
||||
end
|
||||
let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE }
|
||||
let!(:taxon) { create(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable") }
|
||||
let!(:taxon) { create(:taxon, name: "Non local vegetable", dfc_id: "non local vegetable") }
|
||||
|
||||
it "creates a new Spree::Product" do
|
||||
product = builder.import_product(supplied_product)
|
||||
|
||||
Reference in New Issue
Block a user