From 7e6259da31297ac29035cf45b8015714ae1a780d Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 12:31:07 +0100 Subject: [PATCH] Replace admin/variants/search rabl template with existing admin variant serializer with a few added attributes This commit fixes a bug as the image of the variatn was not being rendered, we need to fetch the image of the product, not the image of the variant. --- .../directives/variant_autocomplete.js.coffee | 2 -- .../admin/variants_controller_decorator.rb | 1 + .../api/admin/variant_serializer.rb | 18 +++++++++++++++++- .../spree/admin/variants/_autocomplete.js.erb | 9 +-------- config/locales/en.yml | 3 ++- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee b/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee index c8c462dadb..dd8c9247eb 100644 --- a/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee +++ b/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee @@ -27,8 +27,6 @@ angular.module("admin.utils").directive "variantAutocomplete", ($timeout) -> window.variants = data # this is how spree auto complete JS code picks up variants results: data formatResult: (variant) -> - if variant["images"][0] != undefined && variant["images"][0].image != undefined - variant.image = variant.images[0].image.mini_url variantTemplate variant: variant formatSelection: (variant) -> element.parent().children(".options_placeholder").html variant.options_text diff --git a/app/controllers/spree/admin/variants_controller_decorator.rb b/app/controllers/spree/admin/variants_controller_decorator.rb index 7a519701f9..6577f6c6c6 100644 --- a/app/controllers/spree/admin/variants_controller_decorator.rb +++ b/app/controllers/spree/admin/variants_controller_decorator.rb @@ -18,6 +18,7 @@ Spree::Admin::VariantsController.class_eval do def search scoper = OpenFoodNetwork::ScopeVariantsForSearch.new(params) @variants = scoper.search + render json: @variants, each_serializer: Api::Admin::VariantSerializer end def destroy diff --git a/app/serializers/api/admin/variant_serializer.rb b/app/serializers/api/admin/variant_serializer.rb index 9be1b3662b..ad08a3edd6 100644 --- a/app/serializers/api/admin/variant_serializer.rb +++ b/app/serializers/api/admin/variant_serializer.rb @@ -1,8 +1,16 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer attributes :id, :options_text, :unit_value, :unit_description, :unit_to_display, :on_demand, :display_as, :display_name, :name_to_display, :sku - attributes :on_hand, :price, :import_date + attributes :on_hand, :price, :import_date, :name, :producer_name, :image 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 @@ -12,4 +20,12 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer # Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client side. object.price.nil? ? 0.to_f : object.price end + + def producer_name + object.product.supplier.name + end + + def image + object.product.images.first.mini_url + end end diff --git a/app/views/spree/admin/variants/_autocomplete.js.erb b/app/views/spree/admin/variants/_autocomplete.js.erb index b49840725b..3e712673a1 100644 --- a/app/views/spree/admin/variants/_autocomplete.js.erb +++ b/app/views/spree/admin/variants/_autocomplete.js.erb @@ -17,15 +17,8 @@ - - {{#if variant.option_values}} - - {{/if}} diff --git a/config/locales/en.yml b/config/locales/en.yml index 015624df34..98a31441b4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3139,7 +3139,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using confirmation_pending: "Email confirmation is pending. We've sent a confirmation email to %{address}." variants: autocomplete: - producer_name: Producer + producer_name: "Producer" + unit: "Unit" general_settings: edit: legal_settings: "Legal Settings"