Merge pull request #2998 from coopdevs/adapt-product-on-demand

[Spree Upgrade] Adapt product on demand
This commit is contained in:
Luis Ramos
2018-11-08 19:21:46 +00:00
committed by GitHub
4 changed files with 44 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
require 'active_support/concern'
module ProductOnDemand
extend ActiveSupport::Concern
def on_demand=(value)
raise 'cannot set on_demand of product with variants' if variants.any?
master.on_demand = value
end
end

View File

@@ -1,8 +1,11 @@
require 'open_food_network/permalink_generator'
require 'open_food_network/property_merge'
require 'concerns/product_on_demand'
Spree::Product.class_eval do
include PermalinkGenerator
include ProductOnDemand
# We have an after_destroy callback on Spree::ProductOptionType. However, if we
# don't specify dependent => destroy on this association, it is not called. See:
# https://github.com/rails/rails/issues/7618

View File

@@ -1,7 +1,7 @@
class Api::Admin::ProductSerializer < ActiveModel::Serializer
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
attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name,
:inherits_properties, :on_hand, :price, :available_on, :permalink_live,
:tax_category_id, :import_date, :image_url, :thumb_url
has_one :supplier, key: :producer_id, embed: :id
has_one :primary_taxon, key: :category_id, embed: :id

View File

@@ -0,0 +1,28 @@
require 'spec_helper'
describe ProductOnDemand do
describe '#on_demand=' do
context 'when the product has no variants' do
let(:product) { create(:simple_product) }
before do
product.variants.first.destroy
product.variants.reload
end
it 'sets the value on master.on_demand' do
product.on_demand = false
expect(product.master.on_demand).to eq(false)
end
end
context 'when the product has variants' do
let(:product) { create(:simple_product) }
it 'raises' do
expect { product.on_demand = true }
.to raise_error(StandardError, /cannot set on_demand/)
end
end
end
end