From 1b7ac1a252f2a06f6ee8b45814899b90587c5d8e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 7 Apr 2020 14:30:02 +0200 Subject: [PATCH] Don't re-use fat serializers when thin ones are needed. This cuts the pageload and query count in half, again. --- app/controllers/api/products_controller.rb | 6 ++--- .../api/admin/product_simple_serializer.rb | 16 +++++++++++ .../api/admin/variant_simple_serializer.rb | 27 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 app/serializers/api/admin/product_simple_serializer.rb create mode 100644 app/serializers/api/admin/variant_simple_serializer.rb diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index c0a46aca05..3182c79044 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -74,7 +74,7 @@ module Api @products = paged_products_for_producers producers - render_paged_products @products + render_paged_products @products, ::Api::Admin::ProductSimpleSerializer end # POST /api/products/:product_id/clone @@ -128,10 +128,10 @@ module Api page(params[:page]).per(params[:per_page]) end - def render_paged_products(products) + def render_paged_products(products, product_serializer = ::Api::Admin::ProductSerializer) serializer = ActiveModel::ArraySerializer.new( products, - each_serializer: ::Api::Admin::ProductSerializer + each_serializer: product_serializer ) render text: { diff --git a/app/serializers/api/admin/product_simple_serializer.rb b/app/serializers/api/admin/product_simple_serializer.rb new file mode 100644 index 0000000000..181bb33f54 --- /dev/null +++ b/app/serializers/api/admin/product_simple_serializer.rb @@ -0,0 +1,16 @@ +class Api::Admin::ProductSimpleSerializer < ActiveModel::Serializer + attributes :id, :name + + has_one :supplier, key: :producer_id, embed: :id + has_many :variants, key: :variants, serializer: Api::Admin::VariantSimpleSerializer + + def on_hand + return 0 if object.on_hand.nil? + + object.on_hand + end + + def price + object.price.nil? ? '0.0' : object.price + end +end diff --git a/app/serializers/api/admin/variant_simple_serializer.rb b/app/serializers/api/admin/variant_simple_serializer.rb new file mode 100644 index 0000000000..64aa6e433d --- /dev/null +++ b/app/serializers/api/admin/variant_simple_serializer.rb @@ -0,0 +1,27 @@ +class Api::Admin::VariantSimpleSerializer < ActiveModel::Serializer + attributes :id, :name, :import_date, + :options_text, :unit_value, :unit_description, :unit_to_display, + :display_as, :display_name, :name_to_display, + :price, :on_demand, :on_hand + + has_many :variant_overrides + + def name + if object.full_name.present? + "#{object.name} - #{object.full_name}" + else + object.name + end + end + + def on_hand + return 0 if object.on_hand.nil? + + object.on_hand + end + + def price + # Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client. + object.price.nil? ? 0.to_f : object.price + end +end