mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #5295 from Matt-Yorkley/shopfront-optimising
Enterprise serializer optimisations
This commit is contained in:
@@ -62,10 +62,14 @@ module Api
|
||||
end
|
||||
|
||||
def supplied_taxons
|
||||
return [] unless enterprise.is_primary_producer
|
||||
|
||||
ids_to_objs data.supplied_taxons[enterprise.id]
|
||||
end
|
||||
|
||||
def supplied_properties
|
||||
return [] unless enterprise.is_primary_producer
|
||||
|
||||
(product_properties + producer_properties).uniq do |property_object|
|
||||
property_object.property.presentation
|
||||
end
|
||||
@@ -113,7 +117,7 @@ module Api
|
||||
end
|
||||
|
||||
def active
|
||||
data.active_distributor_ids.andand.include? enterprise.id
|
||||
@active ||= data.active_distributor_ids.andand.include? enterprise.id
|
||||
end
|
||||
|
||||
# Map svg icons.
|
||||
|
||||
@@ -18,7 +18,8 @@ module Api
|
||||
end
|
||||
|
||||
def active
|
||||
enterprise.ready_for_checkout? && OrderCycle.active.with_distributor(enterprise).exists?
|
||||
@active ||=
|
||||
enterprise.ready_for_checkout? && OrderCycle.active.with_distributor(enterprise).exists?
|
||||
end
|
||||
|
||||
def pickup
|
||||
@@ -73,12 +74,16 @@ module Api
|
||||
end
|
||||
|
||||
def supplied_taxons
|
||||
return [] unless enterprise.is_primary_producer
|
||||
|
||||
ActiveModel::ArraySerializer.new(
|
||||
enterprise.supplied_taxons, each_serializer: Api::TaxonSerializer
|
||||
)
|
||||
end
|
||||
|
||||
def supplied_properties
|
||||
return [] unless enterprise.is_primary_producer
|
||||
|
||||
(product_properties + producer_properties).uniq do |property_object|
|
||||
property_object.property.presentation
|
||||
end
|
||||
@@ -118,7 +123,7 @@ module Api
|
||||
private
|
||||
|
||||
def product_properties
|
||||
enterprise.supplied_products.flat_map(&:properties)
|
||||
enterprise.supplied_products.includes(:properties).flat_map(&:properties)
|
||||
end
|
||||
|
||||
def producer_properties
|
||||
|
||||
@@ -9,18 +9,35 @@ describe Api::CachedEnterpriseSerializer do
|
||||
let(:duplicate_property) { create(:property, presentation: 'One') }
|
||||
let(:different_property) { create(:property, presentation: 'Two') }
|
||||
|
||||
let(:enterprise) do
|
||||
create(:enterprise, properties: [duplicate_property, different_property])
|
||||
end
|
||||
|
||||
before do
|
||||
product = create(:product, properties: [property])
|
||||
enterprise.supplied_products << product
|
||||
end
|
||||
|
||||
it "removes duplicate product and producer properties" do
|
||||
properties = cached_enterprise_serializer.supplied_properties
|
||||
expect(properties).to eq([property, different_property])
|
||||
context "when the enterprise is a producer" do
|
||||
let(:enterprise) do
|
||||
create(:enterprise,
|
||||
is_primary_producer: true,
|
||||
properties: [duplicate_property, different_property])
|
||||
end
|
||||
|
||||
it "serializes combined product and producer properties without duplicates" do
|
||||
properties = cached_enterprise_serializer.supplied_properties
|
||||
expect(properties).to eq([property, different_property])
|
||||
end
|
||||
end
|
||||
|
||||
context "when the enterprise is not a producer" do
|
||||
let(:enterprise) do
|
||||
create(:enterprise,
|
||||
is_primary_producer: false,
|
||||
properties: [duplicate_property, different_property])
|
||||
end
|
||||
|
||||
it "does not serialize supplied properties" do
|
||||
properties = cached_enterprise_serializer.supplied_properties
|
||||
expect(properties).to eq([])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'spec_helper'
|
||||
|
||||
describe Api::EnterpriseSerializer do
|
||||
let(:serializer) { Api::EnterpriseSerializer.new enterprise, data: data }
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
let(:enterprise) { create(:distributor_enterprise, is_primary_producer: true) }
|
||||
let(:taxon) { create(:taxon) }
|
||||
let(:data) {
|
||||
OpenStruct.new(earliest_closing_times: {},
|
||||
|
||||
Reference in New Issue
Block a user