mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-12 23:27:48 +00:00
Merge pull request #2998 from coopdevs/adapt-product-on-demand
[Spree Upgrade] Adapt product on demand
This commit is contained in:
10
app/models/concerns/product_on_demand.rb
Normal file
10
app/models/concerns/product_on_demand.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
28
spec/models/concerns/product_on_demand_spec.rb
Normal file
28
spec/models/concerns/product_on_demand_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user