Skip to content

Commit a99bb41

Browse files
refactor audit csv exports for only audited items
1 parent 681aa87 commit a99bb41

File tree

7 files changed

+32
-81
lines changed

7 files changed

+32
-81
lines changed

app/controllers/audits_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ def index
1111
respond_to do |format|
1212
format.html
1313
format.csv do
14-
inventory = View::Inventory.new(current_organization.id)
15-
send_data Audit.generate_csv_from_inventory(@audits, inventory), filename: "Audits-#{Time.zone.today}.csv"
14+
send_data Audit.generate_csv(@audits), filename: "Audits-#{Time.zone.today}.csv"
1615
end
1716
end
1817
end

app/models/audit.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,24 @@ def user_is_organization_admin_of_the_organization
5050
end
5151
end
5252

53-
def self.generate_csv_from_inventory(audits, inventory)
54-
all_items = inventory.all_items.uniq(&:item_id).sort_by(&:name)
55-
additional_headers = all_items.map(&:name).uniq
56-
headers = csv_export_headers + additional_headers
53+
def self.generate_csv(audits)
54+
audited_items = audits.flat_map { |audit| audit.line_items.map(&:name) }.uniq
55+
headers = csv_export_headers + audited_items
5756

5857
CSV.generate(write_headers: true, headers: headers) do |csv|
5958
audits.map do |audit|
60-
sl = audit.storage_location # preloaded earlier with includes to avoid n+1
61-
total_quantity = inventory.quantity_for(storage_location: sl.id)
59+
audit_hash = audited_items.index_with(0)
60+
audit.line_items.each { |li| audit_hash[li.name] = li.quantity }
6261

63-
row = [audit.updated_at.strftime("%B %d %Y"), audit.status, sl.name, sl.address, sl.square_footage, sl.warehouse_type, total_quantity] +
64-
all_items.map { |i| inventory.quantity_for(storage_location: sl.id, item_id: i.item_id) }
62+
row = [audit.updated_at.strftime("%B %d %Y"), audit.status, audit.storage_location.name] + audit_hash.values
6563

6664
csv << row.map { |attr| normalize_csv_attribute(attr) }
6765
end
6866
end
6967
end
7068

7169
def self.csv_export_headers
72-
["Audit Date", "Audit Status", "Name", "Address", "Square Footage", "Warehouse Type", "Total Inventory"]
70+
["Audit Date", "Audit Status", "Storage Location Name"]
7371
end
7472

7573
private

app/views/audits/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<%= clear_filter_button %>
4848

4949
<span class="float-right">
50-
<%= download_button_to(audits_path(format: :csv), {text: "Export Audit", size: "md"}) %>
50+
<%= download_button_to(audits_path(format: :csv), {text: "Export Audits", size: "md"}) %>
5151
<%= new_button_to new_audit_path, {text: "New Audit"} %>
5252
</span>
5353
<% end # form %>

docs/user_guide/bank/exports.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,16 @@ For more information on these, please see the [Annual Survey Report](reports_ann
9292
- % difference in disposable diaper Donations
9393

9494
## Audits
95-
### Navigating to export Audit
96-
Click "Inventory", then "Inventory" in the left-hand menu. Then click "Export Report."
95+
### Navigating to export Audits
96+
Click "Inventory", then "Inventory Audit" in the left-hand menu. Then click "Export Audits".
9797

9898
### Contents of Audit exports
9999
For each Audit of Storage Location in the list:
100100
- Audit Date
101101
- Audit Status
102-
- Name,
103-
- Address,
104-
- Square Footage,
105-
- Warehouse Type,
106-
- Total Inventory, and
107-
- Quantity for each of the organization's Items.
102+
- Storage Location Name
103+
- Audited Items
104+
- Quantity for each of the audited items.
108105

109106
## Barcode Items
110107
### Navigating to export barcode Items

spec/models/audit_spec.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,22 @@
128128
expect(Audit.finalized_since?(xfer3, storage_location5)).to be false # since status isn't finalized
129129
end
130130

131-
describe ".generate_csv_from_inventory" do
131+
describe ".generate_csv" do
132132
let!(:audit) { create(:audit, :with_items, organization: organization) }
133+
let!(:audit_2) { create(:audit, :with_items, organization: organization) }
133134
let(:sl) { audit.storage_location }
134-
let(:inventory) { View::Inventory.new(organization.id) }
135135

136136
it "generates a CSV" do
137-
csv_data = described_class.generate_csv_from_inventory([audit], inventory)
137+
csv_data = described_class.generate_csv([audit, audit_2])
138138

139139
expect(csv_data).to be_a(String)
140-
141-
table = CSV.parse(csv_data)
142-
header = table.first
143-
row = table.second
144-
145-
expect(header).to eq(described_class.csv_export_headers)
146-
147-
expect(row[0]).to eq(audit.updated_at.strftime("%B %d %Y"))
148-
expect(row[1]).to eq(audit.status)
149-
expect(row[2]).to eq(sl.name)
150-
expect(row[3]).to eq(sl.address)
151-
expect(row[4]).to eq(sl.square_footage.to_s)
152-
expect(row[5]).to eq(sl.warehouse_type.to_s)
153-
expect(row[6]).to eq("0")
140+
expect(csv_data).to eq(
141+
<<~CSV
142+
Audit Date,Audit Status,Storage Location Name,1Dont test this,2Dont test this
143+
#{audit.updated_at.strftime("%B %d %Y")},#{audit.status},#{sl.name},#{audit.line_items.first.quantity},0
144+
#{audit_2.updated_at.strftime("%B %d %Y")},#{audit_2.status},#{sl.name},0,#{audit_2.line_items.first.quantity}
145+
CSV
146+
)
154147
end
155148
end
156149
end

spec/requests/audits_requests_spec.rb

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,23 @@
4040

4141
context "csv" do
4242
let(:response_format) { 'csv' }
43-
let!(:audits) do
44-
[
45-
create(:audit, organization: organization, storage_location: storage_location_with_duplicate_item),
46-
create(:audit, organization: organization, storage_location: storage_location_with_items)
47-
]
48-
end
49-
let(:storage_location_with_duplicate_item) {
50-
create(:storage_location,
51-
name: "Storage Location with Duplicate Items",
52-
address: "1500 Remount Road, Front Royal, VA 22630",
53-
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first,
54-
square_footage: 100)
55-
}
56-
let(:storage_location_with_items) {
57-
create(:storage_location,
58-
name: "Storage Location with Items",
59-
address: "123 Donation Site Way",
60-
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first,
61-
square_footage: 100)
62-
}
63-
let(:item1) { create(:item, name: 'A') }
64-
let(:item2) { create(:item, name: 'B') }
65-
let(:item3) { create(:item, name: 'C') }
43+
let(:csv_body) { "Stubbed CSV" }
6644

6745
before do
68-
TestInventory.create_inventory(storage_location_with_items.organization, {
69-
storage_location_with_items.id => {
70-
item1.id => 1,
71-
item2.id => 1,
72-
item3.id => 1
73-
},
74-
storage_location_with_duplicate_item.id => { item3.id => 1 }
75-
})
46+
allow(Audit).to receive(:generate_csv).and_return(csv_body)
7647
end
7748

7849
it "succeeds" do
7950
get audits_path(format: response_format)
51+
expect(response.content_type).to eq("text/csv")
8052
expect(response).to be_successful
8153
end
8254

83-
it "includes headers followed by alphabetized item names" do
55+
it "returns a CSV" do
8456
get audits_path(format: response_format)
85-
expect(response.body.split("\n")[0]).to eq([Audit.csv_export_headers, item1.name, item2.name, item3.name].join(','))
86-
end
8757

88-
it "Generates csv with Storage Location fields, alphabetized item names, item quantities lined up in their columns, and zeroes for no inventory" do
89-
get audits_path(format: response_format)
90-
csv = <<~CSV
91-
Audit Date,Audit Status,Name,Address,Square Footage,Warehouse Type,Total Inventory,A,B,C
92-
#{audits[0].updated_at.strftime("%B %d %Y")},in_progress,Storage Location with Duplicate Items,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,1,0,0,1
93-
#{audits[1].updated_at.strftime("%B %d %Y")},in_progress,Storage Location with Items,123 Donation Site Way,100,Residential space used,3,1,1,1
94-
CSV
95-
expect(response.body).to eq(csv)
58+
expect(Audit).to have_received(:generate_csv)
59+
expect(response.body).to eq(csv_body)
9660
end
9761
end
9862
end

spec/system/audit_system_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@
135135
context "when viewing the audits index" do
136136
subject { audits_path }
137137

138-
it "should have the 'Export Audit' button" do
138+
it "should have the 'Export Audits' button" do
139139
visit subject
140-
expect(page).to have_content(" Export Audit")
140+
expect(page).to have_content(" Export Audits")
141141
end
142142

143143
it "should be able to filter the #index by storage location" do

0 commit comments

Comments
 (0)