Skip to content

Commit cf3911d

Browse files
authored
Merge pull request #5095 from mateusmsf94/5058-ensure-sq-footage-is-non-negativo
Add validation for square footage in StorageLocation model
2 parents 1fbfbf5 + 98b84f5 commit cf3911d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

app/models/storage_location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class StorageLocation < ApplicationRecord
4646
validates :name, :address, presence: true
4747
validates :warehouse_type, inclusion: { in: WAREHOUSE_TYPES },
4848
allow_blank: true
49+
validates :square_footage, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
4950
before_destroy :validate_empty_inventory, prepend: true
5051

5152
include Discard::Model

spec/models/storage_location_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
context "Validations >" do
2222
it { is_expected.to validate_presence_of(:name) }
2323
it { is_expected.to validate_presence_of(:address) }
24+
it "ensures that square_footage cannot be negative" do
25+
expect(build(:storage_location, square_footage: -1)).not_to be_valid
26+
expect(build(:storage_location, square_footage: 0)).to be_valid
27+
expect(build(:storage_location, square_footage: 100)).to be_valid
28+
expect(build(:storage_location, square_footage: nil)).to be_valid
29+
end
2430
end
2531

2632
context "Callbacks >" do

spec/requests/storage_locations_requests_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,41 @@ def item_to_h(view_item)
488488
end
489489
end
490490

491+
describe "POST #create" do
492+
let(:params) do
493+
{
494+
storage_location: {
495+
name: "New Storage Location",
496+
address: "123 New Street",
497+
square_footage: -100
498+
}
499+
}
500+
end
501+
502+
it "shows an error when square footage is negative" do
503+
post storage_locations_path, params: params
504+
expect(response.body).to include("Square footage must be greater than or equal to 0")
505+
end
506+
end
507+
508+
describe "PATCH #update" do
509+
let(:storage_location) { create(:storage_location, organization: organization) }
510+
let(:params) do
511+
{
512+
storage_location: {
513+
name: "Updated Name",
514+
address: "123 Updated Street",
515+
square_footage: -100
516+
}
517+
}
518+
end
519+
520+
it "shows an error when square footage is negative" do
521+
patch storage_location_path(storage_location), params: params
522+
expect(response.body).to include("Square footage must be greater than or equal to 0")
523+
end
524+
end
525+
491526
context "Looking at a different organization" do
492527
let(:object) { create(:storage_location, organization: create(:organization)) }
493528
include_examples "requiring authorization"

0 commit comments

Comments
 (0)