Skip to content

Commit

Permalink
Adds basic support for queries generated by #find_by_sql
Browse files Browse the repository at this point in the history
Relates to #32

Allows exporting data that was generated by [find_by_sql][]. This can be desirable when the caller is required to export a calculated field that is not stored directly on the table as demonstrated below.

```ruby
User.find_by_sql("SELECT id, 'custom_value' AS custom_field FROM users")
ArtVandelay::Export.new(export).csv
```

As such, this feature does not support the `in_batches_of` option, since
that would be the responsibility of the query.

This feature also does not (yet) support the `attributes` option.

[find_by_sql]: https://api.rubyonrails.org/classes/ActiveRecord/Querying.html#method-i-find_by_sql
  • Loading branch information
stevepolitodesign committed Jul 11, 2024
1 parent 21daf91 commit abf6a42
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/art_vandelay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def csv
end
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)
end

Result.new(csv_exports)
Expand Down Expand Up @@ -88,7 +90,7 @@ def filtered_values(attributes)
def generate_csv(relation)
CSV.generate do |csv|
csv << header
if relation.is_a?(ActiveRecord::Relation)
if relation.is_a?(ActiveRecord::Relation) || relation.is_a?(Array)
relation.each do |record|
csv << row(record.attributes)
end
Expand All @@ -104,7 +106,12 @@ def header
standardized_attributes.include?(column_name)
end
else
model.attribute_names
case records
when ActiveRecord::Relation, ActiveRecord::Base
model.attribute_names
when Array
records.first.attributes.keys
end
end
end

Expand All @@ -113,7 +120,12 @@ def model
end

def model_name
records.model_name.name
case records
when ActiveRecord::Relation, ActiveRecord::Base
records.model_name.name
when Array
records.first.model_name.name
end
end

def row(attributes)
Expand Down
20 changes: 20 additions & 0 deletions test/art_vandelay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ class Export < ArtVandelayTest
)
end

test "it creates a CSV when passed an Array of ActiveRecord instances" do
user = User.create!(email: "[email protected]", password: "password")
export = User.find_by_sql("SELECT id, 'custom_value' AS custom_field FROM users")

result = ArtVandelay::Export.new(export).csv
csv = result.csv_exports.first

assert_equal(
[
["id", "custom_field"],
[user.id.to_s, "custom_value"]
],
csv.to_a
)
assert_equal(
["id", "custom_field"],
csv.headers
)
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 abf6a42

Please sign in to comment.