Merge pull request #2817 from coopdevs/remove-reference-to-on-demand

[Spree Upgrade] Remove on_demand from Product serializers
This commit is contained in:
Pau Pérez Fabregat
2018-10-16 17:52:42 +02:00
committed by GitHub
6 changed files with 41 additions and 11 deletions

View File

@@ -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}".

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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