Skip to content

Commit

Permalink
Merge pull request #4025 from rubyforgood/3986-dupe-audit-items
Browse files Browse the repository at this point in the history
3986: Disallow duplicate items in audit
  • Loading branch information
cielf authored Jan 18, 2024
2 parents e07d71e + 730c56f commit e12b3b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/controllers/audits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def update
if @audit.update(audit_params)
save_audit_status_and_redirect(params)
else
flash[:error] = "Something didn't work quite right -- try again?"
flash[:error] = @audit.errors.full_messages.join("\n")
@storage_locations = [@audit.storage_location]
set_items
@audit.line_items.build if @audit.line_items.empty?
Expand Down Expand Up @@ -93,7 +93,8 @@ def destroy

def handle_audit_errors
error_message = @audit.errors.uniq(&:attribute).map do |error|
"#{error.attribute.capitalize} ".tr("_", " ") + error.message
attr = (error.attribute.to_s == 'base') ? '' : error.attribute.capitalize
"#{attr} ".tr("_", " ") + error.message
end
flash[:error] = error_message.join(", ")
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Audit < ApplicationRecord
validates :storage_location, :organization, presence: true
validate :line_items_exist_in_inventory
validate :line_items_quantity_is_not_negative
validate :line_items_unique_by_item_id
validate :user_is_organization_admin_of_the_organization

def self.storage_locations_audited_for(organization)
Expand All @@ -46,6 +47,16 @@ def user_is_organization_admin_of_the_organization

private

def line_items_unique_by_item_id
item_ids = line_items.map(&:item_id)
duplicate_ids = item_ids.select { |i| item_ids.count(i) > 1 }
if duplicate_ids.any?
item_names = Item.where(id: duplicate_ids).map(&:name)
errors.add(:base,
"You have entered at least one duplicate item: #{item_names.join(", ")}")
end
end

def line_items_quantity_is_not_negative
line_items_quantity_is_at_least(0)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/models/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@
expect(audit.save).to be_falsey
end

it 'cannot have duplicate line items' do
item = create(:item, name: "Dupe Item")
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 10)
audit = build(:audit,
storage_location: storage_location,
line_items_attributes: [
{ item_id: item.id, quantity: 3 },
{ item_id: item.id, quantity: 5 }
])

expect(audit.save).to be_falsey
expect(audit.errors.full_messages).to eq(["You have entered at least one duplicate item: Dupe Item"])
end

it "can have line items that has quantity as a positive integer" do
item = create(:item)
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 10)
Expand Down

0 comments on commit e12b3b7

Please sign in to comment.