Attempt to save all records in bulk update

Before, it would abort after the first invalid record, and it doesn't tell you about the others. This way you find out about all at once.

This affects the existing Bulk Edit Products screen, and can result in longer error messages than before. But I would argue that's a good thing.

I think this is technically optional for BUU at this point, but a helpful improvement.
This commit is contained in:
David Cook
2023-08-02 10:25:44 +10:00
parent 71c36585bc
commit a0dba001bc
2 changed files with 30 additions and 2 deletions

View File

@@ -13,9 +13,10 @@ module Sets
end
def save
@collection_hash.each_value.all? do |product_attributes|
# Attempt to save all records, collecting model errors.
@collection_hash.each_value.map do |product_attributes|
update_product_attributes(product_attributes)
end
end.all?
end
def collection_attributes=(attributes)

View File

@@ -229,6 +229,33 @@ describe Sets::ProductSet do
expect(product_set.collection[0]).to eq product_a.reload
expect(product_set.collection[1]).to eq product_b.reload
end
context 'first product has an error' do
let(:collection_hash) do
{
0 => {
id: product_a.id,
name: "", # Product Name can't be blank
},
1 => {
id: product_b.id,
name: "Bananes",
},
}
end
it 'continues to update subsequent products' do
product_set.save
# Errors are logged on the model
first_item = product_set.collection[0]
expect(first_item.errors.full_messages.to_sentence).to eq "Product Name can't be blank"
expect(first_item.name).to eq ""
# Subsequent product was updated
expect(product_b.reload.name).to eq "Bananes"
end
end
end
end
end