Skip to content

Commit

Permalink
Fix Distribution#new,#create,#edit,#index to only show active items
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest committed Dec 12, 2024
1 parent e1e3d11 commit e7d9bf3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def index
.includes(:partner, :storage_location)
.class_filter(scope_filters)
@paginated_distributions = @distributions.page(params[:page])
@items = current_organization.items.alphabetized.select(:id, :name)
@items = current_organization.items.active.alphabetized.select(:id, :name)
@item_categories = current_organization.item_categories.select(:id, :name)
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select(:id, :name)
@partners = current_organization.partners.active.alphabetized.select(:id, :name)
Expand Down Expand Up @@ -117,7 +117,7 @@ def create
elsif request_id
@distribution.initialize_request_items
end
@items = current_organization.items.alphabetized
@items = current_organization.items.active.alphabetized
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized

inventory = View::Inventory.new(@distribution.organization_id)
Expand Down Expand Up @@ -147,7 +147,7 @@ def new
@distribution.line_items.build
@distribution.copy_from_donation(params[:donation_id], params[:storage_location_id])
end
@items = current_organization.items.alphabetized
@items = current_organization.items.active.alphabetized
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized

inventory = View::Inventory.new(current_organization.id)
Expand All @@ -173,7 +173,7 @@ def edit
if (!@distribution.complete? && @distribution.future?) ||
current_user.has_role?(Role::ORG_ADMIN, current_organization)
@distribution.line_items.build if @distribution.line_items.size.zero?
@items = current_organization.items.alphabetized
@items = current_organization.items.active.alphabetized
@partner_list = current_organization.partners.alphabetized
@audit_warning = current_organization.audits
.where(storage_location_id: @distribution.storage_location_id)
Expand Down Expand Up @@ -204,7 +204,7 @@ def update
flash[:error] = insufficient_error_message(result.error.message)
@distribution.line_items.build if @distribution.line_items.size.zero?
@distribution.initialize_request_items
@items = current_organization.items.alphabetized
@items = current_organization.items.active.alphabetized
@storage_locations = current_organization.storage_locations.active_locations.alphabetized
render :edit
end
Expand Down
1 change: 0 additions & 1 deletion app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Distribution < ApplicationRecord

enum state: { scheduled: 5, complete: 10 }
enum delivery_method: { pick_up: 0, delivery: 1, shipped: 2 }
scope :active, -> { joins(:line_items).joins(:items).where(items: { active: true }) }
# add item_id scope to allow filtering distributions by item
scope :by_item_id, ->(item_id) { includes(:items).where(items: { id: item_id }) }
# partner scope to allow filtering by partner
Expand Down
53 changes: 52 additions & 1 deletion spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
end

describe "GET #index" do
let(:item) { create(:item, value_in_cents: 100, organization: organization) }
let(:item) { create(:item, name: "Active Item", value_in_cents: 100, organization: organization) }
let!(:distribution) { create(:distribution, :with_items, :past, item: item, item_quantity: 10, organization: organization) }

it "returns http success" do
Expand All @@ -88,6 +88,18 @@
expect(response.body).not_to match(/Has Inactive Items/)
end

it "renders only active items in dropdown" do
create(:item, :inactive, organization: organization, name: 'Inactive Item')

get distributions_path
page = Nokogiri::HTML(response.body)

selectable_items = page.at_css("select[name='filters\[by_item_id\]']").text.split("\n")

expect(selectable_items).to include("Active Item")
expect(selectable_items).not_to include("Inactive Item")
end

context "with a disabled item" do
before do
item.update(active: false)
Expand Down Expand Up @@ -233,6 +245,20 @@
expect(response).to have_error
end

it "renders #new on failure with only active items in dropdown" do
create(:item, organization: organization, name: 'Active Item')
create(:item, :inactive, organization: organization, name: 'Inactive Item')

post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream)
expect(response).to have_http_status(400)

page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select.line_item_name").text.split("\n")

expect(selectable_items).to include("Active Item")
expect(selectable_items).not_to include("Inactive Item")
end

context "Deactivated partners should not be displayed in partner dropdown" do
before do
create(:partner, name: 'Active Partner', organization: organization, status: "approved")
Expand Down Expand Up @@ -275,6 +301,18 @@
expect(page.css('#distribution_storage_location_id option[selected]')).to be_empty
end

it "should only show active items in item dropdown" do
create(:item, :inactive, organization: organization, name: 'Inactive Item')

get new_distribution_path(default_params)

page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")

expect(selectable_items).to include("Item 1", "Item 2")
expect(selectable_items).not_to include("Inactive Item")
end

context "with org default but no partner default" do
it "selects org default" do
organization.update!(default_storage_location: storage_location.id)
Expand Down Expand Up @@ -572,6 +610,19 @@
expect(response.body).to include("Active Partner")
end

it "should only show active items in item dropdown" do
create(:item, organization: organization, name: 'Active Item')
create(:item, :inactive, organization: organization, name: 'Inactive Item')

get edit_distribution_path(id: distribution.id)

page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")

expect(selectable_items).to include("Active Item")
expect(selectable_items).not_to include("Inactive Item")
end

context 'with units' do
let!(:request) {
create(:request,
Expand Down

0 comments on commit e7d9bf3

Please sign in to comment.