Last active
August 29, 2015 14:16
-
-
Save duderman/555af968cc42bb18a0c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def import_retail_chain_products(path) | |
fail unless File.file? path | |
imported, failed = [], [] | |
retail_chain_id = 'ed45a234-1d88-481b-8bbd-1ff1ab8ff7a8' | |
mappings = { | |
code: { index: 0, type: :string }, | |
full_name: { index: 1, type: :string }, | |
short_name: { index: 2, type: :string }, | |
ru_international_name: { index: 6, type: :string }, | |
retail_chain_registry_concern_id: { index: 4, type: :registry, model: RetailChainRegistry::Concern }, | |
retail_chain_registry_manufacturer_id: { index: 5, type: :registry, model: RetailChainRegistry::Manufacturer }, | |
retail_chain_registry_trade_mark_id: { index: 7, type: :registry, model: RetailChainRegistry::TradeMark }, | |
retail_chain_registry_country_id: { index: 8, type: :registry, model: RetailChainRegistry::Country }, | |
retail_chain_registry_brand_id: { index: 9, type: :registry, model: RetailChainRegistry::Brand }, | |
is_pku: { index: 10, type: :boolean }, | |
is_contains_codeine: { index: 11, type: :boolean }, | |
is_drug: { index: 12, type: :boolean }, | |
is_potent: { index: 13, type: :boolean }, | |
is_vital: { index: 14, type: :boolean }, | |
vat: { index: 15, type: :integer } | |
} | |
registries = {}.tap do |registries| | |
mappings.select { |m| mappings[m][:type] == :registry }.each_pair do |name, props| | |
registries[props[:model].to_s] = props[:model].where(retail_chain_id: retail_chain_id).index_by(&:name) | |
end | |
end | |
CSV.foreach(path) do |row| | |
RetailChainProduct.transaction do | |
rcp = RetailChainProduct.new | |
rcp.properties = {} | |
rcp.name = row[mappings[:full_name][:index]].to_s | |
rcp.retail_chain_id = retail_chain_id | |
rcp.product_type_id = ProductType.find_or_create_by(name: row[3]).id unless row[3].blank? | |
mappings.each_pair do |name, props| | |
value = row[props[:index]].to_s | |
next if value.blank? | |
rcp.properties[name] = case props[:type] | |
when :boolean | |
case value | |
when '1' | |
true | |
when '0' | |
false | |
end | |
when :registry | |
if registries[props[:model].to_s][value].nil? | |
registries[props[:model].to_s][value] = props[:model].create(name: value, retail_chain_id: retail_chain_id) | |
end | |
registries[props[:model].to_s][value].id | |
when :integer | |
value.to_i | |
else | |
value | |
end | |
end | |
if rcp.save | |
imported << rcp | |
puts "Imported #{imported.count}" | |
else | |
failed << row | |
puts "Failed #{imported.count}" | |
end | |
end | |
end | |
CSV.open('/home/hq/failed_imports.csv', 'wb') do |csv| | |
failed.each { |f| csv << f } | |
end unless failed.empty? | |
[imported.count, failed.count] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment