Skip to content

Commit 5c1d33c

Browse files
committed
Remove old parsing and related column
1 parent b544202 commit 5c1d33c

File tree

7 files changed

+38
-93
lines changed

7 files changed

+38
-93
lines changed

app/jobs/parse_customer_import_job.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ class ParseCustomerImportJob < ApplicationJob
44
end
55

66
def perform(customer_import)
7-
if customer_import.parsed_header_at?
8-
customer_import.parse_with_dynamic_headers!
9-
else
10-
customer_import.parse!
11-
end
7+
customer_import.parse_with_dynamic_headers!
128
rescue => e
139
customer_import.update(
1410
parsing_failed_at: Time.zone.now,

app/models/customer_import.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,6 @@ def can_be_finalized?
6161
started_finalizing_at.nil? && finalized_at.nil?
6262
end
6363

64-
def parse!
65-
return if parsed_at?
66-
67-
csv_contents = uploaded_file.download
68-
csv_contents.force_encoding("UTF-8")
69-
70-
transaction do
71-
CSV.parse(csv_contents, headers: true) do |row_from_csv|
72-
rows.create!(parsed_data: row_from_csv.to_hash)
73-
end
74-
75-
touch(:parsed_at)
76-
end
77-
end
78-
7964
def parse_header!
8065
return if parsed_header_at?
8166

app/models/customer_import_row.rb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ def as_customer_attributes
5959
private
6060

6161
def read_value_from_column(column)
62-
if owner.header_data
63-
index = header_index_for(column)
64-
index ? cell_data[index] : nil
65-
else
66-
parsed_data[column]
67-
end
62+
index = header_index_for(column)
63+
index ? cell_data[index] : nil
6864
end
6965

7066
def header_index_for(column)
@@ -82,13 +78,8 @@ def check_for_errors
8278

8379
def check_for_duplicate_in_file
8480
if customer_name.present?
85-
existing_row_with_same_customer_name =
86-
if owner.header_data
87-
index = header_index_for("customer_name")
88-
index ? owner.rows.where("cell_data->>#{index} = ?", customer_name).first : nil
89-
else
90-
owner.rows.where("parsed_data->>'customer_name' = ?", customer_name).first
91-
end
81+
index_of_customer_name = header_index_for("customer_name")
82+
existing_row_with_same_customer_name = owner.rows.where("cell_data->>#{index_of_customer_name} = ?", customer_name).first
9283

9384
if existing_row_with_same_customer_name
9485
self.duplicated = true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DeleteParsedData < ActiveRecord::Migration[5.2]
2+
def change
3+
remove_column :customer_import_rows, :parsed_data
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2018_11_23_052441) do
13+
ActiveRecord::Schema.define(version: 2018_12_08_183038) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -40,7 +40,6 @@
4040
t.datetime "created_at", null: false
4141
t.datetime "updated_at", null: false
4242
t.bigint "owner_id", null: false
43-
t.json "parsed_data"
4443
t.boolean "with_errors", default: false, null: false
4544
t.boolean "duplicated", default: false, null: false
4645
t.string "error_message"

test/controllers/customer_imports_controller_test.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ class CustomerImportsControllerTest < ActionDispatch::IntegrationTest
3333
space = Space.create!(title: "Zeobix", slug: "ZBX")
3434
customer_import = space.customer_imports.new
3535

36+
csv_headers = ["customer_name", "address", "city", "country_name", "country_code"]
37+
3638
begin
3739
csv_file = Tempfile.new(["upload", ".csv"])
3840

3941
CSV.open(csv_file.path, "wb") do |csv|
40-
csv << ["customer_name", "address", "city", "country_name", "country_code"]
42+
csv << csv_headers
4143
csv << ["Shape A/S", "Njalsgade 17A", "Copenhagen", "", "dk"]
4244
csv << ["Devotus", "", "Hvidovre", "", "dk"]
4345
end
@@ -51,7 +53,13 @@ class CustomerImportsControllerTest < ActionDispatch::IntegrationTest
5153
csv_file.unlink
5254
end
5355

54-
customer_import.parse!
56+
customer_import.header_data = {
57+
"as_array" => csv_headers,
58+
"index_mapping" => { "customer_name" => 0, "address" => 1, "city" => 2, "country_code" => 4 },
59+
}
60+
customer_import.save!
61+
62+
customer_import.parse_with_dynamic_headers!
5563

5664
assert_difference "space.customers.count", 2 do
5765
perform_enqueued_jobs do

test/models/customer_import_test.rb

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,6 @@
44
class CustomerImportTest < ActiveSupport::TestCase
55
test "parsing rows" do
66
space = Space.create!(title: "Zeobix", slug: "ZBX")
7-
space.customers.create!(name: "Farm Backup")
8-
9-
customer_import = space.customer_imports.new
10-
11-
begin
12-
csv_file = Tempfile.new(["upload", ".csv"])
13-
14-
CSV.open(csv_file.path, "wb") do |csv|
15-
csv << ["customer_name", "address", "city", "country_name", "country_code", "user_name", "user_email"]
16-
csv << ["Shape A/S", "Njalsgade 17A", "København S", "", "dk", "Bacon", "bacon.the.dog@example.com"]
17-
csv << ["Farm Backup", "Njalsgade 23C", "Copenhagen", "", "dk"]
18-
csv << ["", "-", "-", "", "dk"]
19-
csv << ["Duplicate Company", "", "", "", ""]
20-
csv << ["Duplicate Company", "", "", "", ""]
21-
end
22-
23-
csv_file.rewind
24-
25-
customer_import.uploaded_file.attach(io: csv_file, filename: "upload.csv")
26-
customer_import.save!
27-
ensure
28-
csv_file.close
29-
csv_file.unlink
30-
end
31-
32-
customer_import.parse!
33-
34-
assert_equal 5, customer_import.rows.count
35-
assert_equal 1, customer_import.rows_with_errors.count
36-
assert_equal 2, customer_import.rows_with_duplicates.count
37-
assert_equal 2, customer_import.valid_rows.count
38-
39-
assert_equal ["blank_name"], customer_import.rows_with_errors.pluck(:error_message)
40-
assert_equal ["customer_already_exists", "duplicate_in_file"], customer_import.rows_with_duplicates.order(:id).pluck(:error_message)
41-
end
42-
43-
test "parsing rows with dynamic columns" do
44-
space = Space.create!(title: "Zeobix", slug: "ZBX")
457

468
csv_headers = ["Kunde", "Adresse", "By", "Land", "Brugerens fulde navn", "Bruger-email"]
479

@@ -73,29 +35,28 @@ class CustomerImportTest < ActiveSupport::TestCase
7335

7436
test "finalizing import" do
7537
space = Space.create!(title: "Zeobix", slug: "ZBX")
76-
customer_import = space.customer_imports.new
7738

78-
begin
79-
csv_file = Tempfile.new(["upload", ".csv"])
39+
csv_headers = ["customer_name", "address", "city", "country_name", "country_code", "user_name", "user_email"]
8040

81-
CSV.open(csv_file.path, "wb") do |csv|
82-
csv << ["customer_name", "address", "city", "country_name", "country_code", "user_name", "user_email"]
83-
csv << ["Shape A/S", "Njalsgade 17A", "København S", "", "dk", "Bacon", "bacon.the.dog@example.com"]
84-
csv << ["", "-", "-", "", "dk"]
85-
csv << ["Duplicate Company", "", "", "", ""]
86-
csv << ["Duplicate Company", "", "", "", ""]
87-
end
41+
csv_contents = ""
42+
CSV.generate(csv_contents, headers: csv_headers, write_headers: true) do |csv|
43+
csv << ["Shape A/S", "Njalsgade 17A", "København S", "", "dk", "Bacon", "bacon.the.dog@example.com"]
44+
csv << ["", "-", "-", "", "dk"]
45+
csv << ["Duplicate Company", "", "", "", ""]
46+
csv << ["Duplicate Company", "", "", "", ""]
47+
end
8848

89-
csv_file.rewind
49+
csv_file = StringIO.new(csv_contents)
9050

91-
customer_import.uploaded_file.attach(io: csv_file, filename: "upload.csv")
92-
customer_import.save!
93-
ensure
94-
csv_file.close
95-
csv_file.unlink
96-
end
51+
customer_import = space.customer_imports.new
52+
customer_import.uploaded_file.attach(io: csv_file, filename: "upload.csv")
53+
customer_import.header_data = {
54+
"as_array" => csv_headers,
55+
"index_mapping" => { "customer_name" => 0, "address" => 1, "city" => 2, "country_code" => 4, "user_name" => 5, "user_email" => 6 },
56+
}
57+
customer_import.save!
9758

98-
customer_import.parse!
59+
customer_import.parse_with_dynamic_headers!
9960

10061
assert_difference -> { space.customers.count } => 2, -> { space.users.count } => 1 do
10162
customer_import.finalize!

0 commit comments

Comments
 (0)