From 5739a82c1961388c4c0b694279a11ee8496b1810 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 22 Jul 2019 21:03:40 +0100 Subject: [PATCH 1/8] Remove unused admin variants search rabl template --- app/views/spree/admin/variants/search.rabl | 40 ---------------------- 1 file changed, 40 deletions(-) delete mode 100644 app/views/spree/admin/variants/search.rabl diff --git a/app/views/spree/admin/variants/search.rabl b/app/views/spree/admin/variants/search.rabl deleted file mode 100644 index 62b91ccf9b..0000000000 --- a/app/views/spree/admin/variants/search.rabl +++ /dev/null @@ -1,40 +0,0 @@ -# -# overriding spree/core/app/views/spree/admin/variants/search.rabl -# -collection @variants -attributes :sku, :options_text, :in_stock?, :on_demand, :on_hand, :id, :cost_price - -node(:name) do |v| - # TODO: when products must have a unit, full_name will always be present - variant_specific = v.full_name - if variant_specific.present? - "#{v.name} - #{v.full_name}" - else - v.name - end -end - -node(:full_name, &:full_name) - -node(:producer_name) do |v| - v.product.supplier.name -end - -node(:stock_location_id) do |v| - v.stock_items.first.stock_location.id -end - -node(:stock_location_name) do |v| - v.stock_items.first.stock_location.name -end - -child(images: :images) do - attributes :mini_url -end - -child(option_values: :option_values) do - child(option_type: :option_type) do - attributes :name, :presentation - end - attributes :name, :presentation -end From 7e6259da31297ac29035cf45b8015714ae1a780d Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 12:31:07 +0100 Subject: [PATCH 2/8] 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" From 2d5eccbf97d7d7fc5bdc303aa6ee3fbba0fbdfd0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 16:32:10 +0100 Subject: [PATCH 3/8] Add :in_stock, :stock_location_id, :stock_location_name to admin variant serializer, these are needed for the variant_autocomplete js code from spree --- .rubocop_manual_todo.yml | 1 - .../api/admin/variant_serializer.rb | 24 ++++++++++++++++--- .../spree/admin/variants/_autocomplete.js.erb | 8 +++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 6d4a4719e3..9826867220 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -123,7 +123,6 @@ Metrics/LineLength: - app/serializers/api/admin/subscription_serializer.rb - app/serializers/api/admin/tag_rule_serializer.rb - app/serializers/api/admin/variant_override_serializer.rb - - app/serializers/api/admin/variant_serializer.rb - app/services/cart_service.rb - app/services/default_stock_location.rb - app/services/embedded_page_service.rb diff --git a/app/serializers/api/admin/variant_serializer.rb b/app/serializers/api/admin/variant_serializer.rb index ad08a3edd6..479780064f 100644 --- a/app/serializers/api/admin/variant_serializer.rb +++ b/app/serializers/api/admin/variant_serializer.rb @@ -1,6 +1,9 @@ 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, :name, :producer_name, :image + attributes :id, :name, :producer_name, :image, :sku, :import_date + attributes :options_text, :unit_value, :unit_description, :unit_to_display + attributes :display_as, :display_name, :name_to_display + attributes :price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name + has_many :variant_overrides def name @@ -17,7 +20,7 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer end def price - # Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client side. + # 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 @@ -26,6 +29,21 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer end def image + return if object.product.images.empty? object.product.images.first.mini_url end + + def in_stock + object.in_stock? + end + + def stock_location_id + return if object.stock_items.empty? + 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 + end end diff --git a/app/views/spree/admin/variants/_autocomplete.js.erb b/app/views/spree/admin/variants/_autocomplete.js.erb index 3e712673a1..527b55568c 100644 --- a/app/views/spree/admin/variants/_autocomplete.js.erb +++ b/app/views/spree/admin/variants/_autocomplete.js.erb @@ -16,7 +16,11 @@
  • <%= t('admin.sku') %>: {{variant.sku}}
  • -
  • <%= t('on_hand') %>: {{variant.on_hand}}
  • + {{#if variant.on_demand }} +
  • <%= t('on_demand') %>
  • + {{ else }} +
  • <%= t('on_hand') %>: {{variant.on_hand}}
  • + {{/if}}
  • <%= t('.unit') %>: {{variant.options_text}}
@@ -38,7 +42,7 @@ - {{#if variant.[in_stock?]}} + {{#if variant.in_stock}} {{#if variant.on_demand}} <%= Spree.t(:on_demand) %> {{else}} From 90c621ac07cf6e13e9290199e68718740f0917d9 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 20:02:57 +0100 Subject: [PATCH 4/8] Improve translations, css and add button aligment in subscriptions_line_items variant autocomplete --- .../admin/pages/subscription_line_items.css.scss | 11 +++++++++++ .../admin/subscriptions/_autocomplete.html.haml | 12 ++++++------ config/locales/en.yml | 4 ++++ 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 app/assets/stylesheets/admin/pages/subscription_line_items.css.scss diff --git a/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss new file mode 100644 index 0000000000..c28a418b44 --- /dev/null +++ b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss @@ -0,0 +1,11 @@ +#add-line-item { + fieldset { + td.vertical_align_top { + vertical-align: top; + } + + .actions { + padding-top: 18px; + } + } +} diff --git a/app/views/admin/subscriptions/_autocomplete.html.haml b/app/views/admin/subscriptions/_autocomplete.html.haml index a9d5a75a7d..e5fbc62e49 100644 --- a/app/views/admin/subscriptions/_autocomplete.html.haml +++ b/app/views/admin/subscriptions/_autocomplete.html.haml @@ -6,15 +6,15 @@ %col{ width: '20%' } %col{ width: '20%' } %tr - %td{ style: "vertical-align:top" } + %td.vertical_align_top .field - = label_tag :add_variant_id, t(:name_or_sku) + = label_tag :add_variant_id, t('.name_or_sku') %input#add_variant_id.variant_autocomplete.fullwidth{ type: 'number', ng: { model: 'newItem.variant_id' } } - %td{ style: "vertical-align:top" } + %td.vertical_align_top .field - = label_tag :add_quantity, t(:qty) + = label_tag :add_quantity, t('.quantity') %input#add_quantity.fullwidth{ type: 'number', min: 1, ng: { model: 'newItem.quantity' } } - %td{ style: "vertical-align:top" } + %td .actions %a.icon-plus.button.fullwidth{ href: 'javascript:void(0)', method: :post, ng: { click: 'addSubscriptionLineItem()' } } - = t(:add) + = t('.add') diff --git a/config/locales/en.yml b/config/locales/en.yml index 98a31441b4..2e008f7a6b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1054,6 +1054,10 @@ en: The displayed prices are only an estimate and calculated at the time the subscription is changed. If you change prices or fees, orders will be updated, but the subscription will still display the old values. not_in_open_and_upcoming_order_cycles_warning: "There are no open or upcoming order cycles for this product." + autocomplete: + name_or_sku: "NAME OR SKU" + quantity: "Quantity" + add: "Add" details: details: Details invalid_error: Oops! Please fill in all of the required fields... From 90690cd238c60542707e4c9b07274d8dfc0c464b Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 22:03:40 +0100 Subject: [PATCH 5/8] Move api/admin/variant_serializer_spec to the correct folder under /spec --- spec/serializers/{spree => api/admin}/variant_serializer_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/serializers/{spree => api/admin}/variant_serializer_spec.rb (100%) diff --git a/spec/serializers/spree/variant_serializer_spec.rb b/spec/serializers/api/admin/variant_serializer_spec.rb similarity index 100% rename from spec/serializers/spree/variant_serializer_spec.rb rename to spec/serializers/api/admin/variant_serializer_spec.rb From 7a8b5e89be149a40fa037059ff48a9a6ea9ecc7a Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 22:11:00 +0100 Subject: [PATCH 6/8] Add specs to cover the most important new fields in api/admin/variant_serializer --- .../api/admin/variant_serializer_spec.rb | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/spec/serializers/api/admin/variant_serializer_spec.rb b/spec/serializers/api/admin/variant_serializer_spec.rb index c7ad285970..d6eec1c1c5 100644 --- a/spec/serializers/api/admin/variant_serializer_spec.rb +++ b/spec/serializers/api/admin/variant_serializer_spec.rb @@ -1,7 +1,30 @@ +require 'spec_helper' + describe Api::Admin::VariantSerializer do + let(:variant) { create(:variant) } - it "serializes a variant" do + + it "serializes the variant name" do serializer = Api::Admin::VariantSerializer.new variant + + expect(serializer.to_json).to match variant.name + end + + it "serializes the variant options" do + serializer = Api::Admin::VariantSerializer.new variant + expect(serializer.to_json).to match variant.options_text end + + it "serializes the variant full name" do + serializer = Api::Admin::VariantSerializer.new variant + + expect(serializer.to_json).to match variant.full_name + end + + it "serializes the variant stock location id" do + serializer = Api::Admin::VariantSerializer.new variant + + expect(serializer.to_json).to match variant.stock_items.first.stock_location.id.to_s + end end From ffb8edef0bb1f4aec4bdd691f1789381db0c9ff2 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 23 Jul 2019 22:37:10 +0100 Subject: [PATCH 7/8] Fix rubocop css issues --- .../stylesheets/admin/pages/subscription_line_items.css.scss | 2 +- app/views/admin/subscriptions/_autocomplete.html.haml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss index c28a418b44..8e4293ef42 100644 --- a/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss +++ b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss @@ -1,6 +1,6 @@ #add-line-item { fieldset { - td.vertical_align_top { + .vertical-align-top { vertical-align: top; } diff --git a/app/views/admin/subscriptions/_autocomplete.html.haml b/app/views/admin/subscriptions/_autocomplete.html.haml index e5fbc62e49..43aa03cdca 100644 --- a/app/views/admin/subscriptions/_autocomplete.html.haml +++ b/app/views/admin/subscriptions/_autocomplete.html.haml @@ -6,11 +6,11 @@ %col{ width: '20%' } %col{ width: '20%' } %tr - %td.vertical_align_top + %td.vertical-align-top .field = label_tag :add_variant_id, t('.name_or_sku') %input#add_variant_id.variant_autocomplete.fullwidth{ type: 'number', ng: { model: 'newItem.variant_id' } } - %td.vertical_align_top + %td.vertical-align-top .field = label_tag :add_quantity, t('.quantity') %input#add_quantity.fullwidth{ type: 'number', min: 1, ng: { model: 'newItem.quantity' } } From 04e13e113638dc6d80bd59e7c9d163aa9952c8bb Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 29 Jul 2019 19:45:55 +0100 Subject: [PATCH 8/8] Change css styling from id to class to make rubocop happy. Keeping id name as it may be needed in spree autocomplete css or js code --- .../stylesheets/admin/pages/subscription_line_items.css.scss | 2 +- app/views/admin/subscriptions/_autocomplete.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss index 8e4293ef42..80a29448bf 100644 --- a/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss +++ b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss @@ -1,4 +1,4 @@ -#add-line-item { +.add-line-item { fieldset { .vertical-align-top { vertical-align: top; diff --git a/app/views/admin/subscriptions/_autocomplete.html.haml b/app/views/admin/subscriptions/_autocomplete.html.haml index 43aa03cdca..789f27104c 100644 --- a/app/views/admin/subscriptions/_autocomplete.html.haml +++ b/app/views/admin/subscriptions/_autocomplete.html.haml @@ -1,4 +1,4 @@ -#add-line-item +#add-line-item.add-line-item %fieldset %legend{ align: 'center'}= t(:products) %table.no-borders.layout