Skip to content

Commit b1e4750

Browse files
authored
Fix Distribution#new,#create,#edit to only show active items (#4848)
1 parent 79a8e84 commit b1e4750

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

app/controllers/distributions_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def create
117117
elsif request_id
118118
@distribution.initialize_request_items
119119
end
120-
@items = current_organization.items.alphabetized
120+
@items = current_organization.items.active.alphabetized
121121
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized
122122

123123
inventory = View::Inventory.new(@distribution.organization_id)
@@ -147,7 +147,7 @@ def new
147147
@distribution.line_items.build
148148
@distribution.copy_from_donation(params[:donation_id], params[:storage_location_id])
149149
end
150-
@items = current_organization.items.alphabetized
150+
@items = current_organization.items.active.alphabetized
151151
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized
152152

153153
inventory = View::Inventory.new(current_organization.id)
@@ -173,7 +173,7 @@ def edit
173173
if (!@distribution.complete? && @distribution.future?) ||
174174
current_user.has_role?(Role::ORG_ADMIN, current_organization)
175175
@distribution.line_items.build if @distribution.line_items.size.zero?
176-
@items = current_organization.items.alphabetized
176+
@items = current_organization.items.active.alphabetized
177177
@partner_list = current_organization.partners.alphabetized
178178
@audit_warning = current_organization.audits
179179
.where(storage_location_id: @distribution.storage_location_id)
@@ -204,7 +204,7 @@ def update
204204
flash[:error] = insufficient_error_message(result.error.message)
205205
@distribution.line_items.build if @distribution.line_items.size.zero?
206206
@distribution.initialize_request_items
207-
@items = current_organization.items.alphabetized
207+
@items = current_organization.items.active.alphabetized
208208
@storage_locations = current_organization.storage_locations.active_locations.alphabetized
209209
render :edit
210210
end

app/models/distribution.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Distribution < ApplicationRecord
4646

4747
enum state: { scheduled: 5, complete: 10 }
4848
enum delivery_method: { pick_up: 0, delivery: 1, shipped: 2 }
49-
scope :active, -> { joins(:line_items).joins(:items).where(items: { active: true }) }
5049
# add item_id scope to allow filtering distributions by item
5150
scope :by_item_id, ->(item_id) { includes(:items).where(items: { id: item_id }) }
5251
# partner scope to allow filtering by partner

spec/requests/distributions_requests_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@
233233
expect(response).to have_error
234234
end
235235

236+
it "renders #new on failure with only active items in dropdown" do
237+
create(:item, organization: organization, name: 'Active Item')
238+
create(:item, :inactive, organization: organization, name: 'Inactive Item')
239+
240+
post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream)
241+
expect(response).to have_http_status(400)
242+
243+
page = Nokogiri::HTML(response.body)
244+
selectable_items = page.at_css("select.line_item_name").text.split("\n")
245+
246+
expect(selectable_items).to include("Active Item")
247+
expect(selectable_items).not_to include("Inactive Item")
248+
end
249+
236250
context "Deactivated partners should not be displayed in partner dropdown" do
237251
before do
238252
create(:partner, name: 'Active Partner', organization: organization, status: "approved")
@@ -275,6 +289,18 @@
275289
expect(page.css('#distribution_storage_location_id option[selected]')).to be_empty
276290
end
277291

292+
it "should only show active items in item dropdown" do
293+
create(:item, :inactive, organization: organization, name: 'Inactive Item')
294+
295+
get new_distribution_path(default_params)
296+
297+
page = Nokogiri::HTML(response.body)
298+
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")
299+
300+
expect(selectable_items).to include("Item 1", "Item 2")
301+
expect(selectable_items).not_to include("Inactive Item")
302+
end
303+
278304
context "with org default but no partner default" do
279305
it "selects org default" do
280306
organization.update!(default_storage_location: storage_location.id)
@@ -572,6 +598,19 @@
572598
expect(response.body).to include("Active Partner")
573599
end
574600

601+
it "should only show active items in item dropdown" do
602+
create(:item, organization: organization, name: 'Active Item')
603+
create(:item, :inactive, organization: organization, name: 'Inactive Item')
604+
605+
get edit_distribution_path(id: distribution.id)
606+
607+
page = Nokogiri::HTML(response.body)
608+
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")
609+
610+
expect(selectable_items).to include("Active Item")
611+
expect(selectable_items).not_to include("Inactive Item")
612+
end
613+
575614
context 'with units' do
576615
let!(:request) {
577616
create(:request,

0 commit comments

Comments
 (0)