Merge pull request #10814 from Matt-Yorkley/serializer-eager-loading

Improve eager-loading on admin products page
This commit is contained in:
Maikel
2023-05-19 14:31:25 +10:00
committed by GitHub
3 changed files with 21 additions and 7 deletions

View File

@@ -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 }]
]

View File

@@ -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"

View File

@@ -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