Skip to content

Commit 943428c

Browse files
rename requests_info to avoid clashing with show request_info
add coverage for Request model view
1 parent f18b920 commit 943428c

8 files changed

Lines changed: 154 additions & 45 deletions

File tree

app/controllers/requests_controller.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@ class RequestsController < ApplicationController
33
def index
44
setup_date_range_picker
55

6-
@request_info = View::Requests.from_params(params: params, organization: current_organization, helpers: helpers)
6+
@requests_info = View::Requests.from_params(params: params, organization: current_organization, helpers: helpers)
77

88
respond_to do |format|
99
format.html
10-
format.csv { send_data Exports::ExportRequestService.new(@request_info.requests).generate_csv, filename: "Requests-#{Time.zone.today}.csv" }
10+
format.csv { send_data Exports::ExportRequestService.new(@requests_info.requests).generate_csv, filename: "Requests-#{Time.zone.today}.csv" }
1111
end
1212
end
1313

1414
def show
15-
@request = current_organization.requests.find(params[:id])
16-
@item_requests = @request.item_requests.includes(:item)
17-
18-
@inventory = View::Inventory.new(@request.organization_id)
19-
@default_storage_location = @request.partner.default_storage_location_id || @request.organization.default_storage_location
20-
@location = StorageLocation.find_by(id: @default_storage_location)
21-
22-
@custom_units = Flipper.enabled?(:enable_packs) && @request.item_requests.any? { |item| item.request_unit }
15+
@request_info = View::RequestInfo.from_params(params:, organization: current_organization)
2316
end
2417

2518
# Clicking the "New Distribution" button will set the the request to started

app/models/view/request_info.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module View
2+
RequestInfo = Data.define(
3+
:request
4+
) do
5+
6+
class << self
7+
def from_params(params:, organization:)
8+
request = organization.requests.find(params[:id])
9+
10+
new(request:)
11+
end
12+
end
13+
14+
def item_requests
15+
request.item_requests.includes(:item)
16+
end
17+
18+
def inventory
19+
View::Inventory.new(request.organization_id)
20+
end
21+
22+
def default_storage_location
23+
request.partner.default_storage_location_id || request.organization.default_storage_location
24+
end
25+
26+
def location
27+
StorageLocation.find_by(id: default_storage_location)
28+
end
29+
30+
def custom_units
31+
Flipper.enabled?(:enable_packs) && request.item_requests.any? { |item| item.request_unit }
32+
end
33+
end
34+
end

app/views/requests/_calculate_product_totals.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</tr>
1717
</thead>
1818
<tbody>
19-
<% @request_info.calculate_product_totals.sort_by { |name, quantity| name.downcase }.each do |name, quantity| %>
19+
<% @requests_info.calculate_product_totals.sort_by { |name, quantity| name.downcase }.each do |name, quantity| %>
2020
<tr>
2121
<td><%= name %></td>
2222
<td><%= quantity %></td>

app/views/requests/_new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="modal-body p-4">
1111
<%= form_with(url: new_partners_request_path, method: :get) do |form| %>
1212
<div class="form-group">
13-
<%= form.collection_select(:partner_id, @request_info.partners.active, :id, :name, {include_blank: "Select a partner", required: true}, {class: "form-control mb-2"} ) %>
13+
<%= form.collection_select(:partner_id, @requests_info.partners.active, :id, :name, {include_blank: "Select a partner", required: true}, {class: "form-control mb-2"} ) %>
1414
</div>
1515
<%= submit_button({text: "Next", icon: nil, name: ""}) %>
1616
<% end %>

app/views/requests/_request_row.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<td class="date"><%= request_row.created_at.strftime("%m/%d/%Y") %></td>
33
<td><%= request_row.partner.name %> </td>
44
<td>
5-
<% partner_user = @request_info.partner_users.find { |pu| pu.id == request_row.partner_user_id } %>
5+
<% partner_user = @requests_info.partner_users.find { |pu| pu.id == request_row.partner_user_id } %>
66
<%= "#{partner_user&.formatted_email}" %>
77
</td>
88
<td class="<%= quota_column_class(request_row.total_items, request_row.partner) %> text-right">

app/views/requests/index.html.erb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@
3737
<div class="card-body">
3838
<%= form_tag(requests_path, method: :get) do |f| %>
3939
<div class="row">
40-
<% if @request_info.items.present? %>
40+
<% if @requests_info.items.present? %>
4141
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
4242
<%= filter_select(label: "Filter by item", scope: :by_request_item_id, collection: @items, selected: @selected_request_item) %>
4343
</div>
4444
<% end %>
45-
<% if @request_info.partners.present? %>
45+
<% if @requests_info.partners.present? %>
4646
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
4747
<%= filter_select(scope: :by_partner, collection: @partners, selected: @selected_partner) %>
4848
</div>
4949
<% end %>
5050
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
51-
<%= filter_select(scope: :by_request_type, collection: @request_info.request_types, key: :last, value: :first, selected: @selected_request_type) %>
51+
<%= filter_select(scope: :by_request_type, collection: @requests_info.request_types, key: :last, value: :first, selected: @selected_request_type) %>
5252
</div>
5353
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
54-
<%= filter_select(scope: :by_status, collection: @request_info.statuses, key: :last, value: :first, selected: @selected_status) %>
54+
<%= filter_select(scope: :by_status, collection: @requests_info.statuses, key: :last, value: :first, selected: @selected_status) %>
5555
</div>
5656
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
5757
<%= label_tag "Date Range" %>
@@ -67,13 +67,13 @@
6767
<%= modal_button_to("#calculateTotals", {text: "Calculate Product Totals", icon: nil, size: "md", type: "success"}) %>
6868
</div>
6969
<div class="d-flex flex-wrap gap-2">
70-
<% if @request_info.unfulfilled_requests_count > 0 %>
70+
<% if @requests_info.unfulfilled_requests_count > 0 %>
7171
<%= print_button_to(
7272
print_unfulfilled_requests_path(format: :pdf, filters: filter_params.merge(date_range: date_range_params)),
73-
text: "Print Unfulfilled Picklists (#{@request_info.unfulfilled_requests_count})",
73+
text: "Print Unfulfilled Picklists (#{@requests_info.unfulfilled_requests_count})",
7474
size: "md") %>
7575
<% end %>
76-
<%= download_button_to(requests_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)), {text: "Export Requests", size: "md"}) if @request_info.requests.any? %>
76+
<%= download_button_to(requests_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)), {text: "Export Requests", size: "md"}) if @requests_info.requests.any? %>
7777
<%= modal_button_to("#newRequest", text: "New Quantity Request", icon: "plus", type: "success") if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
7878
</div>
7979
</div>
@@ -112,13 +112,13 @@
112112
</tr>
113113
</thead>
114114
<tbody>
115-
<%= render partial: "request_row", collection: @request_info.paginated_requests %>
115+
<%= render partial: "request_row", collection: @requests_info.paginated_requests %>
116116
</tbody>
117117
</table>
118118
</div>
119119
<!-- /.card-body -->
120120
<div class="card-footer clearfix">
121-
<%= paginate @request_info.paginated_requests %>
121+
<%= paginate @requests_info.paginated_requests %>
122122
</div>
123123
<!-- /.card-footer-->
124124
</div>

app/views/requests/show.html.erb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<div class="container-fluid">
33
<div class="row mb-2">
44
<div class="col-sm-6">
5-
<% content_for :title, "Requests - #{@request.id} - #{current_organization.name}" %>
5+
<% content_for :title, "Requests - #{@request_info.request.id} - #{current_organization.name}" %>
66
<h1>
77
Request
8-
<small>from <%= @request.partner.name %></small>
8+
<small>from <%= @request_info.request.partner.name %></small>
99
</h1>
1010
</div>
1111
<div class="col-sm-6">
@@ -15,8 +15,8 @@
1515
<% end %>
1616
</li>
1717
<li class="breadcrumb-item"><%= link_to "Requests", requests_path %></li>
18-
<li class="breadcrumb-item"><a href="#"> Request from <%= @request.partner.name %>
19-
at <%= @request.created_at.to_fs(:distribution_date) %></a></li>
18+
<li class="breadcrumb-item"><a href="#"> Request from <%= @request_info.request.partner.name %>
19+
at <%= @request_info.request.created_at.to_fs(:distribution_date) %></a></li>
2020
</ol>
2121
</div>
2222
</div>
@@ -29,7 +29,7 @@
2929
<div class="col-12">
3030
<div class="card">
3131
<div class="card-header">
32-
<h3 class="card-title">This request was sent on <%= @request.created_at.to_fs(:distribution_date) %></h3>
32+
<h3 class="card-title">This request was sent on <%= @request_info.request.created_at.to_fs(:distribution_date) %></h3>
3333
</div>
3434
<div class="card-body p-0">
3535
<table class="table">
@@ -44,11 +44,11 @@
4444
</thead>
4545
<tbody>
4646
<tr>
47-
<td><%= @request.partner.name %></td>
48-
<td><%= @request.partner_user&.formatted_email %></td>
49-
<td><%= @request.request_type&.humanize %></td>
50-
<td><%= render partial: "status", locals: {status: @request.status} %></td>
51-
<td><%= @request.comments || 'None' %></td>
47+
<td><%= @request_info.request.partner.name %></td>
48+
<td><%= @request_info.request.partner_user&.formatted_email %></td>
49+
<td><%= @request_info.request.request_type&.humanize %></td>
50+
<td><%= render partial: "status", locals: {status: @request_info.request.status} %></td>
51+
<td><%= @request_info.request.comments || 'None' %></td>
5252
</tr>
5353
</table>
5454
</div>
@@ -67,47 +67,47 @@
6767
<tr>
6868
<th>Item</th>
6969
<th>Quantity</th>
70-
<% if @custom_units %>
70+
<% if @request_info.custom_units %>
7171
<th>Units (if applicable)</th>
7272
<% end %>
73-
<% if @default_storage_location %>
73+
<% if @request_info.default_storage_location %>
7474
<th>Default storage location inventory</th>
7575
<% end %>
7676
<th>Total Inventory</th>
7777
</tr>
7878
</thead>
7979
<tbody>
80-
<% @item_requests.each do |item_request| %>
80+
<% @request_info.item_requests.each do |item_request| %>
8181
<tr>
8282
<td><%= item_request.item_name %></td>
8383
<td><%= item_request.quantity %></td>
84-
<% if @custom_units %>
84+
<% if @request_info.custom_units %>
8585
<td><%= item_request.request_unit&.pluralize(item_request.quantity.to_i) %></td>
8686
<% end %>
87-
<% if @default_storage_location %>
88-
<% on_hand_for_location = @inventory.quantity_for(storage_location: @location&.id, item_id: item_request.item_id) %>
87+
<% if @request_info.default_storage_location %>
88+
<% on_hand_for_location = @request_info.inventory.quantity_for(storage_location: @request_info.location&.id, item_id: item_request.item_id) %>
8989
<td><%= on_hand_for_location&.positive? ? on_hand_for_location : 'N/A' %></td>
9090
<% end %>
91-
<% on_hand = @inventory.quantity_for(item_id: item_request.item_id) %>
91+
<% on_hand = @request_info.inventory.quantity_for(item_id: item_request.item_id) %>
9292
<td><%= on_hand || 0 %></td>
9393
</tr>
9494
<% end %>
9595
<tr>
9696
<td>Total (Quota)</td>
9797
<td>
98-
<%= @request.total_items %>
99-
<%= quota_display(@request.partner) %>
98+
<%= @request_info.request.total_items %>
99+
<%= quota_display(@request_info.request.partner) %>
100100
</td>
101101
<td />
102102
</tr>
103103
</tbody>
104104
</table>
105105
</div>
106106
<div class="card-footer flex flex-row space-x-2">
107-
<%= submit_button_to start_request_path(@request), {text: "Fulfill request", size: "md"} unless @request.distribution %>
108-
<%= view_button_to(distribution_path(@request.distribution), {text: "View Associated Distribution", size: "md"}) if @request.distribution %>
109-
<%= print_button_to print_picklist_request_path(@request), { format: :pdf, text: "Print", size: "md" } %>
110-
<%= button_to 'Cancel', new_request_cancelation_path(request_id: @request.id),
107+
<%= submit_button_to start_request_path(@request_info.request), {text: "Fulfill request", size: "md"} unless @request_info.request.distribution %>
108+
<%= view_button_to(distribution_path(@request_info.request.distribution), {text: "View Associated Distribution", size: "md"}) if @request_info.request.distribution %>
109+
<%= print_button_to print_picklist_request_path(@request_info.request), { format: :pdf, text: "Print", size: "md" } %>
110+
<%= button_to 'Cancel', new_request_cancelation_path(request_id: @request_info.request.id),
111111
method: :get, data: { disable_with: "Please wait..." }, form_class: 'd-inline', class: 'btn btn-danger btn-md' %>
112112
</div>
113113
</div>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
RSpec.describe View::RequestInfo do
2+
describe "#default_storage_location" do
3+
context "when the request partner has a default_storage_location_id" do
4+
it "returns the request partner's default_storage_location_id" do
5+
storage_location = build(:storage_location)
6+
organization = build(:organization)
7+
request = create(
8+
:request,
9+
partner: build(:partner, default_storage_location_id: storage_location.id),
10+
organization:
11+
)
12+
13+
request = View::RequestInfo.from_params(params: {id: request.id}, organization:)
14+
15+
expect(request.default_storage_location).to eq(storage_location.id)
16+
end
17+
end
18+
19+
context "when the request organization has a default_storage_location_id" do
20+
it "returns the request organization's default_storage_location_id" do
21+
organization = build(:organization)
22+
storage_location = build(:storage_location, organization:)
23+
organization.update!(default_storage_location: storage_location.id)
24+
request = create(
25+
:request,
26+
organization:
27+
)
28+
29+
request = View::RequestInfo.from_params(params: {id: request.id}, organization:)
30+
31+
expect(request.default_storage_location).to eq(storage_location.id)
32+
end
33+
end
34+
end
35+
36+
describe "#custom_units" do
37+
context "when a request has request units for an item request" do
38+
context "when enable_packs is disabled" do
39+
it "returns false" do
40+
organization = build(:organization)
41+
item = build(:item, name: "First item")
42+
create(:item_unit, item: item, name: "flat")
43+
request = create(
44+
:request,
45+
:with_item_requests,
46+
organization:,
47+
request_items: [
48+
{item_id: item.id, quantity: '559', request_unit: 'flat'}
49+
]
50+
)
51+
52+
request = View::RequestInfo.from_params(params: {id: request.id}, organization:)
53+
54+
expect(request.custom_units).to be_falsey
55+
end
56+
end
57+
58+
context "when enable_packs is enabled" do
59+
it "returns true" do
60+
Flipper.enable(:enable_packs)
61+
62+
organization = build(:organization)
63+
item = build(:item, name: "First item")
64+
create(:item_unit, item: item, name: "flat")
65+
request = create(
66+
:request,
67+
:with_item_requests,
68+
organization:,
69+
request_items: [
70+
{item_id: item.id, quantity: '559', request_unit: 'flat'},
71+
]
72+
)
73+
request = View::RequestInfo.from_params(params: {id: request.id}, organization:)
74+
75+
expect(request.custom_units).to be_truthy
76+
77+
Flipper.disable(:enable_packs)
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)