Skip to content

Allow a hard limit on number of specific items per request - #5386

Open
embarnard wants to merge 18 commits into
mainfrom
request-limit-on-forms
Open

Allow a hard limit on number of specific items per request#5386
embarnard wants to merge 18 commits into
mainfrom
request-limit-on-forms

Conversation

@embarnard

@embarnard embarnard commented Sep 12, 2025

Copy link
Copy Markdown
Collaborator

Resolves #5269

Description

  • Adds request_limit to ItemUnit and unit_request_limit to Item fields to models
  • Adds these request limit options for units in the bank's item edit page including for custom units
  • Request limit form fields will be disabled if the unit is disabled
  • Adds validation to the partners/request page if amount requested is higher than the request limit and will display flash error:
    "[Item name]: You requested [amount requested] [units if applicable], but are limited to [request limit] [units if applicable].
  • Removed ambiguous error message on partners/request page
  • Removed some duplicate error messages for the same thing on the partner/request page so it wouldn't much up the flash message

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • unit tests for adding an item with request units and request limits
  • feature tests that test what displays in the error message on the request page if request quantity exceeds the limit

Screenshots

items/:id/edit page
Screenshot 2025-09-13 at 11 18 36 AM

/partners/requests page
Screenshot 2025-09-13 at 11 10 55 AM

removed this error message from /partners/requests page
Screenshot 2025-09-13 at 10 27 44 AM

@embarnard
embarnard marked this pull request as draft September 12, 2025 19:36
tabindex="-1"
data-bs-backdrop="static"
data-confirmation-target="modal">
</div>

@embarnard embarnard Sep 13, 2025

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.

just added a missing div in this file, rest of the changes are just spacing from auto-format

data-confirmation-pre-check-path-value="<%= validate_partners_requests_path(format: :json) %>">
<div class="card-body">

<% if @errors.present? %>

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.

removed this error message in favor of flash messages instead
Screenshot 2025-09-13 at 10 27 44 AM

Comment thread app/models/request.rb
item_ids = item_requests.map(&:item_id)
if item_ids.uniq.length != item_ids.length
errors.add(:item_requests, "should have unique item_ids")
errors.add(:base, "Please ensure a single unit is selected for each item that supports it")

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.

changed to :base since we are printing from the error.full_messages now so that it won't include the ugly and confusing item_requests before the error message

.joins(profile: :counties)
.group(:id)
.pluck(Arel.sql("partners.id, STRING_AGG(counties.name, '; ' ORDER BY counties.region, counties.name) AS county_list"))
.pluck(Arel.sql("partners.id, STRING_AGG(counties.name, '; ' ORDER BY LOWER(counties.region), LOWER(counties.name)) AS county_list"))

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.

have no idea why this started failing in this PR but if a region or name is lower case and another is uppercase of the same letter the uppercase will take precedence in the order

pre_existing_entry = items[input_item['item_id']]

if pre_existing_entry
if pre_existing_entry.request_unit != input_item['request_unit']

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.

removed this because we basically add the same error in the model

@awwaiid
awwaiid self-requested a review September 14, 2025 13:44
@embarnard
embarnard marked this pull request as ready for review September 14, 2025 13:49
@jonny5

jonny5 commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

@awwaiid

awwaiid and others added 3 commits August 29, 2026 20:34
Resolves conflicts from the removal of the enable_packs feature flag:
- partners/requests_controller: keep main's flag-free fetch_items, keep
  this branch's includes(:request_units) to avoid the N+1
- items/_form: keep the new request limit field, drop the flag guard
- items_controller_spec: keep this branch's rewritten request-unit
  tests, drop the Flipper.enable call
- partners/requests_system_spec: take main's flattened tests (the packs
  on/off contexts are gone with the flag) while keeping this branch's
  expected message, "Please select a unit for item 1."

db/schema.rb regenerated so the new columns sort alphabetically, and
the model/factory/spec schema annotations updated to match.
This branch changed the partner CSV export's county ordering to sort
case-insensitively, which has nothing to do with per-item request
limits; the author noted at the time they could not tell why it had
started failing.

It is no longer needed: the export, partner request, county, county
profile and distributions-by-county specs all pass without it (101
examples), so whatever it was compensating for has since been fixed on
main. Dropping it keeps the branch to its own subject.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AoUPX63nd1LSkKDPEiQ7jt
The flash built the message with String#capitalize, which upcases the
first character and downcases everything after it. That mangled the
item names and the sentence the issue asks for: a request over the
limit read

  Kids (size 1): you requested 100, but are limited to 50.

instead of the specified "[Item name]: You requested ... but are
limited to ...". With several errors joined by "; ", every message
after the first was flattened the same way.

upcase_first leaves the rest of the string alone, so the messages that
already begin with a capital keep their names intact and the ones that
begin with a unit name ("flat is not a supported unit type") still get
their first letter raised, which is what capitalize was there for.

The system spec expected the mangled "item 1" text; it now expects the
item's real name.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AoUPX63nd1LSkKDPEiQ7jt
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.

Allow a hard limit on number of specific items per request.

3 participants