Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Adding a new property with the same name to a product omits vali… #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def update
params[:product][:option_type_ids] = params[:product][:option_type_ids].reject(&:empty?)
end
invoke_callbacks(:update, :before)
if @object.update(permitted_resource_params)
if @object.update(sanitized_resource_params)
set_current_store
invoke_callbacks(:update, :after)
flash[:success] = flash_message_for(@object, :successfully_updated)
Expand Down Expand Up @@ -196,6 +196,23 @@ def permitted_resource_params
end
end

def sanitized_resource_params
return permitted_resource_params if permitted_resource_params[:product_properties_attributes].nil?

taken_property_names = []
unique_product_properties_attributes = permitted_resource_params[:product_properties_attributes].select do |_, property_attributes|
if taken_property_names.include?(property_attributes[:property_name])
false
else
taken_property_names << property_attributes[:property_name]
true
end
end
sanitized_resource_params = permitted_resource_params
sanitized_resource_params[:product_properties_attributes] = unique_product_properties_attributes
sanitized_resource_params
end

private

def variant_stock_includes
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/spree/admin/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!")
end
end

context 'adding the same property to a product twice' do
let(:product_params) { { product_properties_attributes: { '1' => { property_name: 'Foo', value: 'bar' }, '2' => { property_name: 'Foo', value: 'bar2' } } } }

specify do
expect { send_request }.not_to raise_error
expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!")
end
end
end

# regression test for #801
Expand Down