Skip to content

Commit

Permalink
Fix missing item dropdown options on turbo stream distribution pages
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest committed Nov 30, 2024
1 parent 4f5f239 commit 006a687
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def create
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
inventory.quantity_for(storage_location: storage_loc.id).positive?
end
if @distribution.storage_location.present?
@item_labels_with_quantities = inventory
.items_for_location(@distribution.storage_location.id, include_omitted: true)
.map(&:to_dropdown_option)
end

flash_error = insufficient_error_message(result.error.message)

Expand Down
6 changes: 6 additions & 0 deletions app/models/view/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ module Types
module View
# A wrapper around event-driven InventoryAggregate for use in views.
class Inventory
ItemDropdownOption = Struct.new(:id, :name)

class ViewInventoryItem < EventTypes::EventItem
attribute :db_item, Types::Nominal::Any
delegate(*Item.column_names.map(&:to_sym), to: :db_item)

def to_dropdown_option
ItemDropdownOption.new(id: id, name: "#{name} (#{quantity})")
end
end

attr_accessor :inventory, :organization_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/line_items/_line_item_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<span class="li-name w-100">
<%= field.input :item_id,
disabled: requested.present?,
collection: @items, prompt: "Choose an item",
collection: @item_labels_with_quantities || @items, prompt: "Choose an item",
include_blank: "",
label: false,
input_html: { class: "my-0 line_item_name", "data-controller": "select2" } %>
Expand Down
11 changes: 11 additions & 0 deletions spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@
expect(response).to have_error
end

it "renders #new with item quantities in dropdowns listed" do
create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack')

post distributions_path(distribution: distribution.except(:partner_id), format: :turbo_stream)

expect(response).to have_http_status(400)
expect(flash[:error]).to include("Sorry, we weren't able to save the distribution.")

expect(response.body).to include("Item 1 (0)")
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

0 comments on commit 006a687

Please sign in to comment.