mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #10814 from Matt-Yorkley/serializer-eager-loading
Improve eager-loading on admin products page
This commit is contained in:
@@ -116,7 +116,7 @@ module Api
|
||||
|
||||
def product_query_includes
|
||||
[
|
||||
master: [:images],
|
||||
master: { images: { attachment_attachment: :blob } },
|
||||
variants: [:default_price, :stock_locations, :stock_items, :variant_overrides,
|
||||
{ option_values: :option_type }]
|
||||
]
|
||||
|
||||
@@ -5,12 +5,26 @@ module Api
|
||||
class ProductSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name,
|
||||
:inherits_properties, :on_hand, :price, :available_on, :permalink_live,
|
||||
:tax_category_id, :import_date, :image_url, :thumb_url
|
||||
:tax_category_id, :import_date, :image_url, :thumb_url, :variants, :master
|
||||
|
||||
has_one :supplier, key: :producer_id, embed: :id
|
||||
has_one :primary_taxon, key: :category_id, embed: :id
|
||||
has_many :variants, key: :variants, serializer: Api::Admin::VariantSerializer
|
||||
has_one :master, serializer: Api::Admin::VariantSerializer
|
||||
|
||||
def variants
|
||||
ActiveModel::ArraySerializer.new(
|
||||
object.variants,
|
||||
each_serializer: Api::Admin::VariantSerializer,
|
||||
image: thumb_url,
|
||||
stock_location: Spree::StockLocation.first
|
||||
)
|
||||
end
|
||||
|
||||
def master
|
||||
Api::Admin::VariantSerializer.new(
|
||||
object.master,
|
||||
image: thumb_url
|
||||
)
|
||||
end
|
||||
|
||||
def image_url
|
||||
object.images.first&.url(:product) || "/noimage/product.png"
|
||||
|
||||
@@ -33,7 +33,7 @@ module Api
|
||||
end
|
||||
|
||||
def image
|
||||
object.product.images.first&.url(:mini)
|
||||
options[:image] || object.product.images.first&.url(:mini)
|
||||
end
|
||||
|
||||
def in_stock
|
||||
@@ -43,13 +43,13 @@ module Api
|
||||
def stock_location_id
|
||||
return if object.stock_items.empty?
|
||||
|
||||
object.stock_items.first.stock_location.id
|
||||
options[:stock_location]&.id || object.stock_items.first.stock_location.id
|
||||
end
|
||||
|
||||
def stock_location_name
|
||||
return if object.stock_items.empty?
|
||||
|
||||
object.stock_items.first.stock_location.name
|
||||
options[:stock_location]&.name || object.stock_items.first.stock_location.name
|
||||
end
|
||||
|
||||
def variant_overrides_count
|
||||
|
||||
Reference in New Issue
Block a user