Adding a standard variant upon initialisation of a new instance of Spree::Product

This commit is contained in:
Rob Harrington
2014-09-26 12:17:55 +10:00
parent d1ab2d9dac
commit 3f01a459ac
2 changed files with 12 additions and 1 deletions

View File

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

View File

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