Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5021 - Fixed break of list items on validation of new barcode item #5045

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/controllers/barcode_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Provides full CRUD+ for Barcode Items. These barcode items are all associated with regular Items. The one
# anomaly here is the :find action, which has some special logic built-in to it, see the comments below.
class BarcodeItemsController < ApplicationController
before_action :load_items, only: %i[edit new]
def index
@items = current_organization.items.joins(:barcode_items)
@base_items = BaseItem.alphabetized
Expand Down Expand Up @@ -28,18 +29,17 @@ def create
end
else
flash.now[:error] = "Something didn't work quite right -- try again?"
load_items
render action: :new
end
end

def new
@barcode_item = current_organization.barcode_items.new
@items = current_organization.items.alphabetized
end

def edit
@barcode_item = current_organization.barcode_items.includes(:barcodeable).find(params[:id])
@items = current_organization.items.alphabetized
end

def show
Expand Down Expand Up @@ -79,6 +79,7 @@ def update
redirect_to barcode_items_path, notice: "Barcode updated!"
else
flash.now[:error] = "Something didn't work quite right -- try again?"
load_items
render action: :edit
end
end
Expand All @@ -102,6 +103,10 @@ def barcode_item_params
params.require(:barcode_item).permit(:value, :barcodeable_id, :quantity).merge(organization_id: current_organization.id)
end

def load_items
@items = current_organization.items.alphabetized
end

helper_method \
def filter_params
return {} unless params.key?(:filters)
Expand Down
51 changes: 51 additions & 0 deletions spec/controllers/barcode_items_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "rails_helper"

RSpec.describe "BarcodeItems", type: :system do
let(:organization) { create(:organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
let!(:barcode_items) { create_list(:barcode_item, 6, organization: organization) }
let(:items) { barcode_items.map(&:barcodeable) }
let(:barcode_item) { barcode_items.first }

context "when organizational admin is signed in" do
before do
sign_in(organization_admin)
end
describe "Creating a Barcode Item" do
before { visit new_barcode_item_path }

it "retains item dropdown options after an unsuccessful form submission" do
select items.first.name, from: "Item"
click_button "Save"
expect(page).to have_content("Something didn't work quite right -- try again?")
items.each do |item|
expect(page).to have_select("Item", with_options: [item.name])
end
end
end

describe "Editing a Barcode Item" do
before { visit edit_barcode_item_path(barcode_item) }
it "retains item dropdown options after an unsuccessful form submission" do
fill_in "Barcode", with: ""
click_button "Save"
expect(page).to have_content("Something didn't work quite right -- try again?")
items.each do |item|
expect(page).to have_select("Item", with_options: [item.name])
end
end
end

describe "when admin visits index page" do
before { visit barcode_items_path }

it "displays all barcode items" do
barcode_items.each do |barcode_item|
expect(page).to have_content(barcode_item.value)
expect(page).to have_content(barcode_item.quantity)
expect(page).to have_content(barcode_item.barcodeable_type)
end
end
end
end
end