diff --git a/app/models/concerns/product_stock.rb b/app/models/concerns/product_stock.rb index df7f55c949..9b1c3990fb 100644 --- a/app/models/concerns/product_stock.rb +++ b/app/models/concerns/product_stock.rb @@ -4,8 +4,4 @@ require 'active_support/concern' module ProductStock extend ActiveSupport::Concern - - def on_hand - variants.map(&:on_hand).reduce(:+) - end end diff --git a/app/serializers/api/admin/product_serializer.rb b/app/serializers/api/admin/product_serializer.rb index 991cb8e8b9..2cedbcf860 100644 --- a/app/serializers/api/admin/product_serializer.rb +++ b/app/serializers/api/admin/product_serializer.rb @@ -3,7 +3,7 @@ module Api module Admin class ProductSerializer < ActiveModel::Serializer - attributes :id, :name, :sku, :inherits_properties, :on_hand, :price, :import_date, :image_url, + attributes :id, :name, :sku, :inherits_properties, :price, :import_date, :image_url, :thumb_url, :variants def variants diff --git a/spec/controllers/api/v0/products_controller_spec.rb b/spec/controllers/api/v0/products_controller_spec.rb index 9e961e9ae0..c2954ddc6a 100644 --- a/spec/controllers/api/v0/products_controller_spec.rb +++ b/spec/controllers/api/v0/products_controller_spec.rb @@ -189,7 +189,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do # stock info - clone is set to zero it '(does not) clone the stock info of the product' do spree_post :clone, product_id: product.id, format: :json - expect(json_response['on_hand']).to eq(0) + expect(json_response.dig("variants", 0, "on_hand")).to eq(0) end # variants: only the master variant of the product is cloned diff --git a/spec/models/concerns/product_stock_spec.rb b/spec/models/concerns/product_stock_spec.rb index 70168671a6..ede8bff198 100644 --- a/spec/models/concerns/product_stock_spec.rb +++ b/spec/models/concerns/product_stock_spec.rb @@ -4,25 +4,4 @@ require "spec_helper" RSpec.describe ProductStock do let(:product) { create(:simple_product) } - - context "when product has one variant" do - describe "product.on_hand" do - it "is the products first variant on_hand" do - expect(product.on_hand).to eq(product.variants.first.on_hand) - end - end - end - - context 'when product has more than one variant' do - before do - product.variants << create(:variant, product:) - end - - describe "product.on_hand" do - it "is the sum of the products variants on_hand values" do - expect(product.on_hand) - .to eq(product.variants.first.on_hand + product.variants.second.on_hand) - end - end - end end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index a2a5b96b25..f11c596ea8 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -159,7 +159,6 @@ RSpec.describe ProductImport::ProductImporter do carrots = Spree::Product.find_by(name: 'Carrots') carrots_variant = carrots.variants.first - expect(carrots.on_hand).to eq 5 expect(carrots_variant.supplier).to eq enterprise expect(carrots_variant.price).to eq 3.20 @@ -167,11 +166,11 @@ RSpec.describe ProductImport::ProductImporter do expect(carrots_variant.variant_unit).to eq 'weight' expect(carrots_variant.variant_unit_scale).to eq 1 expect(carrots_variant.on_demand).not_to eq true + expect(carrots_variant.on_hand).to eq 5 expect(carrots_variant.import_date).to be_within(1.minute).of Time.zone.now potatoes = Spree::Product.find_by(name: 'Potatoes') potatoes_variant = potatoes.variants.first - expect(potatoes.on_hand).to eq 6 expect(potatoes_variant.supplier).to eq enterprise expect(potatoes_variant.price).to eq 6.50 @@ -179,11 +178,11 @@ RSpec.describe ProductImport::ProductImporter do expect(potatoes_variant.variant_unit).to eq 'weight' expect(potatoes_variant.variant_unit_scale).to eq 1000 expect(potatoes_variant.on_demand).not_to eq true + expect(potatoes_variant.on_hand).to eq 6 expect(potatoes_variant.import_date).to be_within(1.minute).of Time.zone.now pea_soup = Spree::Product.find_by(name: 'Pea Soup') pea_soup_variant = pea_soup.variants.first - expect(pea_soup.on_hand).to eq 8 expect(pea_soup_variant.supplier).to eq enterprise expect(pea_soup_variant.price).to eq 5.50 @@ -191,11 +190,11 @@ RSpec.describe ProductImport::ProductImporter do expect(pea_soup_variant.variant_unit).to eq 'volume' expect(pea_soup_variant.variant_unit_scale).to eq 0.001 expect(pea_soup_variant.on_demand).not_to eq true + expect(pea_soup_variant.on_hand).to eq 8 expect(pea_soup_variant.import_date).to be_within(1.minute).of Time.zone.now salad = Spree::Product.find_by(name: 'Salad') salad_variant = salad.variants.first - expect(salad.on_hand).to eq 7 expect(salad_variant.supplier).to eq enterprise expect(salad_variant.price).to eq 4.50 @@ -203,11 +202,11 @@ RSpec.describe ProductImport::ProductImporter do expect(salad_variant.variant_unit).to eq 'items' expect(salad_variant.variant_unit_scale).to eq nil expect(salad_variant.on_demand).not_to eq true + expect(salad_variant.on_hand).to eq 7 expect(salad_variant.import_date).to be_within(1.minute).of Time.zone.now buns = Spree::Product.find_by(name: 'Hot Cross Buns') buns_variant = buns.variants.first - expect(buns.on_hand).to eq 7 expect(buns_variant.supplier).to eq enterprise expect(buns_variant.price).to eq 3.50 @@ -215,6 +214,7 @@ RSpec.describe ProductImport::ProductImporter do expect(buns_variant.variant_unit).to eq 'items' expect(buns_variant.variant_unit_scale).to eq nil expect(buns_variant.on_demand).to eq true + expect(buns_variant.on_hand).to eq 7 expect(buns_variant.import_date).to be_within(1.minute).of Time.zone.now end end @@ -250,7 +250,7 @@ RSpec.describe ProductImport::ProductImporter do carrots = Spree::Product.find_by(name: 'Good Carrots') carrots_variant = carrots.variants.first - expect(carrots.on_hand).to eq 5 + expect(carrots_variant.on_hand).to eq 5 expect(carrots_variant.supplier).to eq enterprise expect(carrots_variant.price).to eq 3.20 expect(carrots_variant.import_date).to be_within(1.minute).of Time.zone.now @@ -298,7 +298,7 @@ RSpec.describe ProductImport::ProductImporter do carrots = Spree::Product.find_by(name: 'Good Carrots') carrots_variant = carrots.variants.first - expect(carrots.on_hand).to eq 5 + expect(carrots_variant.on_hand).to eq 5 expect(carrots_variant.primary_taxon.name).to eq "Vegetables" expect(carrots_variant.supplier).to eq enterprise @@ -960,11 +960,11 @@ RSpec.describe ProductImport::ProductImporter do expect(importer.products_reset_count).to eq 7 - expect(Spree::Product.find_by(name: 'Carrots').on_hand).to eq 5 # Present in file, added - expect(Spree::Product.find_by(name: 'Beans').on_hand).to eq 6 # Present in file, updated - expect(Spree::Product.find_by(name: 'Sprouts').on_hand).to eq 0 # In enterprise, not file - expect(Spree::Product.find_by(name: 'Cabbage').on_hand).to eq 0 # In enterprise, not file - expect(Spree::Product.find_by(name: 'Lettuce').on_hand) + expect(Spree::Product.find_by(name: 'Carrots').variants.first.on_hand).to eq 5 # Present in file, added + expect(Spree::Product.find_by(name: 'Beans').variants.first.on_hand).to eq 6 # Present in file, updated + expect(Spree::Product.find_by(name: 'Sprouts').variants.first.on_hand).to eq 0 # In enterprise, not file + expect(Spree::Product.find_by(name: 'Cabbage').variants.first.on_hand).to eq 0 # In enterprise, not file + expect(Spree::Product.find_by(name: 'Lettuce').variants.first.on_hand) .to eq 100 # In different enterprise; unchanged end diff --git a/spec/system/admin/product_import_spec.rb b/spec/system/admin/product_import_spec.rb index a8095dc338..62e58f6ba3 100644 --- a/spec/system/admin/product_import_spec.rb +++ b/spec/system/admin/product_import_spec.rb @@ -93,7 +93,7 @@ RSpec.describe "Product Import" do carrots = Spree::Product.find_by(name: 'Carrots') potatoes = Spree::Product.find_by(name: 'Potatoes') expect(potatoes.variants.first.supplier).to eq enterprise - expect(potatoes.on_hand).to eq 6 + expect(potatoes.variants.first.on_hand).to eq 6 expect(potatoes.variants.first.price).to eq 6.50 expect(potatoes.variants.first.import_date).to be_within(1.minute).of Time.zone.now @@ -261,9 +261,9 @@ RSpec.describe "Product Import" do expect(page).to have_selector '.created-count', text: '1' expect(page).to have_selector '.reset-count', text: '3' - expect(Spree::Product.find_by(name: 'Carrots').on_hand).to eq 500 - expect(Spree::Product.find_by(name: 'Cabbage').on_hand).to eq 0 - expect(Spree::Product.find_by(name: 'Beans').on_hand).to eq 0 + expect(Spree::Product.find_by(name: 'Carrots').variants.first.on_hand).to eq 500 + expect(Spree::Product.find_by(name: 'Cabbage').variants.first.on_hand).to eq 0 + expect(Spree::Product.find_by(name: 'Beans').variants.first.on_hand).to eq 0 end it "can save a new product and variant of that product at the same time, " \