diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index ffc3711da6..2aaea795e1 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/app/models/distribution.rb b/app/models/distribution.rb index 1d11c25d7b..08b6fdbb29 100644 --- a/app/models/distribution.rb +++ b/app/models/distribution.rb @@ -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 diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index 0f0d15a3a3..bf5934184d 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -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 @@ -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) @@ -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") @@ -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) @@ -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,