Skip to content

Commit

Permalink
Handle empty arrays
Browse files Browse the repository at this point in the history
The previous commit failed to account for the case where `#find_by_sql`
returns no results.
  • Loading branch information
stevepolitodesign committed Jul 11, 2024
1 parent abf6a42 commit 189693b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/art_vandelay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def csv
elsif records.is_a?(ActiveRecord::Base)
csv_exports << CSV.parse(generate_csv(records), headers: true)
elsif records.is_a?(Array)
csv_exports << CSV.parse(generate_csv(records), headers: true)
if records.any?
csv_exports << CSV.parse(generate_csv(records), headers: true)
end
end

Result.new(csv_exports)
Expand Down Expand Up @@ -125,6 +127,8 @@ def model_name
records.model_name.name
when Array
records.first.model_name.name
else
raise "Unsupported export: #{records.class}"
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/art_vandelay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class Export < ArtVandelayTest
)
end

test "it does not create a CSV when passed an empty Array" do
result = ArtVandelay::Export.new([]).csv

assert_empty result.csv_exports
end

test "it controlls what data is filtered" do
user = User.create!(email: "[email protected]", password: "password")
ArtVandelay.setup do |config|
Expand Down

0 comments on commit 189693b

Please sign in to comment.