From 71702af3c35cb6b2006c9adcf1058274c5980f86 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 6 Nov 2018 13:58:32 +0000 Subject: [PATCH 1/2] Have a single attributes list in serializer --- app/serializers/api/admin/product_serializer.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/serializers/api/admin/product_serializer.rb b/app/serializers/api/admin/product_serializer.rb index c7760822d7..7d0607809a 100644 --- a/app/serializers/api/admin/product_serializer.rb +++ b/app/serializers/api/admin/product_serializer.rb @@ -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 From de318231519490c68a50f4a4d8908a4023fe0fab Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 18 Oct 2018 17:23:06 +0200 Subject: [PATCH 2/2] Add #on_demand= to make product creation page work We're bringing in the setter that got removed in Spree 2.0 so that we can still pass an on_demand value from the product creation form. However, we won't keep the getter to keep things simple. The frontend seems to be checking the existence of variants anyway, so then it can also check for product.master.on_demand and we avoid some logic on the backend. --- app/models/concerns/product_on_demand.rb | 10 +++++++ app/models/spree/product_decorator.rb | 3 ++ .../models/concerns/product_on_demand_spec.rb | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 app/models/concerns/product_on_demand.rb create mode 100644 spec/models/concerns/product_on_demand_spec.rb diff --git a/app/models/concerns/product_on_demand.rb b/app/models/concerns/product_on_demand.rb new file mode 100644 index 0000000000..0ad101e7ca --- /dev/null +++ b/app/models/concerns/product_on_demand.rb @@ -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 diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 8c0571558a..a7aefcee48 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -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 diff --git a/spec/models/concerns/product_on_demand_spec.rb b/spec/models/concerns/product_on_demand_spec.rb new file mode 100644 index 0000000000..8ff11302cd --- /dev/null +++ b/spec/models/concerns/product_on_demand_spec.rb @@ -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