mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user