Skip to content

Commit 176d88a

Browse files
committed
enable importable concern to display flash errors or flash alerts
1 parent c00561f commit 176d88a

File tree

7 files changed

+17
-21
lines changed

7 files changed

+17
-21
lines changed

app/controllers/concerns/importable.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ def import_csv
2727
data = File.read(params[:file].path, encoding: "BOM|UTF-8")
2828
csv = CSV.parse(data, headers: true, skip_blanks: true)
2929
if csv.count.positive? && csv.first.headers.all? { |header| !header.nil? }
30-
errors = resource_model.import_csv(csv, current_organization.id)
31-
if errors.empty?
30+
errors, warnings = resource_model.import_csv(csv, current_organization.id)
31+
if errors.empty? && warnings.empty?
3232
flash[:notice] = "#{resource_model_humanized} were imported successfully!"
33-
else
33+
elsif errors.present?
3434
flash[:error] = "The following #{resource_model_humanized} did not import successfully:\n#{errors.join("\n")}"
35+
elsif warnings.present?
36+
flash[:alert] = "The following #{resource_model_humanized} imported with warnings:\n#{warnings.join("\n")}"
3537
end
3638
else
3739
flash[:error] = "Check headers in file!"

app/models/concerns/provideable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.import_csv(csv, organization)
1515

1616
loc.save!
1717
end
18-
[]
18+
[[], []]
1919
end
2020

2121
def self.csv_export_headers

app/models/donation_site.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DonationSite < ApplicationRecord
3838

3939
def self.import_csv(csv, organization)
4040
errors = []
41+
warnings = []
4142
csv.each_with_index do |row, index|
4243
loc = DonationSite.new(row.to_hash)
4344
loc.organization_id = organization
@@ -47,7 +48,7 @@ def self.import_csv(csv, organization)
4748
errors << "Row #{index + 2}, #{row.to_hash["name"]} - #{loc.errors.full_messages.join(", ")}"
4849
end
4950
end
50-
errors
51+
[errors, warnings]
5152
end
5253

5354
def self.csv_export_headers

app/models/partner.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class Partner < ApplicationRecord
5454

5555
validate :correct_document_mime_type
5656

57-
validate :default_storage_location_belongs_to_organization
58-
5957
before_save { email&.downcase! }
6058
before_create :default_send_reminders_to_false, if: :send_reminders_nil?
6159
before_update :invite_new_partner, if: :should_invite_because_email_changed?
@@ -104,6 +102,7 @@ def approvable?
104102
# better to extract this outside of the model
105103
def self.import_csv(csv, organization_id)
106104
errors = []
105+
warnings = []
107106
organization = Organization.find(organization_id)
108107

109108
csv.each do |row|
@@ -112,12 +111,12 @@ def self.import_csv(csv, organization_id)
112111
svc = PartnerCreateService.new(organization: organization, partner_attrs: hash_rows)
113112
svc.call
114113
if svc.errors.present? && svc.partner.errors.blank?
115-
errors << "#{svc.partner.name}: #{svc.errors.full_messages.to_sentence}"
114+
warnings << "#{svc.partner.name}: #{svc.errors.full_messages.to_sentence}"
116115
elsif svc.errors.present?
117116
errors << "#{svc.partner.name}: #{svc.partner.errors.full_messages.to_sentence}"
118117
end
119118
end
120-
errors
119+
[errors, warnings]
121120
end
122121

123122
def partials_to_show
@@ -162,13 +161,6 @@ def family_zipcodes_list
162161
families.pluck(:guardian_zip_code).uniq
163162
end
164163

165-
def default_storage_location_belongs_to_organization
166-
location_ids = organization&.storage_locations&.pluck(:id)
167-
unless location_ids&.include?(default_storage_location_id) || default_storage_location_id.nil?
168-
errors.add(:default_storage_location_id, "The default storage location is not a storage location for this partner's organization")
169-
end
170-
end
171-
172164
def correct_document_mime_type
173165
if documents.attached? && documents.any? { |doc| !doc.content_type.in?(ALLOWED_MIME_TYPES) }
174166
errors.add(:documents, "Must be a PDF or DOC file")

app/models/storage_location.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def self.import_csv(csv, organization)
123123
loc.organization_id = organization
124124
loc.save!
125125
end
126-
[]
126+
[[], []]
127127
end
128128

129129
# NOTE: We should generalize this elsewhere -- Importable concern?

spec/models/donation_site_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
data = File.read(duplicated_name_csv_path, encoding: "BOM|UTF-8")
4444
csv = CSV.parse(data, headers: true)
4545

46-
errors = DonationSite.import_csv(csv, organization.id)
46+
errors, _ = DonationSite.import_csv(csv, organization.id)
4747
expect(errors).not_to be_empty
4848
expect(errors.first).to match(/Row/)
4949
expect(errors.first).to include("Name must be unique within the organization")
@@ -55,7 +55,7 @@
5555
data = File.read(valid_csv_path, encoding: "BOM|UTF-8")
5656
csv = CSV.parse(data, headers: true)
5757

58-
errors = DonationSite.import_csv(csv, organization.id)
58+
errors, _ = DonationSite.import_csv(csv, organization.id)
5959
expect(errors).to be_empty
6060
expect(DonationSite.count).to eq 1
6161

@@ -67,7 +67,7 @@
6767
data = File.read(invalid_csv_path, encoding: "BOM|UTF-8")
6868
csv = CSV.parse(data, headers: true)
6969

70-
errors = DonationSite.import_csv(csv, organization.id)
70+
errors, _ = DonationSite.import_csv(csv, organization.id)
7171
expect(errors).not_to be_empty
7272
expect(errors.first).to match(/Row/)
7373
expect(errors.first).to include("can't be blank")

spec/requests/partners_requests_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@
608608

609609
it "presents a flash error message" do
610610
subject
611-
expect(response).to have_error "The following Partners did not import successfully:\nPartner 4: default_storage_location The default storage location is not a storage location for this partner's organization"
611+
expect(flash[:alert]).to be_present
612+
expect(flash[:alert]).to match(/The following Partners imported with warnings/)
612613
end
613614
end
614615

0 commit comments

Comments
 (0)