From b78716c283099ba4cd34ed129c482be79eef1cd8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 8 Oct 2018 13:59:33 +0100 Subject: [PATCH 1/2] Fix missing local variable --- app/models/product_import/entry_processor.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 315721ffbb..7c0ee54575 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -139,7 +139,8 @@ module ProductImport @inventory_created += 1 @updated_ids.push new_item.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} #{line_number}:", new_item.errors.full_messages) + @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ + #{entry.line_number}:", new_item.errors.full_messages) end end @@ -153,7 +154,8 @@ module ProductImport @inventory_updated += 1 @updated_ids.push existing_item.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} #{line_number}:", existing_item.errors.full_messages) + @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ + #{entry.line_number}:", existing_item.errors.full_messages) end end @@ -177,7 +179,8 @@ module ProductImport @products_created += 1 @updated_ids.push product.variants.first.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} #{line_number}:", product.errors.full_messages) + @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ + #{entry.line_number}:", product.errors.full_messages) end @already_created[entry.supplier_id] = { entry.name => product.id } @@ -192,7 +195,8 @@ module ProductImport @updated_ids.push variant.id true else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} #{line_number}:", variant.errors.full_messages) + @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ + #{entry.line_number}:", variant.errors.full_messages) false end end From 57c6530e4658437b7143f4793e28819684e026c3 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 11 Oct 2018 16:04:45 +0100 Subject: [PATCH 2/2] DRY code and use more flexible I18n --- app/models/product_import/entry_processor.rb | 20 ++++++++++++-------- config/locales/en.yml | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 7c0ee54575..7766d7b8e0 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -139,8 +139,7 @@ module ProductImport @inventory_created += 1 @updated_ids.push new_item.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ - #{entry.line_number}:", new_item.errors.full_messages) + assign_errors new_item.errors.full_messages, entry.line_number end end @@ -154,8 +153,7 @@ module ProductImport @inventory_updated += 1 @updated_ids.push existing_item.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ - #{entry.line_number}:", existing_item.errors.full_messages) + assign_errors existing_item.errors.full_messages, entry.line_number end end @@ -179,8 +177,7 @@ module ProductImport @products_created += 1 @updated_ids.push product.variants.first.id else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ - #{entry.line_number}:", product.errors.full_messages) + assign_errors product.errors.full_messages, entry.line_number end @already_created[entry.supplier_id] = { entry.name => product.id } @@ -195,12 +192,19 @@ module ProductImport @updated_ids.push variant.id true else - @importer.errors.add("#{I18n.t('admin.product_import.model.line')} \ - #{entry.line_number}:", variant.errors.full_messages) + assign_errors variant.errors.full_messages, entry.line_number false end end + def assign_errors(errors, line_number) + @importer.errors.add( + I18n.t('admin.product_import.model.line_number', + number: line_number), + errors + ) + end + def assign_defaults(object, entry) # Assigns a default value for a specified field e.g. category='Vegetables', setting this value # either for all entries (overwrite_all), or only for those entries where the field was blank diff --git a/config/locales/en.yml b/config/locales/en.yml index ea6a746d97..51d776855a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -520,7 +520,7 @@ en: products_no_permission: you do not have permission to manage products for this enterprise inventory_no_permission: you do not have permission to create inventory for this producer none_saved: did not save any products successfully - line: Line + line_number: "Line %{number}:" index: select_file: Select a spreadsheet to upload spreadsheet: Spreadsheet