From f3aa5ff7846c134706fdb60465109775988b889d Mon Sep 17 00:00:00 2001 From: jparcill Date: Wed, 15 Apr 2026 16:18:47 -0400 Subject: [PATCH 1/2] Generalize duplicate modal and use for kits --- ...oller.js => duplicate_items_controller.js} | 8 ++-- app/views/audits/_form.html.erb | 5 +- app/views/kits/_form.html.erb | 4 +- spec/system/kit_system_spec.rb | 47 +++++++++++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) rename app/javascript/controllers/{audit_duplicates_controller.js => duplicate_items_controller.js} (97%) diff --git a/app/javascript/controllers/audit_duplicates_controller.js b/app/javascript/controllers/duplicate_items_controller.js similarity index 97% rename from app/javascript/controllers/audit_duplicates_controller.js rename to app/javascript/controllers/duplicate_items_controller.js index 24c8c0b4e1..f341a20c61 100644 --- a/app/javascript/controllers/audit_duplicates_controller.js +++ b/app/javascript/controllers/duplicate_items_controller.js @@ -1,6 +1,8 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { + static targets = ["itemSubmitButton"] + connect() { this.boundHandleSubmit = this.handleSubmit.bind(this) this.element.addEventListener("submit", this.boundHandleSubmit) @@ -18,11 +20,7 @@ export default class extends Controller { handleSubmit(event) { const submitter = event.submitter - if (!submitter?.name) return - if (!submitter.name.includes('save_progress') && - !submitter.name.includes('confirm_audit')) { - return - } + if (!this.itemSubmitButtonTargets.includes(submitter)) return event.preventDefault() diff --git a/app/views/audits/_form.html.erb b/app/views/audits/_form.html.erb index e2b5b4e187..9a44e4b814 100644 --- a/app/views/audits/_form.html.erb +++ b/app/views/audits/_form.html.erb @@ -13,7 +13,7 @@
- <%= simple_form_for @audit, data: { controller: "form-input audit-duplicates" }, html: {class: "storage-location-required"} do |f| %> + <%= simple_form_for @audit, data: { controller: "form-input duplicate-items" }, html: {class: "storage-location-required"} do |f| %> <%= render partial: "storage_locations/source", object: f, locals: { label: "Storage location", error: "What storage location are you auditing?", include_omitted_items: true } %>
Items in this audit @@ -33,8 +33,9 @@ <% end %>
diff --git a/app/views/kits/_form.html.erb b/app/views/kits/_form.html.erb index e5fe5ccac3..94963c311f 100644 --- a/app/views/kits/_form.html.erb +++ b/app/views/kits/_form.html.erb @@ -1,4 +1,4 @@ -<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input" }, html: { class: 'form-horizontal' } do |f| %> +<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input duplicate-items" }, html: { class: 'form-horizontal' } do |f| %>
@@ -36,7 +36,7 @@
diff --git a/spec/system/kit_system_spec.rb b/spec/system/kit_system_spec.rb index 840d8df3cb..bc7ff563df 100644 --- a/spec/system/kit_system_spec.rb +++ b/spec/system/kit_system_spec.rb @@ -226,4 +226,51 @@ expect(page).to have_content(item.name) end end + + describe "when duplicate items" do + it "detects duplicate items and shows modal", js: true do + # Disable server-side validation to test JS modal + #allow_any_instance_of(Audit).to receive(:line_items_unique_by_item_id) + visit new_kit_path + click_link "New Kit" + + kit_traits = attributes_for(:kit) + fill_in "Name", with: kit_traits[:name] + find(:css, '#kit_value_in_dollars').set('10.10') + + item = Item.last + + # Add first entry for the item + select item.name, from: "item_line_items_attributes_0_item_id" + fill_in "item_line_items_attributes_0_quantity", with: "10" + + # Add a new line item row + find("[data-form-input-target='addButton']").click + + # Add second entry for the same item + within all('.line_item_section').last do + item_select = find('select[name*="[item_id]"]') + select item.name, from: item_select[:id] + quantity_input = find('input[name*="[quantity]"]') + fill_in quantity_input[:id], with: "15" + end + + # Try to save - should trigger duplicate detection modal + click_button "Save" + + # JavaScript modal should appear + expect(page).to have_css("#duplicateItemsModal", visible: true) + expect(page).to have_content("Multiple Item Entries Detected") + expect(page).to have_content("Merge Items") + expect(page).to have_content("Make Changes") + + + # Test merge functionality + click_button "Merge Items" + + expect(page.find(".alert")).to have_content "Kit created successfully" + expect(page).to have_content(kit_traits[:name]) + expect(page).to have_content("25 #{item.name}") + end + end end From 5207853f547d9b74b0605659e71ab5ad35225fb2 Mon Sep 17 00:00:00 2001 From: jparcill Date: Wed, 15 Apr 2026 16:24:15 -0400 Subject: [PATCH 2/2] Rubocop --- spec/system/kit_system_spec.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/spec/system/kit_system_spec.rb b/spec/system/kit_system_spec.rb index bc7ff563df..20d5c93f6b 100644 --- a/spec/system/kit_system_spec.rb +++ b/spec/system/kit_system_spec.rb @@ -229,24 +229,22 @@ describe "when duplicate items" do it "detects duplicate items and shows modal", js: true do - # Disable server-side validation to test JS modal - #allow_any_instance_of(Audit).to receive(:line_items_unique_by_item_id) visit new_kit_path click_link "New Kit" kit_traits = attributes_for(:kit) fill_in "Name", with: kit_traits[:name] find(:css, '#kit_value_in_dollars').set('10.10') - + item = Item.last # Add first entry for the item select item.name, from: "item_line_items_attributes_0_item_id" fill_in "item_line_items_attributes_0_quantity", with: "10" - + # Add a new line item row find("[data-form-input-target='addButton']").click - + # Add second entry for the same item within all('.line_item_section').last do item_select = find('select[name*="[item_id]"]') @@ -254,20 +252,19 @@ quantity_input = find('input[name*="[quantity]"]') fill_in quantity_input[:id], with: "15" end - + # Try to save - should trigger duplicate detection modal click_button "Save" - + # JavaScript modal should appear expect(page).to have_css("#duplicateItemsModal", visible: true) expect(page).to have_content("Multiple Item Entries Detected") expect(page).to have_content("Merge Items") expect(page).to have_content("Make Changes") - - + # Test merge functionality click_button "Merge Items" - + expect(page.find(".alert")).to have_content "Kit created successfully" expect(page).to have_content(kit_traits[:name]) expect(page).to have_content("25 #{item.name}")