Skip to content

Disallow saving Purchase with a quantity of 0 (#5507)#5510

Open
cassxw wants to merge 1 commit intorubyforgood:mainfrom
cassxw:5507-disallow-purchase-zero-quantity
Open

Disallow saving Purchase with a quantity of 0 (#5507)#5510
cassxw wants to merge 1 commit intorubyforgood:mainfrom
cassxw:5507-disallow-purchase-zero-quantity

Conversation

@cassxw
Copy link

@cassxw cassxw commented Mar 7, 2026

Resolves #5507

Description

This bug fix prevents users from saving a Purchase with zero quantity line items. Previously, users could create Purchases with 0 or blank quantities, which should not be allowed.

The fix adds validation to the Purchase model that checks all line items have a quantity of at least 1, reusing the existing line_items_quantity_is_at_least(1) method from the Itemizable concern - which was already used by Donation, Distribution, and Transfer models.

Note: The error message uses existing wording ("Inventory {item}'s quantity needs to be at least 1") rather than the issue's suggested wording, to remain consistent with other models.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added a unit test in spec/models/purchase_spec.rb that verifies a Purchase with a zero-quantity line item is invalid.
  • All 25 tests in the Purchase spec pass.
  • Manual testing confirmed the error message displays on the form when attempting to save with zero quantity (see screenshot below).
  1. Click on Purchases in side bar.
  2. Click on New Purchase
  3. Fill out the new purchase form and either leave out the quantity or explicitly enter a 0 quantity.
  4. Notice that the purchase cannot be saved, with an appropriate error message.

Screenshots

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Disallow Saving Purchase with a Quantity of 0

1 participant