Skip to content

Commit

Permalink
Merge pull request #4531 from rubyforgood/partner-req-units-display-4399
Browse files Browse the repository at this point in the history
Partner request units display (packs-4) #4399
  • Loading branch information
cielf authored Jul 27, 2024
2 parents d13ef01 + 9e59dba commit 3d073eb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
9 changes: 8 additions & 1 deletion app/views/partners/requests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@
<span class='text-2xl font-bold'>Requested Items:</span>
<ul class='text-lg'>
<% @partner_request.item_requests.each do |item| %>
<li><%= item.quantity %> of <%= item.name %></li>
<li>
<%= item.quantity %>
<% if Flipper.enabled?(:enable_packs) && item.request_unit %>
<%= item.request_unit.pluralize(item.quantity.to_i) %>
<% end %>
of
<%= item.name %>
</li>
<% end %>
</ul>
</div>
Expand Down
55 changes: 37 additions & 18 deletions spec/factories/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,48 @@ def random_request_items
request_items { random_request_items }
comments { "Urgent" }
partner_user { ::User.partner_users.first || create(:partners_user) }
end

trait :started do
status { 'started' }
end
# For compatibility we can take in a list of request_items and turn it into a
# list of item_requests
trait :with_item_requests do
after(:build) do |request|
if request.item_requests.empty?
request.request_items.each do |request_item|
item = Item.find(request_item['item_id'])
request.item_requests << Partners::ItemRequest.new(
item_id: item.id,
quantity: request_item['quantity'],
name: item.name,
partner_key: item.partner_key,
request_unit: request_item["request_unit"]
)
end
end
end
end

trait :fulfilled do
status { 'fulfilled' }
end
trait :started do
status { 'started' }
end

trait :pending do
status { 'pending' }
end
trait :fulfilled do
status { 'fulfilled' }
end

trait :pending do
status { 'pending' }
end

trait :with_varied_quantities do
request_items {
# get 10 unique item ids
keys = Item.active.pluck(:id).sample(10)
trait :with_varied_quantities do
request_items {
# get 10 unique item ids
keys = Item.active.pluck(:id).sample(10)

# This *could* pass in error -- if the plucking order happens to match the end order.
# This *could* pass in error -- if the plucking order happens to match the end order.

item_quantities = [50, 150, 75, 125, 200, 3, 15, 88, 46, 22]
keys.map.with_index { |k, i| { "item_id" => k, "quantity" => item_quantities[i]} }
}
item_quantities = [50, 150, 75, 125, 200, 3, 15, 88, 46, 22]
keys.map.with_index { |k, i| { "item_id" => k, "quantity" => item_quantities[i]} }
}
end
end
end
32 changes: 32 additions & 0 deletions spec/requests/partners/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@
get partners_request_path(other_request)
expect(response.code).to eq("404")
end

it 'should show the units if they are provided and enabled' do
item1 = create(:item, name: "First item")
item2 = create(:item, name: "Second item")
item3 = create(:item, name: "Third item")
create(:item_unit, item: item1, name: "flat")
create(:item_unit, item: item2, name: "flat")
create(:item_unit, item: item3, name: "flat")
request = create(
:request,
:with_item_requests,
partner_id: partner.id,
partner_user_id: partner_user.id,
request_items: [
{item_id: item1.id, quantity: '125'},
{item_id: item2.id, quantity: '559', request_unit: 'flat'},
{item_id: item3.id, quantity: '1', request_unit: 'flat'}
]
)

Flipper.enable(:enable_packs)
get partners_request_path(request)
expect(response.body).to match(/125\s+of\s+First item/m)
expect(response.body).to match(/559\s+flats\s+of\s+Second item/m)
expect(response.body).to match(/1\s+flat\s+of\s+Third item/m)

Flipper.disable(:enable_packs)
get partners_request_path(request)
expect(response.body).to match(/125\s+of\s+First item/m)
expect(response.body).to match(/559\s+of\s+Second item/m)
expect(response.body).to match(/1\s+of\s+Third item/m)
end
end

describe "POST #create" do
Expand Down

0 comments on commit 3d073eb

Please sign in to comment.