Skip to content

Commit

Permalink
Merge pull request #2840 from jacobherrington/bug/allow-multiple-taxo…
Browse files Browse the repository at this point in the history
…ns-on-product-create

allow multiple taxons on product creation
  • Loading branch information
kennyadsl authored Oct 3, 2018
2 parents 3010496 + 016d8f8 commit 7c626c2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
16 changes: 10 additions & 6 deletions backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(',')
Expand Down Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions backend/spec/controllers/spree/admin/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 7c626c2

Please sign in to comment.