Skip to content

Commit f906f01

Browse files
authored
Merge pull request #5375 from Tyler-Fanuele/5361StorageLocationNameUnique
Storage location name uniqueness
2 parents 237090e + 6dfa687 commit f906f01

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

app/models/storage_location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class StorageLocation < ApplicationRecord
4444
dependent: :destroy
4545

4646
validates :name, :address, presence: true
47+
validates :name, uniqueness: { scope: :organization_id, case_sensitive: false}
4748
validates :warehouse_type, inclusion: { in: WAREHOUSE_TYPES },
4849
allow_blank: true
4950
validates :square_footage, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

spec/factories/storage_locations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
FactoryBot.define do
2222
factory :storage_location do
23-
name { "Dont test this name" }
23+
sequence(:name) { |n| "Dont test this name #{n}" }
2424
address { "Dont test this address" }
2525
organization { Organization.try(:first) || create(:organization) }
2626

spec/models/storage_location_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
context "Validations >" do
2222
it { is_expected.to validate_presence_of(:name) }
23+
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:organization_id).case_insensitive }
2324
it { is_expected.to validate_presence_of(:address) }
2425
it "ensures that square_footage cannot be negative" do
2526
expect(build(:storage_location, square_footage: -1)).not_to be_valid

spec/system/storage_location_system_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
expect(page.find(".alert")).to have_content "added"
2121
end
2222

23+
it "User creates a new storage location with the same name" do
24+
visit subject
25+
storage_location1 = create(:storage_location, name: "non-Unique Name")
26+
27+
fill_in "Name", with: storage_location1.name
28+
fill_in "Address", with: storage_location1.address
29+
click_on "Save"
30+
31+
expect(page).to have_content "Name has already been taken"
32+
end
33+
34+
it "User creates a new storage location with the same name with different casing" do
35+
visit subject
36+
storage_location1 = create(:storage_location, name: "non-Unique Name")
37+
38+
fill_in "Name", with: storage_location1.name.upcase
39+
fill_in "Address", with: storage_location1.address
40+
click_on "Save"
41+
42+
expect(page).to have_content "Name has already been taken"
43+
end
44+
2345
it 'User creates a new storage location with optional fields' do
2446
visit subject
2547
storage_location_traits = attributes_for(:storage_location)

0 commit comments

Comments
 (0)