mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Encapsulate SuppliedProductBuilder
And improve EnterpriseBuilder. It was builder products twice.
This commit is contained in:
@@ -7,7 +7,7 @@ module DfcProvider
|
||||
before_action :check_enterprise
|
||||
|
||||
def show
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = SuppliedProductBuilder.supplied_product(variant)
|
||||
render json: DfcLoader.connector.export(product)
|
||||
end
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class DfcBuilder
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
product = supplied_product(variant)
|
||||
product = SuppliedProductBuilder.supplied_product(variant)
|
||||
|
||||
DataFoodConsortium::Connector::CatalogItem.new(
|
||||
id, product: product,
|
||||
@@ -16,21 +16,6 @@ class DfcBuilder
|
||||
)
|
||||
end
|
||||
|
||||
def self.supplied_product(variant)
|
||||
id = urls.enterprise_supplied_product_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
|
||||
DataFoodConsortium::Connector::SuppliedProduct.new(
|
||||
id,
|
||||
name: variant.name_to_display,
|
||||
description: variant.description,
|
||||
productType: product_type,
|
||||
quantity: QuantitativeValueBuilder.quantity(variant),
|
||||
)
|
||||
end
|
||||
|
||||
def self.offer(variant)
|
||||
# We don't have an endpoint for offers yet and this URL is only a
|
||||
# placeholder for now. The offer is actually affected by order cycle and
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
class EnterpriseBuilder < DfcBuilder
|
||||
def self.enterprise(enterprise)
|
||||
variants = VariantFetcher.new(enterprise).scope.to_a
|
||||
supplied_products = variants.map(&method(:supplied_product))
|
||||
catalog_items = variants.map(&method(:catalog_item))
|
||||
supplied_products = catalog_items.map(&:product)
|
||||
|
||||
DataFoodConsortium::Connector::Enterprise.new(
|
||||
enterprise.name
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SuppliedProductBuilder < DfcBuilder
|
||||
def self.supplied_product(variant)
|
||||
id = urls.enterprise_supplied_product_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
|
||||
DataFoodConsortium::Connector::SuppliedProduct.new(
|
||||
id,
|
||||
name: variant.name_to_display,
|
||||
description: variant.description,
|
||||
productType: product_type,
|
||||
quantity: QuantitativeValueBuilder.quantity(variant),
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require DfcProvider::Engine.root.join("spec/spec_helper")
|
||||
|
||||
describe EnterpriseBuilder do
|
||||
subject(:builder) { described_class }
|
||||
let(:enterprise) { variant.product.supplier }
|
||||
let(:variant) { create(:product, name: "Apple").variants.first }
|
||||
|
||||
describe ".enterprise" do
|
||||
it "assigns a semantic id" do
|
||||
result = builder.enterprise(enterprise)
|
||||
|
||||
expect(result.semanticId).to eq(
|
||||
"http://test.host/api/dfc-v1.7/enterprises/#{enterprise.id}"
|
||||
)
|
||||
end
|
||||
|
||||
it "assignes products" do
|
||||
result = builder.enterprise(enterprise)
|
||||
|
||||
expect(result.suppliedProducts.count).to eq 1
|
||||
expect(result.suppliedProducts[0].name).to eq "Apple"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
require DfcProvider::Engine.root.join("spec/spec_helper")
|
||||
|
||||
describe DfcBuilder do
|
||||
describe SuppliedProductBuilder do
|
||||
subject(:builder) { described_class }
|
||||
let(:variant) {
|
||||
build(:variant, id: 5).tap { |v| v.product.supplier_id = 7 }
|
||||
}
|
||||
|
||||
describe ".supplied_product" do
|
||||
it "assigns a semantic id" do
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.semanticId).to eq(
|
||||
"http://test.host/api/dfc-v1.7/enterprises/7/supplied_products/5"
|
||||
@@ -17,7 +18,7 @@ describe DfcBuilder do
|
||||
end
|
||||
|
||||
it "assigns a quantity" do
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.quantity.value).to eq 1.0
|
||||
expect(product.quantity.unit.semanticId).to eq "dfc-m:Gram"
|
||||
@@ -25,7 +26,7 @@ describe DfcBuilder do
|
||||
|
||||
it "assigns the product name by default" do
|
||||
variant.product.name = "Apple"
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.name).to eq "Apple"
|
||||
end
|
||||
@@ -33,13 +34,13 @@ describe DfcBuilder do
|
||||
it "assigns the variant name if present" do
|
||||
variant.product.name = "Apple"
|
||||
variant.display_name = "Granny Smith"
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.name).to eq "Granny Smith"
|
||||
end
|
||||
|
||||
it "assigns a product type" do
|
||||
product = DfcBuilder.supplied_product(variant)
|
||||
product = builder.supplied_product(variant)
|
||||
vegetable = DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE
|
||||
|
||||
expect(product.productType).to eq vegetable
|
||||
|
||||
Reference in New Issue
Block a user