Skip to content

Commit 103b5b3

Browse files
committed
Fix tests, remove duplicate error messages and reformat for readibility
1 parent 494a691 commit 103b5b3

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

app/models/partners/item_request.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ class ItemRequest < Base
2222
has_many :children, through: :child_item_requests
2323

2424
validates :quantity, presence: true
25-
validates :quantity, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
25+
validates :quantity, numericality: {
26+
only_integer: true,
27+
greater_than_or_equal_to: 1,
28+
message: "quantity must be a whole number greater than or equal to 1"
29+
}
2630
validates :name, presence: true
2731
validate :request_unit_is_supported
2832

2933
def request_unit_is_supported
30-
return if request_unit.blank?
34+
return if request_unit.blank? || request_unit == "-1" # nothing selected
3135

3236
names = item.request_units.map(&:name)
3337
unless names.include?(request_unit)
34-
errors.add(:request_unit, "is not supported")
38+
errors.add(:base, "#{request_unit} is not a supported unit type")
3539
end
3640
end
3741

app/models/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def request_type_label
6868
def item_requests_uniqueness_by_item_id
6969
item_ids = item_requests.map(&:item_id)
7070
if item_ids.uniq.length != item_ids.length
71-
errors.add(:item_requests, "should have unique item_ids")
71+
errors.add(:base, "Please ensure a single unit is selected for each item that supports it")
7272
end
7373
end
7474

app/services/partners/request_create_service.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/requests/items_requests_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
get edit_item_path(item)
101101

102102
parsed_body = Nokogiri::HTML(response.body)
103-
checkboxes = parsed_body.css("input[type='checkbox'][name='item[request_unit_ids][]']")
103+
checkboxes = parsed_body.css("input[type='checkbox'][name='item[unit_ids][]']")
104104
expect(checkboxes.length).to eq organization_units.length
105105
checkboxes.each do |checkbox|
106106
if checkbox['value'] == selected_unit.id.to_s

spec/requests/partners/requests_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,7 @@
242242
expect { post partners_requests_path, params: request_attributes }.to_not change { Request.count }
243243

244244
expect(response).to be_unprocessable
245-
expect(response.body).to include("Oops! Something went wrong with your Request")
246-
expect(response.body).to include("Ensure each line item has a item selected AND a quantity greater than 0.")
247-
expect(response.body).to include("Still need help? Please contact your essentials bank, #{partner.organization.name}")
248-
expect(response.body).to include("Our email on record for them is:")
249-
expect(response.body).to include(partner.organization.email)
245+
expect(response.body).to include("quantity must be a whole number greater than or equal to 1")
250246
end
251247
end
252248

@@ -346,11 +342,7 @@
346342
expect { post partners_requests_path, params: request_attributes }.to_not change { Request.count }
347343

348344
expect(response).to be_unprocessable
349-
expect(response.body).to include("Oops! Something went wrong with your Request")
350-
expect(response.body).to include("Ensure each line item has a item selected AND a quantity greater than 0.")
351-
expect(response.body).to include("Still need help? Please contact your essentials bank, #{partner.organization.name}")
352-
expect(response.body).to include("Our email on record for them is:")
353-
expect(response.body).to include(partner.organization.email)
345+
expect(response.body).to include("Completely empty request")
354346
end
355347
end
356348

spec/services/partners/request_create_service_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@
193193
let(:partner) { create(:partner) }
194194
let(:request_type) { "child" }
195195
let(:comments) { Faker::Lorem.paragraph }
196-
let(:item) { FactoryBot.create(:item) }
196+
let(:item) { FactoryBot.create(:item, unit_request_limit: request_limit) }
197+
let(:request_limit) { nil }
197198
let(:item_requests_attributes) do
198199
[
199200
ActionController::Parameters.new(
@@ -209,5 +210,14 @@
209210
expect(subject.partner_request.item_requests.first.item.name).to eq(item.name)
210211
expect(subject.partner_request.item_requests.first.quantity).to eq("25")
211212
end
213+
214+
context "request quantity exceeds the request limit for the item and unit type" do
215+
let(:request_limit) { 10 }
216+
217+
it "adds error" do
218+
expect(subject.errors).not_to be_empty
219+
expect(subject.errors[:base].first).to include "You requested 25, but are limited to 10"
220+
end
221+
end
212222
end
213223
end

spec/system/partners/requests_system_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
fill_in "request_item_requests_attributes_0_quantity", with: 50
4444
click_on "Submit Essentials Request"
4545

46-
expect(page).to have_text "Please ensure a single unit is selected for each item that supports it."
46+
expect(page).to have_text "Please select a unit for item 1."
4747
expect(Request.count).to eq(0)
4848
end
4949

0 commit comments

Comments
 (0)