diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index e61f06ee37..d5ccd13dc7 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/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 diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 16f045b03c..76a69d7747 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,6 +712,26 @@ 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 describe "product import" do 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