Skip to content

Commit a0d4396

Browse files
authored
Merge pull request #703 from rubyforgood/702-CSV-importing
Fixes #702: check if csv file is empty or has wrong headers
2 parents eb1c225 + e6d45a8 commit a0d4396

File tree

11 files changed

+42
-15
lines changed

11 files changed

+42
-15
lines changed

app/controllers/concerns/importable.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ module Importable
2626
def import_csv
2727
if params[:file].present?
2828
data = File.read(params[:file].path, encoding: "BOM|UTF-8")
29-
resource_model.import_csv(data, current_organization.id)
30-
flash[:notice] = "#{resource_model_humanized} were imported successfully!"
29+
csv = CSV.parse(data, headers: true).reject { |row| row.to_hash.values.any?(&:nil?) }
30+
if csv.count.positive? && csv.first.headers.all? { |header| !header.nil? }
31+
resource_model.import_csv(csv, current_organization.id)
32+
flash[:notice] = "#{resource_model_humanized} were imported successfully!"
33+
else
34+
flash[:error] = "Check headers in file!"
35+
end
3136
else
3237
flash[:error] = "No file was attached!"
3338
end

app/models/diaper_drive_participant.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def volume
3939
donations.map { |d| d.line_items.total }.reduce(:+)
4040
end
4141

42-
def self.import_csv(data, organization)
43-
CSV.parse(data, headers: true) do |row|
42+
def self.import_csv(csv, organization)
43+
csv.each do |row|
4444
loc = DiaperDriveParticipant.new(row.to_hash)
4545
loc.organization_id = organization
4646
loc.save!

app/models/donation_site.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class DonationSite < ApplicationRecord
2929
.order(:name)
3030
}
3131

32-
def self.import_csv(data, organization)
33-
CSV.parse(data, headers: true) do |row|
32+
def self.import_csv(csv, organization)
33+
csv.each do |row|
3434
loc = DonationSite.new(row.to_hash)
3535
loc.organization_id = organization
3636
loc.save!

app/models/partner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class Partner < ApplicationRecord
2929
after_create :register_on_partnerbase
3030

3131
# better to extract this outside of the model
32-
def self.import_csv(data, organization_id)
33-
CSV.parse(data, headers: true) do |row|
32+
def self.import_csv(csv, organization_id)
33+
csv.each do |row|
3434
hash_rows = Hash[row.to_hash.map { |k, v| [k.downcase, v] }]
3535
loc = Partner.new(hash_rows)
3636
loc.organization_id = organization_id

app/models/storage_location.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def distribute!(distribution)
156156
update_inventory_inventory_items(updated_quantities)
157157
end
158158

159-
def self.import_csv(data, organization)
160-
CSV.parse(data, headers: true) do |row|
159+
def self.import_csv(csv, organization)
160+
csv.each do |row|
161161
loc = StorageLocation.new(row.to_hash)
162162
loc.organization_id = organization
163163
loc.save!

spec/fixtures/wrong_headers.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,email,,
2+
3+
4+

spec/models/diaper_drive_participant_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
organization = create(:organization)
5353
import_file_path = Rails.root.join("spec", "fixtures", "diaper_drive_participants.csv")
5454
data = File.read(import_file_path, encoding: "BOM|UTF-8")
55-
DiaperDriveParticipant.import_csv(data, organization.id)
55+
csv = CSV.parse(data, headers: true)
56+
DiaperDriveParticipant.import_csv(csv, organization.id)
5657
expect(DiaperDriveParticipant.count).to eq before_import + 3
5758
end
5859
end

spec/models/donation_site_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
organization = create(:organization)
3131
import_file_path = Rails.root.join("spec", "fixtures", "donation_sites.csv")
3232
data = File.read(import_file_path, encoding: "BOM|UTF-8")
33-
DonationSite.import_csv(data, organization.id)
33+
csv = CSV.parse(data, headers: true)
34+
DonationSite.import_csv(csv, organization.id)
3435
expect(DonationSite.count).to eq 3
3536
end
3637
end

spec/models/partner_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@
6464
before_import = Partner.count
6565
import_file_path = Rails.root.join("spec", "fixtures", "partners.csv")
6666
data = File.read(import_file_path, encoding: "BOM|UTF-8")
67-
Partner.import_csv(data, organization.id)
67+
csv = CSV.parse(data, headers: true)
68+
Partner.import_csv(csv, organization.id)
6869
expect(Partner.count).to eq before_import + 3
6970
end
7071

7172
it "imports storage locations from a csv file with BOM encodings" do
7273
import_file_path = Rails.root.join("spec", "fixtures", "partners_with_bom_encoding.csv")
7374
data = File.read(import_file_path, encoding: "BOM|UTF-8")
75+
csv = CSV.parse(data, headers: true)
7476
expect do
75-
Partner.import_csv(data, organization.id)
77+
Partner.import_csv(csv, organization.id)
7678
end.to change { Partner.count }.by(20)
7779
end
7880
end

spec/models/storage_location_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@
198198
before_import = StorageLocation.count
199199
import_file_path = Rails.root.join("spec", "fixtures", "storage_locations.csv")
200200
data = File.read(import_file_path, encoding: "BOM|UTF-8")
201-
StorageLocation.import_csv(data, organization.id)
201+
csv = CSV.parse(data, headers: true)
202+
StorageLocation.import_csv(csv, organization.id)
202203
expect(StorageLocation.count).to eq before_import + 3
203204
end
204205
end

0 commit comments

Comments
 (0)