From bd4560cff64f00e105fc4fdb7bb5de3ac07923c0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 6 Jan 2021 13:30:19 +0000 Subject: [PATCH 1/2] Don't serialize all variant override objects where they are not needed This is only needed in one place, and only as a count, and can cause huge amounts of superfluous data to be fetched and serialized, for example in the admin products index page (where variant override data is not used at all). --- app/serializers/api/admin/variant_serializer.rb | 8 +++++--- .../admin/products/index/_products_variant.html.haml | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/serializers/api/admin/variant_serializer.rb b/app/serializers/api/admin/variant_serializer.rb index bffd5b8168..1be17e1c19 100644 --- a/app/serializers/api/admin/variant_serializer.rb +++ b/app/serializers/api/admin/variant_serializer.rb @@ -5,11 +5,9 @@ module Api class VariantSerializer < ActiveModel::Serializer attributes :id, :name, :producer_name, :image, :sku, :import_date, :options_text, :unit_value, :unit_description, :unit_to_display, - :display_as, :display_name, :name_to_display, + :display_as, :display_name, :name_to_display, :variant_overrides_count, :price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name - has_many :variant_overrides - def name if object.full_name.present? "#{object.name} - #{object.full_name}" @@ -55,6 +53,10 @@ module Api object.stock_items.first.stock_location.name end + + def variant_overrides_count + object.variant_overrides.count + end end end end diff --git a/app/views/spree/admin/products/index/_products_variant.html.haml b/app/views/spree/admin/products/index/_products_variant.html.haml index 584e9f3610..61db4e0ea7 100644 --- a/app/views/spree/admin/products/index/_products_variant.html.haml +++ b/app/views/spree/admin/products/index/_products_variant.html.haml @@ -29,6 +29,6 @@ %td.actions %a{ 'ng-click' => 'editWarn(product,variant)', :class => "edit-variant icon-edit no-text", 'ng-show' => "variantSaved(variant)", 'ofn-with-tip' => t(:edit) } %td.actions - %span.icon-warning-sign{ 'ng-if' => 'variant.variant_overrides', 'ofn-with-tip' => "{{ 'spree.admin.products.index.products_variant.variant_has_n_overrides' | t:{n: variant.variant_overrides.length} }}" } + %span.icon-warning-sign{ 'ng-if' => 'variant.variant_overrides_count > 0', 'ofn-with-tip' => "{{ 'spree.admin.products.index.products_variant.variant_has_n_overrides' | t:{n: variant.variant_overrides_count} }}" } %td.actions %a{ 'ng-click' => 'deleteVariant(product,variant)', "ng-class" => '{disabled: product.variants.length < 2}', :class => "delete-variant icon-trash no-text", 'ofn-with-tip' => t(:remove) } From 9633ddf7ce1ee7b6e582c80743d7401b7b4e37f1 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 13 Jan 2021 18:19:16 +0000 Subject: [PATCH 2/2] Add test for variant override symbol and tooltip --- .../admin/bulk_product_update_spec.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index 3692cb26c1..09fd5f3ea0 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -168,6 +168,28 @@ feature ' expect(page).to have_field "variant_display_as", with: "bag" expect(page).to have_field "variant_display_as", with: "bin" end + + context "with variant overrides" do + let!(:product) { create(:product) } + let(:variant) { product.variants.first } + let(:hub) { create(:distributor_enterprise) } + let!(:override) { create(:variant_override, variant: variant, hub: hub ) } + let(:variant_overrides_tip) { + I18n.t('spree.admin.products.index.products_variant.variant_has_n_overrides', n: 1) + } + + it "displays an icon indicating a variant has overrides" do + visit spree.admin_products_path + + find("a.view-variants").click + + within "tr#v_#{variant.id}" do + expect(page).to have_selector( + "span.icon-warning-sign[data-powertip='#{variant_overrides_tip}']" + ) + end + end + end end scenario "creating a new product" do