diff --git a/backend/app/controllers/spree/admin/variants_controller.rb b/backend/app/controllers/spree/admin/variants_controller.rb index 489de4dbdd..cd9467bd8f 100644 --- a/backend/app/controllers/spree/admin/variants_controller.rb +++ b/backend/app/controllers/spree/admin/variants_controller.rb @@ -16,7 +16,9 @@ def new_before @object.attributes = @object.product.master.attributes.except("id", "created_at", "deleted_at", "sku", "is_master") # Shallow Clone of the default price to populate the price field. - @object.prices.build(@object.product.master.default_price.attributes.except("id", "created_at", "updated_at", "deleted_at")) + if (default_price = @object.product.master.default_price) + @object.prices.build(default_price.attributes.except("id", "created_at", "updated_at", "deleted_at")) + end end def collection diff --git a/backend/spec/controllers/spree/admin/variants_controller_spec.rb b/backend/spec/controllers/spree/admin/variants_controller_spec.rb index 223cf937f6..048db598b5 100644 --- a/backend/spec/controllers/spree/admin/variants_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/variants_controller_spec.rb @@ -7,6 +7,42 @@ module Admin describe VariantsController, type: :controller do stub_authorization! + describe "#new" do + render_views + + let(:product) { create(:product_with_option_types) } + + before do + create(:option_value, option_type: product.option_types.first) + end + + subject { get :new, params: {product_id: product.slug} } + + it "builds a variant with the master default price" do + subject + + expect(response).to be_successful + expect(assigns(:variant).default_price.amount).to eq(product.master.default_price.amount) + end + + context "when the product has no master default price" do + before do + product.master.prices.delete_all + end + + it "builds a variant with a new default price" do + subject + + default_price = assigns(:variant).default_price + + expect(response).to be_successful + expect(default_price).to be_new_record + expect(default_price.amount).to be_nil + expect(default_price.currency).to eq(Spree::Config[:currency]) + end + end + end + describe "#index" do let(:product) { create(:product) } let(:params) { {product_id: product.slug} }