@@ -65,9 +65,7 @@ def populate_item_request(partner_request)
6565 pre_existing_entry = items [ input_item [ 'item_id' ] ]
6666
6767 if pre_existing_entry
68- if pre_existing_entry . request_unit != input_item [ 'request_unit' ]
69- errors . add ( :base , "Please use the same unit for every #{ Item . find ( input_item [ "item_id" ] ) . name } " )
70- else
68+ unless pre_existing_entry . request_unit != input_item [ 'request_unit' ]
7169 pre_existing_entry . quantity = ( pre_existing_entry . quantity . to_i + input_item [ 'quantity' ] . to_i ) . to_s
7270 # NOTE: When this code was written (and maybe it's still the
7371 # case as you read it!), the FamilyRequestsController does a
@@ -82,7 +80,7 @@ def populate_item_request(partner_request)
8280 end
8381
8482 if input_item [ 'request_unit' ] . to_s == '-1' # nothing selected
85- errors . add ( :base , "Please select a unit for #{ Item . find ( input_item [ "item_id" ] ) . name } " )
83+ errors . add ( :base , "Please select a unit for #{ Item . find_by_id ( input_item [ "item_id" ] ) . name } " )
8684 end
8785
8886 item_request = Partners ::ItemRequest . new (
@@ -105,9 +103,10 @@ def populate_item_request(partner_request)
105103 } . compact
106104 end
107105
108- # Validate if request quantity exceeds the request limit for the item and unit type
106+ # Validate request quantity doesn't exceed the request limit for the item and unit type
109107 partner_request . request_items . each do |ir |
110- item = Item . find ( ir [ "item_id" ] )
108+ item = Item . find_by_id ( ir [ "item_id" ] )
109+ next if item . nil?
111110 unit_type = ir [ "request_unit" ]
112111 quantity_requested = ir [ "quantity" ] . to_i
113112
@@ -118,7 +117,12 @@ def populate_item_request(partner_request)
118117 end
119118
120119 if limit . present? && ( quantity_requested > limit )
121- errors . add ( :base , "#{ item . name } : You requested #{ quantity_requested } #{ unit_type . pluralize } , but are limited to #{ limit } #{ unit_type . pluralize } " )
120+ message = if unit_type . blank?
121+ "#{ item . name } : You requested #{ quantity_requested } , but are limited to #{ limit } "
122+ else
123+ "#{ item . name } : You requested #{ quantity_requested } #{ unit_type &.pluralize } , but are limited to #{ limit } #{ unit_type &.pluralize } "
124+ end
125+ errors . add ( :base , message )
122126 end
123127 end
124128
0 commit comments