Skip to content

WAIT: Validate grant amount covers scholarships already issued#1888

Draft
maebeale wants to merge 1 commit into
mainfrom
maebeale/grant-amount-min-validation
Draft

WAIT: Validate grant amount covers scholarships already issued#1888
maebeale wants to merge 1 commit into
mainfrom
maebeale/grant-amount-min-validation

Conversation

@maebeale

Copy link
Copy Markdown
Collaborator

🤖 PR, suggested 👤 review level: 📖 Read — light-logic: small, contained logic changes with low blast radius

What is the goal of this PR and why is this important?

  • Prevent saving a grant with an amount_cents lower than the total scholarships already awarded against it. Those draws are committed, so an amount below them would leave scholarships unbacked.

How did you approach the change?

  • Added a Grant model validation (amount_covers_scholarships_already_issued) that compares the proposed amount against the existing scholarships_total_cents helper.
  • Mirrors Scholarship#within_grant_budget, which guards each new draw from the other side of the ledger. Equality is allowed; new grants with no scholarships are unaffected.
  • Error message uses MoneyFormatter.dollars_from_cents (e.g. "can't be less than the $600 already awarded in scholarships").

Anything else to add?

  • TDD: added four model specs (below/equal/above total, plus fresh grant). All grant + scholarship specs pass; RuboCop clean.

A grant's funds are committed once scholarships are drawn against it, so
lowering the grant amount below the total already awarded would leave those
scholarships unbacked. Guard the amount from the grant side, mirroring
Scholarship#within_grant_budget which guards each draw from the other side.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread app/models/grant.rb

issued = scholarships_total_cents
if amount_cents < issued
errors.add(:amount_cents, "can't be less than the #{MoneyFormatter.dollars_from_cents(issued)} already awarded in scholarships")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 From Claude: Reuses scholarships_total_cents, which sums the in-memory association when loaded and otherwise runs a single SQL aggregate — so no extra query is forced on save. Equality passes intentionally (amount == awarded is valid).

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.

1 participant