Skip to content
Merged
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
27 changes: 27 additions & 0 deletions spec/requests/barcode_items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@
end
end

context 'while signed in as organization admin' do
let!(:barcode_items) { create_list(:barcode_item, 6, organization: organization) }
let(:items) { barcode_items.map(&:barcodeable) }
let(:barcode_item) { barcode_items.first }
before do
sign_in(organization_admin)
end

describe "GET #new" do
it "The new barcode item form should contain the Items even after a failed save" do
post barcode_items_path, params: { barcode_item: {value: 10, quantity: 20 } }
items.each do |item|
expect(response.body).to include(item.name)
end
end
end

describe "GET #edit" do
it "The Edit Barcode Item form should contain the Items even after a failed update" do
put barcode_item_path(barcode_item.id), params: { barcode_item: { value: 10, quantity: '', barcodeable_id: items.first.id} }
items.each do |item|
expect(response.body).to include(item.name)
end
end
end
end

# For the time being, users cannot access these routes, but this may change in
# the near future.
# context "While not signed in" do
Expand Down