diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index cf7d02bf46..034bbed043 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -32,6 +32,7 @@ Spree::Product.class_eval do validates_presence_of :variant_unit_name, if: -> p { p.variant_unit == 'items' } + after_initialize :ensure_first_standard_variant, :if => :new_record? after_initialize :set_available_on_to_now, :if => :new_record? after_save :update_units after_touch :touch_distributors @@ -205,4 +206,7 @@ Spree::Product.class_eval do Spree::OptionType.where('name LIKE ?', 'unit_%%') end + def ensure_first_standard_variant + self.variants << Spree::Variant.new + end end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 400f92754b..73c6f2fe10 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -85,6 +85,14 @@ module Spree end end + context "instatiating a new product" do + let!(:product) { Spree::Product.new } + + it "creates a standard (non-master) variant when created" do + product.variants.should_not be_empty + end + end + context "when the unit is items" do it "is valid when unit name is set and unit scale is not" do product.variant_unit = 'items' @@ -122,7 +130,6 @@ module Spree end end end - describe "scopes" do describe "in_supplier" do