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/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/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..80a29448bf --- /dev/null +++ b/app/assets/stylesheets/admin/pages/subscription_line_items.css.scss @@ -0,0 +1,11 @@ +.add-line-item { + fieldset { + .vertical-align-top { + vertical-align: top; + } + + .actions { + padding-top: 18px; + } + } +} 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..479780064f 100644 --- a/app/serializers/api/admin/variant_serializer.rb +++ b/app/serializers/api/admin/variant_serializer.rb @@ -1,15 +1,49 @@ 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 :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 + 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 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 + + def producer_name + object.product.supplier.name + 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/admin/subscriptions/_autocomplete.html.haml b/app/views/admin/subscriptions/_autocomplete.html.haml index a9d5a75a7d..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 @@ -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/app/views/spree/admin/variants/_autocomplete.js.erb b/app/views/spree/admin/variants/_autocomplete.js.erb index b49840725b..527b55568c 100644 --- a/app/views/spree/admin/variants/_autocomplete.js.erb +++ b/app/views/spree/admin/variants/_autocomplete.js.erb @@ -16,16 +16,13 @@ - - {{#if variant.option_values}} - - {{/if}} @@ -45,7 +42,7 @@ - {{#if variant.[in_stock?]}} + {{#if variant.in_stock}} {{#if variant.on_demand}} <%= Spree.t(:on_demand) %> {{else}} 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index afb422310b..3e53a82a20 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1060,6 +1060,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... @@ -3146,7 +3150,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" diff --git a/spec/serializers/api/admin/variant_serializer_spec.rb b/spec/serializers/api/admin/variant_serializer_spec.rb new file mode 100644 index 0000000000..d6eec1c1c5 --- /dev/null +++ b/spec/serializers/api/admin/variant_serializer_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe Api::Admin::VariantSerializer do + + let(:variant) { create(:variant) } + + 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 diff --git a/spec/serializers/spree/variant_serializer_spec.rb b/spec/serializers/spree/variant_serializer_spec.rb deleted file mode 100644 index c7ad285970..0000000000 --- a/spec/serializers/spree/variant_serializer_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe Api::Admin::VariantSerializer do - let(:variant) { create(:variant) } - it "serializes a variant" do - serializer = Api::Admin::VariantSerializer.new variant - expect(serializer.to_json).to match variant.options_text - end -end