From 75ad4254a6c50b28a2f3b36828d2e3428b20b774 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 3 Oct 2018 12:26:13 +0200 Subject: [PATCH 1/2] Remove on_demand from Api::Admin::ProductSerializer This change also moves the spec file to its appropriate place for the sake of consistency and RSpec3-izes the test example. --- app/models/spree/product_decorator.rb | 8 +++++++ .../api/admin/product_serializer.rb | 2 +- spec/models/spree/product_spec.rb | 21 ++++++++++++++++++- .../api/admin/product_serializer_spec.rb | 10 +++++++++ .../spree/product_serializer_spec.rb | 7 ------- 5 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 spec/serializers/api/admin/product_serializer_spec.rb delete mode 100644 spec/serializers/spree/product_serializer_spec.rb diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 64e5c74677..53f51749c7 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -128,6 +128,14 @@ Spree::Product.class_eval do # -- Methods + def on_hand + if has_variants? + variants.map(&:on_hand).reduce(:+) + else + master.on_hand + end + end + # Called by Spree::Product::duplicate before saving. def duplicate_extra(parent) # Spree sets the SKU to "COPY OF #{parent sku}". diff --git a/app/serializers/api/admin/product_serializer.rb b/app/serializers/api/admin/product_serializer.rb index 8af5701b94..c7760822d7 100644 --- a/app/serializers/api/admin/product_serializer.rb +++ b/app/serializers/api/admin/product_serializer.rb @@ -1,5 +1,5 @@ class Api::Admin::ProductSerializer < ActiveModel::Serializer - attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name, :on_demand, :inherits_properties + attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name, :inherits_properties attributes :on_hand, :price, :available_on, :permalink_live, :tax_category_id, :import_date, :image_url, :thumb_url diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index f90de704e2..dad517be73 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -542,7 +542,6 @@ module Spree end end - describe "variant units" do context "when the product already has a variant unit set (and all required option types exist)" do let!(:p) { create(:simple_product, @@ -713,5 +712,25 @@ module Spree e.variants(true).should be_empty end end + + describe '#on_hand' do + let(:product) { create(:product) } + + context 'when the product has variants' do + before { create(:variant, product: product) } + + it 'returns the sum of the on_hand of its variants' do + expect(product.on_hand).to eq(Float::INFINITY) + end + end + + context 'when the product has no variants' do + before { product.variants.destroy_all } + + it 'returns the on_hand of the master' do + expect(product.on_hand).to eq(product.master.on_hand) + end + end + end end end diff --git a/spec/serializers/api/admin/product_serializer_spec.rb b/spec/serializers/api/admin/product_serializer_spec.rb new file mode 100644 index 0000000000..1946735d5e --- /dev/null +++ b/spec/serializers/api/admin/product_serializer_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe Api::Admin::ProductSerializer do + let(:product) { create(:simple_product) } + let(:serializer) { described_class.new(product) } + + it "serializes a product" do + expect(serializer.to_json).to match(product.name) + end +end diff --git a/spec/serializers/spree/product_serializer_spec.rb b/spec/serializers/spree/product_serializer_spec.rb deleted file mode 100644 index ab6883f9c5..0000000000 --- a/spec/serializers/spree/product_serializer_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe Api::Admin::ProductSerializer do - let(:product) { create(:simple_product) } - it "serializes a product" do - serializer = Api::Admin::ProductSerializer.new product - serializer.to_json.should match product.name - end -end From f8ec198f9f3e7aa3a0361a9a8753c881f5f27381 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 3 Oct 2018 14:25:32 +0200 Subject: [PATCH 2/2] Remove on_demand from Api::CachedProductSerializer This fixes lots of specs like the whole spec/controllers/shop_controller_spec.rb, almost all specs of spec/lib/open_food_network/products_renderer_spec.rb and most of spec/features/consumer/shopping/shopping_spec.rb. --- app/serializers/api/product_serializer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/serializers/api/product_serializer.rb b/app/serializers/api/product_serializer.rb index 27ea6c5ed4..9f2726cdd7 100644 --- a/app/serializers/api/product_serializer.rb +++ b/app/serializers/api/product_serializer.rb @@ -4,7 +4,7 @@ class Api::ProductSerializer < ActiveModel::Serializer # TODO # Prices can't be cached? How? def serializable_hash - cached_serializer_hash.merge uncached_serializer_hash + cached_serializer_hash.merge(uncached_serializer_hash) end private @@ -36,7 +36,7 @@ class Api::CachedProductSerializer < ActiveModel::Serializer include ActionView::Helpers::SanitizeHelper attributes :id, :name, :permalink, :meta_keywords - attributes :on_demand, :group_buy, :notes, :description, :description_html + attributes :group_buy, :notes, :description, :description_html attributes :properties_with_values has_many :variants, serializer: Api::VariantSerializer