mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refactor long lines for new character limit
This commit is contained in:
@@ -46,7 +46,10 @@ module ProductImport
|
||||
|
||||
products_count =
|
||||
if settings.importing_into_inventory?
|
||||
VariantOverride.where('variant_overrides.hub_id IN (?)', enterprise_id).count
|
||||
VariantOverride.where(
|
||||
'variant_overrides.hub_id IN (?)',
|
||||
enterprise_id
|
||||
).count
|
||||
else
|
||||
Spree::Variant.
|
||||
not_deleted.
|
||||
@@ -158,7 +161,9 @@ module ProductImport
|
||||
# If we've already added a new product with these attributes
|
||||
# from this spreadsheet, mark this entry as a new variant with
|
||||
# the new product id, as this is a now variant of that product...
|
||||
if @already_created[entry.enterprise_id] && @already_created[entry.enterprise_id][entry.name]
|
||||
if @already_created[entry.enterprise_id] &&
|
||||
@already_created[entry.enterprise_id][entry.name]
|
||||
|
||||
product_id = @already_created[entry.enterprise_id][entry.name]
|
||||
@validator.mark_as_new_variant(entry, product_id)
|
||||
return
|
||||
@@ -207,7 +212,10 @@ module ProductImport
|
||||
when 'overwrite_all'
|
||||
object.assign_attributes(attribute => setting['value'])
|
||||
when 'overwrite_empty'
|
||||
if object.public_send(attribute).blank? || ((attribute == 'on_hand' || attribute == 'count_on_hand') && entry.on_hand_nil)
|
||||
if object.public_send(attribute).blank? ||
|
||||
((attribute == 'on_hand' || attribute == 'count_on_hand') &&
|
||||
entry.on_hand_nil)
|
||||
|
||||
object.assign_attributes(attribute => setting['value'])
|
||||
end
|
||||
end
|
||||
@@ -216,7 +224,10 @@ module ProductImport
|
||||
|
||||
def display_in_inventory(variant_override, is_new = false)
|
||||
unless is_new
|
||||
existing_item = InventoryItem.where(variant_id: variant_override.variant_id, enterprise_id: variant_override.hub_id).first
|
||||
existing_item = InventoryItem.where(
|
||||
variant_id: variant_override.variant_id,
|
||||
enterprise_id: variant_override.hub_id
|
||||
).first
|
||||
|
||||
if existing_item
|
||||
existing_item.assign_attributes(visible: true)
|
||||
@@ -225,7 +236,11 @@ module ProductImport
|
||||
end
|
||||
end
|
||||
|
||||
InventoryItem.new(variant_id: variant_override.variant_id, enterprise_id: variant_override.hub_id, visible: true).save
|
||||
InventoryItem.new(
|
||||
variant_id: variant_override.variant_id,
|
||||
enterprise_id: variant_override.hub_id,
|
||||
visible: true
|
||||
).save
|
||||
end
|
||||
|
||||
def ensure_variant_updated(product, entry)
|
||||
|
||||
@@ -74,32 +74,50 @@ module ProductImport
|
||||
return if permissions_error entry
|
||||
return if primary_producer_error entry
|
||||
|
||||
entry.enterprise_id = @spreadsheet_data.enterprises_index[entry.enterprise][:id]
|
||||
entry.public_send "#{enterprise_field}_id=", @spreadsheet_data.enterprises_index[entry.enterprise][:id]
|
||||
entry.enterprise_id =
|
||||
@spreadsheet_data.enterprises_index[entry.enterprise][:id]
|
||||
|
||||
entry.public_send(
|
||||
"#{enterprise_field}_id=",
|
||||
@spreadsheet_data.enterprises_index[entry.enterprise][:id]
|
||||
)
|
||||
end
|
||||
|
||||
def name_presence_error(entry)
|
||||
return if entry.enterprise.present?
|
||||
mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_required))
|
||||
mark_as_invalid(entry,
|
||||
attribute: enterprise_field,
|
||||
error: I18n.t(:error_required))
|
||||
true
|
||||
end
|
||||
|
||||
def enterprise_not_found_error(entry)
|
||||
return if @spreadsheet_data.enterprises_index[entry.enterprise][:id]
|
||||
mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_found_in_database, name: entry.enterprise))
|
||||
mark_as_invalid(entry,
|
||||
attribute: enterprise_field,
|
||||
error: I18n.t(:error_not_found_in_database,
|
||||
name: entry.enterprise))
|
||||
true
|
||||
end
|
||||
|
||||
def permissions_error(entry)
|
||||
return if permission_by_name?(entry.enterprise)
|
||||
mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_no_permission_for_enterprise, name: entry.enterprise))
|
||||
mark_as_invalid(entry,
|
||||
attribute: enterprise_field,
|
||||
error: I18n.t(:error_no_permission_for_enterprise,
|
||||
name: entry.enterprise))
|
||||
true
|
||||
end
|
||||
|
||||
def primary_producer_error(entry)
|
||||
return if import_into_inventory?
|
||||
return if @spreadsheet_data.enterprises_index[entry.enterprise][:is_primary_producer]
|
||||
mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_primary_producer, name: entry.enterprise))
|
||||
return if @spreadsheet_data.
|
||||
enterprises_index[entry.enterprise][:is_primary_producer]
|
||||
|
||||
mark_as_invalid(entry,
|
||||
attribute: enterprise_field,
|
||||
error: I18n.t(:error_not_primary_producer,
|
||||
name: entry.enterprise))
|
||||
true
|
||||
end
|
||||
|
||||
@@ -136,7 +154,11 @@ module ProductImport
|
||||
return
|
||||
end
|
||||
|
||||
unless inventory_permission?(entry.enterprise_id, @spreadsheet_data.producers_index[producer_name])
|
||||
unless inventory_permission?(
|
||||
entry.enterprise_id,
|
||||
@spreadsheet_data.producers_index[producer_name]
|
||||
)
|
||||
|
||||
mark_as_invalid(entry, attribute: "producer", error: "\"#{producer_name}\": #{I18n.t('admin.product_import.model.inventory_no_permission')}")
|
||||
return
|
||||
end
|
||||
@@ -196,7 +218,9 @@ module ProductImport
|
||||
end
|
||||
|
||||
def product_validation(entry)
|
||||
products = Spree::Product.where(supplier_id: entry.enterprise_id, name: entry.name, deleted_at: nil)
|
||||
products = Spree::Product.where(supplier_id: entry.enterprise_id,
|
||||
name: entry.name,
|
||||
deleted_at: nil)
|
||||
|
||||
if products.empty?
|
||||
mark_as_new_product(entry)
|
||||
@@ -263,7 +287,9 @@ module ProductImport
|
||||
end
|
||||
|
||||
def inventory_permission?(enterprise_id, producer_id)
|
||||
@current_user.admin? || ( @inventory_permissions[enterprise_id] && @inventory_permissions[enterprise_id].include?(producer_id) )
|
||||
@current_user.admin? ||
|
||||
( @inventory_permissions[enterprise_id] &&
|
||||
@inventory_permissions[enterprise_id].include?(producer_id) )
|
||||
end
|
||||
|
||||
def mark_as_invalid(entry, options = {})
|
||||
@@ -272,7 +298,7 @@ module ProductImport
|
||||
end
|
||||
|
||||
def import_into_inventory?
|
||||
@import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories'
|
||||
@import_settings[:settings].andand['import_into'] == 'inventories'
|
||||
end
|
||||
|
||||
def validate_inventory_item(entry, variant_override)
|
||||
@@ -284,9 +310,16 @@ module ProductImport
|
||||
end
|
||||
|
||||
def create_inventory_item(entry, existing_variant)
|
||||
existing_variant_override = VariantOverride.where(variant_id: existing_variant.id, hub_id: entry.enterprise_id).first
|
||||
existing_variant_override = VariantOverride.where(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
).first
|
||||
|
||||
variant_override = existing_variant_override || VariantOverride.new(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
)
|
||||
|
||||
variant_override = existing_variant_override || VariantOverride.new(variant_id: existing_variant.id, hub_id: entry.enterprise_id)
|
||||
variant_override.assign_attributes(count_on_hand: entry.on_hand, import_date: @import_time)
|
||||
check_on_hand_nil(entry, variant_override)
|
||||
variant_override.assign_attributes(entry.attributes.slice('price', 'on_demand'))
|
||||
@@ -306,7 +339,9 @@ module ProductImport
|
||||
end
|
||||
|
||||
def updates_count_per_enterprise(enterprise_id)
|
||||
if @reset_counts[enterprise_id] && @reset_counts[enterprise_id][:updates_count]
|
||||
if @reset_counts[enterprise_id] &&
|
||||
@reset_counts[enterprise_id][:updates_count]
|
||||
|
||||
@reset_counts[enterprise_id][:updates_count] += 1
|
||||
else
|
||||
@reset_counts[enterprise_id] = { updates_count: 1 }
|
||||
|
||||
@@ -65,12 +65,14 @@ module ProductImport
|
||||
end
|
||||
|
||||
def reset_counts
|
||||
# Return indexed data about existing product count, reset count, and updates count per enterprise
|
||||
# Return indexed data about existing product count, reset count, and
|
||||
# updates count per enterprise
|
||||
@reset_counts.each do |enterprise_id, values|
|
||||
values[:updates_count] = 0 if values[:updates_count].blank?
|
||||
|
||||
if values[:updates_count] && values[:existing_products]
|
||||
@reset_counts[enterprise_id][:reset_count] = values[:existing_products] - values[:updates_count]
|
||||
@reset_counts[enterprise_id][:reset_count] =
|
||||
values[:existing_products] - values[:updates_count]
|
||||
end
|
||||
end
|
||||
@reset_counts
|
||||
|
||||
@@ -35,16 +35,21 @@ module ProductImport
|
||||
private
|
||||
|
||||
def import_into_inventory?
|
||||
@import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories'
|
||||
@import_settings[:settings].andand['import_into'] == 'inventories'
|
||||
end
|
||||
|
||||
def create_enterprises_index
|
||||
@enterprises_index = {}
|
||||
@entries.each do |entry|
|
||||
enterprise_name = import_into_inventory? ? entry.distributor : entry.producer
|
||||
|
||||
next if @enterprises_index.key? enterprise_name
|
||||
enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, name, is_primary_producer')
|
||||
@enterprises_index[enterprise_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) }
|
||||
|
||||
enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, is_primary_producer')
|
||||
|
||||
@enterprises_index[enterprise_name] =
|
||||
{ id: enterprise.try(:id),
|
||||
is_primary_producer: enterprise.try(:is_primary_producer) }
|
||||
end
|
||||
@enterprises_index
|
||||
end
|
||||
|
||||
@@ -8,13 +8,17 @@ module ProductImport
|
||||
include ActiveModel::Conversion
|
||||
include ActiveModel::Validations
|
||||
|
||||
attr_accessor :line_number, :valid, :validates_as, :product_object, :product_validations, :on_hand_nil,
|
||||
:has_overrides, :units, :unscaled_units, :unit_type, :tax_category, :shipping_category
|
||||
attr_accessor :line_number, :valid, :validates_as, :product_object,
|
||||
:product_validations, :on_hand_nil, :has_overrides, :units,
|
||||
:unscaled_units, :unit_type, :tax_category, :shipping_category
|
||||
|
||||
attr_accessor :id, :product_id, :producer, :producer_id, :distributor, :distributor_id, :name, :display_name, :sku,
|
||||
:unit_value, :unit_description, :variant_unit, :variant_unit_scale, :variant_unit_name,
|
||||
:display_as, :category, :primary_taxon_id, :price, :on_hand, :count_on_hand, :on_demand,
|
||||
:tax_category_id, :shipping_category_id, :description, :import_date, :enterprise, :enterprise_id
|
||||
attr_accessor :id, :product_id, :producer, :producer_id, :distributor,
|
||||
:distributor_id, :name, :display_name, :sku, :unit_value,
|
||||
:unit_description, :variant_unit, :variant_unit_scale,
|
||||
:variant_unit_name, :display_as, :category, :primary_taxon_id,
|
||||
:price, :on_hand, :count_on_hand, :on_demand,
|
||||
:tax_category_id, :shipping_category_id, :description,
|
||||
:import_date, :enterprise, :enterprise_id
|
||||
|
||||
def initialize(attrs)
|
||||
@validates_as = ''
|
||||
@@ -77,11 +81,16 @@ module ProductImport
|
||||
end
|
||||
|
||||
def non_display_attributes
|
||||
['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise', 'enterprise_id', 'producer_id', 'distributor_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value']
|
||||
['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise',
|
||||
'enterprise_id', 'producer_id', 'distributor_id', 'primary_taxon',
|
||||
'primary_taxon_id', 'category_id', 'shipping_category_id',
|
||||
'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value']
|
||||
end
|
||||
|
||||
def non_product_attributes
|
||||
['line_number', 'valid', 'errors', 'product_object', 'product_validations', 'inventory_validations', 'validates_as', 'save_type', 'on_hand_nil', 'has_overrides']
|
||||
['line_number', 'valid', 'errors', 'product_object',
|
||||
'product_validations', 'inventory_validations', 'validates_as',
|
||||
'save_type', 'on_hand_nil', 'has_overrides']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user