mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
Serialize taxons and relatives in expected format
This commit is contained in:
@@ -3,7 +3,6 @@ class Api::EnterpriseSerializer < ActiveModel::Serializer
|
||||
Api::IdSerializer
|
||||
|
||||
def serializable_hash
|
||||
|
||||
cached_serializer_hash.merge uncached_serializer_hash
|
||||
end
|
||||
|
||||
@@ -51,11 +50,11 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer
|
||||
|
||||
|
||||
def taxons
|
||||
options[:data].distributed_taxons[object.id]
|
||||
ids_to_objs options[:data].distributed_taxons[object.id]
|
||||
end
|
||||
|
||||
def supplied_taxons
|
||||
options[:data].supplied_taxons[object.id]
|
||||
ids_to_objs options[:data].supplied_taxons[object.id]
|
||||
end
|
||||
|
||||
def pickup
|
||||
@@ -90,12 +89,12 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer
|
||||
|
||||
def producers
|
||||
relatives = options[:data].relatives[object.id]
|
||||
relatives ? relatives[:producers] : []
|
||||
relatives ? ids_to_objs(relatives[:producers]) : []
|
||||
end
|
||||
|
||||
def hubs
|
||||
relatives = options[:data].relatives[object.id]
|
||||
relatives ? relatives[:distributors] : []
|
||||
relatives ? ids_to_objs(relatives[:distributors]) : []
|
||||
end
|
||||
|
||||
# Map svg icons.
|
||||
@@ -135,4 +134,11 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer
|
||||
}
|
||||
icon_fonts[object.category]
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def ids_to_objs(ids)
|
||||
ids.andand.map { |id| {id: id} }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,20 +1,31 @@
|
||||
#require 'spec_helper'
|
||||
|
||||
describe Api::EnterpriseSerializer do
|
||||
let(:serializer) { Api::EnterpriseSerializer.new enterprise, data: data }
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
let(:taxon) { create(:taxon) }
|
||||
let(:data_class) { Struct.new(:earliest_closing_times, :active_distributors,
|
||||
:distributed_taxons, :supplied_taxons,
|
||||
:shipping_method_services, :relatives) }
|
||||
let(:data) { data_class.new({}, [], {}, {}, {}, {producers: [], distributors: []}) }
|
||||
let(:data) { OpenStruct.new(earliest_closing_times: {},
|
||||
active_distributors: [],
|
||||
distributed_taxons: {enterprise.id => [123]},
|
||||
supplied_taxons: {enterprise.id => [456]},
|
||||
shipping_method_services: {},
|
||||
relatives: {enterprise.id => {producers: [123], distributors: [456]}}) }
|
||||
|
||||
it "serializes an enterprise" do
|
||||
serializer = Api::EnterpriseSerializer.new enterprise, data: data
|
||||
serializer.to_json.should match enterprise.name
|
||||
end
|
||||
|
||||
it "will render urls" do
|
||||
serializer = Api::EnterpriseSerializer.new enterprise, data: data
|
||||
it "serializes taxons as ids only" do
|
||||
serializer.serializable_hash[:taxons].should == [{id: 123}]
|
||||
serializer.serializable_hash[:supplied_taxons].should == [{id: 456}]
|
||||
end
|
||||
|
||||
it "serializes producers and hubs as ids only" do
|
||||
serializer.serializable_hash[:producers].should == [{id: 123}]
|
||||
serializer.serializable_hash[:hubs].should == [{id: 456}]
|
||||
end
|
||||
|
||||
it "serializes icons" do
|
||||
serializer.to_json.should match "map_005-hub.svg"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user