Skip to content

Commit

Permalink
make use of col_sep option in import model
Browse files Browse the repository at this point in the history
  • Loading branch information
code-constructor committed Aug 16, 2024
1 parent 8ee4b08 commit 0786b96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/models/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get_normalized_csv_with_validation

def get_raw_csv
return nil if raw_csv_str.nil?
Import::Csv.new(raw_csv_str)
Import::Csv.new(raw_csv_str, col_sep:)
end

def should_initialize_csv?
Expand All @@ -103,7 +103,7 @@ def initialize_csv

# Uses the user-provided raw CSV + mappings to generate a normalized CSV for the import
def generate_normalized_csv(csv_str)
Import::Csv.create_with_field_mappings(csv_str, expected_fields, column_mappings)
Import::Csv.create_with_field_mappings(csv_str, expected_fields, column_mappings, col_sep)
end

def update_csv(row_idx, col_idx, value)
Expand Down Expand Up @@ -177,7 +177,7 @@ def define_column_mapping_keys

def raw_csv_must_be_parsable
begin
CSV.parse(raw_csv_str || "")
CSV.parse(raw_csv_str || "", col_sep:)
rescue CSV::MalformedCSVError
# i18n-tasks-use t('activerecord.errors.models.import.attributes.raw_csv_str.invalid_csv_format')
errors.add(:raw_csv_str, :invalid_csv_format)
Expand Down
14 changes: 14 additions & 0 deletions test/models/import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@ class ImportTest < ActiveSupport::TestCase
@empty_import.reload
assert @empty_import.failed?
end

test "can create transactions from csv with custom column seperator" do
loaded_import = @empty_import.dup

loaded_import.update! raw_csv_str: valid_csv_str_with_semicolon_separator, col_sep: ";"
transactions = loaded_import.dry_run

assert_equal 4, transactions.count

data = transactions.first.as_json(only: [ :name, :amount, :date ])
assert_equal data, { "amount" => "8.55", "date" => "2024-01-01", "name" => "Starbucks drink" }

assert_equal valid_csv_str, loaded_import.normalized_csv_str
end
end

0 comments on commit 0786b96

Please sign in to comment.