diff --git a/backend/app/controllers/spree/admin/products_controller.rb b/backend/app/controllers/spree/admin/products_controller.rb index 160ab8c2a31..ffffde186a7 100644 --- a/backend/app/controllers/spree/admin/products_controller.rb +++ b/backend/app/controllers/spree/admin/products_controller.rb @@ -8,6 +8,7 @@ class ProductsController < ResourceController before_action :load_data, except: [:index] update.before :update_before helper_method :clone_object_url + before_action :split_params, only: [:create, :update] def show redirect_to action: :edit @@ -19,12 +20,6 @@ def index end def update - if params[:product][:taxon_ids].present? - params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',') - end - if params[:product][:option_type_ids].present? - params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',') - end if updating_variant_property_rules? params[:product][:variant_property_rules_attributes].each do |_index, param_attrs| param_attrs[:option_value_ids] = param_attrs[:option_value_ids].split(',') @@ -73,6 +68,15 @@ def clone private + def split_params + if params[:product][:taxon_ids].present? + params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',') + end + if params[:product][:option_type_ids].present? + params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',') + end + end + def find_resource Spree::Product.with_deleted.friendly.find(params[:id]) end diff --git a/backend/spec/controllers/spree/admin/products_controller_spec.rb b/backend/spec/controllers/spree/admin/products_controller_spec.rb index d6713527616..2592f0322f8 100644 --- a/backend/spec/controllers/spree/admin/products_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/products_controller_spec.rb @@ -50,6 +50,78 @@ end end + # regression test for https://github.com/solidusio/solidus/issues/2791 + context "creating a product" do + before(:all) do + create(:shipping_category) + end + + it "creates a product" do + post :create, params: { + product: { + name: "Product #1 - 9632", + description: "As seen on TV!", + price: 19.99, + shipping_category_id: Spree::ShippingCategory.first.id, + } + } + expect(flash[:success]).to eq("Product \"Product #1 - 9632\" has been successfully created!") + end + + context "when there is a taxon" do + let(:first_taxon) { create(:taxon) } + + it "creates a product with a taxon" do + post :create, params: { + product: { + name: "Product #1 - 9632", + description: "As seen on TV!", + price: 19.99, + shipping_category_id: Spree::ShippingCategory.first.id, + taxon_ids: first_taxon.id.to_s + } + } + expect(flash[:success]).to eq("Product \"Product #1 - 9632\" has been successfully created!") + end + + context "when their are multiple taxons" do + let(:second_taxon) { create(:taxon) } + + it "creates a product with multiple taxons" do + post :create, params: { + product: { + name: "Product #1 - 9632", + description: "As seen on TV!", + price: 19.99, + shipping_category_id: Spree::ShippingCategory.first.id, + taxon_ids: "#{first_taxon.id}, #{second_taxon.id}" + } + } + expect(flash[:success]).to eq("Product \"Product #1 - 9632\" has been successfully created!") + end + end + end + end + + context "adding taxons to a product" do + let(:product) { create(:product) } + let(:first_taxon) { create(:taxon) } + + it "adds a single taxon to a product" do + put :update, params: { id: product.to_param, product: { taxon_ids: first_taxon.id.to_s } } + expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!") + end + + context "when there are mulitple taxons" do + let(:second_taxon) { create(:taxon) } + + it "adds multiple taxons to a product" do + put :update, params: { id: product.to_param, product: { taxon_ids: "#{first_taxon.id}, #{second_taxon.id}" } } + expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!") + end + end + end + describe "creating variant property rules" do let(:first_property) { create(:property) } let(:second_property) { create(:property) }