From 2b057a26d599e5f4d44e353f98bec0f383a4217f Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 16 Aug 2018 16:18:39 +0100 Subject: [PATCH] Add primary_producer to index to reduce queries --- app/models/product_import/entry_processor.rb | 3 ++- app/models/product_import/product_importer.rb | 3 +-- app/models/product_import/spreadsheet_data.rb | 5 +++-- app/views/admin/product_import/_import_options.html.haml | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index a07b527338..58f23a1889 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -34,7 +34,8 @@ module ProductImport end def count_existing_items - @spreadsheet_data.suppliers_index.each do |_supplier_name, supplier_id| + @spreadsheet_data.suppliers_index.each do |_supplier_name, attrs| + supplier_id = attrs[:id] next unless supplier_id && permission_by_id?(supplier_id) products_count = diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 6ec0695768..e8dbb619d5 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -63,8 +63,7 @@ module ProductImport end def suppliers_index - index = @spreadsheet_data.suppliers_index - index.sort_by{ |_k, v| v.to_i }.reverse.to_h + @spreadsheet_data.suppliers_index end def supplier_products diff --git a/app/models/product_import/spreadsheet_data.rb b/app/models/product_import/spreadsheet_data.rb index 303cf1a93b..caf0b7d8e0 100644 --- a/app/models/product_import/spreadsheet_data.rb +++ b/app/models/product_import/spreadsheet_data.rb @@ -30,8 +30,9 @@ module ProductImport @suppliers_index = {} @entries.each do |entry| supplier_name = entry.supplier - supplier_id = @suppliers_index[supplier_name] || Enterprise.find_by_name(supplier_name, select: 'id, name').try(:id) - @suppliers_index[supplier_name] = supplier_id + next if @suppliers_index.key? supplier_name + enterprise = Enterprise.find_by_name(supplier_name, select: 'id, name, is_primary_producer') + @suppliers_index[supplier_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) } end @suppliers_index end diff --git a/app/views/admin/product_import/_import_options.html.haml b/app/views/admin/product_import/_import_options.html.haml index c03f16237a..2debc9a86c 100644 --- a/app/views/admin/product_import/_import_options.html.haml +++ b/app/views/admin/product_import/_import_options.html.haml @@ -1,15 +1,15 @@ %h5= t('admin.product_import.import.options_and_defaults') %br -- @importer.suppliers_index.each do |name, supplier_id| - - if name and supplier_id and @importer.permission_by_id?(supplier_id) +- @importer.suppliers_index.each do |name, attrs| + - if name and attrs[:id] and @importer.permission_by_id?(attrs[:id]) %div.panel-section.import-settings %div.panel-header{ng: {click: 'togglePanel()', class: '{active: active}'}} %div.header-icon.success %i.fa.fa-check-circle %div.header-description = name - - elsif name and supplier_id + - elsif name and attrs[:id] %div.panel-section.import-settings %div.panel-header %div.header-icon.error