fix bug: look up parent crop by name in csv import

This commit is contained in:
Brenda Wallace
2017-02-19 20:47:56 +13:00
committed by Shiny
parent 2f9961651f
commit abbd2cf0a4
3 changed files with 60 additions and 61 deletions

View File

@@ -1,70 +1,69 @@
class CsvImporter
class Crops
# used by db/seeds.rb and rake growstuff:import_crops
# CSV fields:
# - name (required)
# - en_wikipedia_url (required)
# - parent (name, optional)
# - scientific name (optional, can be picked up from parent if it has one)
def import(row)
name, en_wikipedia_url, parent_name, scientific_names, alternate_names = row
# used by db/seeds.rb and rake growstuff:import_crops
# CSV fields:
# - name (required)
# - en_wikipedia_url (required)
# - parent (name, optional)
# - scientific name (optional, can be picked up from parent if it has one)
def import_crop(row)
name, en_wikipedia_url, parent_name, scientific_names, alternate_names = row
@crop = Crop.find_or_create_by(name: name)
@crop.update_attributes(
en_wikipedia_url: en_wikipedia_url,
creator_id: cropbot.id
)
if parent_name
parent = Crop.find_by(name: parent)
if parent
@crop.update_attributes(parent_id: parent.id)
else
@crop.logger.warn("Warning: parent crop #{parent_name} not found")
end
end
add_scientific_names(scientific_names)
add_alternate_names(alternate_names)
end
def add_scientific_names(scientific_names)
names_to_add = []
if !scientific_names.blank? # i.e. we actually passed something in, which isn't a given
names_to_add = scientific_names.split(/,\s*/)
elsif @crop.parent && !@crop.parent.scientific_names.empty? # pick up from parent
names_to_add = @crop.parent.scientific_names.map(&:name)
@crop = Crop.find_or_create_by(name: name)
@crop.update_attributes(
en_wikipedia_url: en_wikipedia_url,
creator_id: cropbot.id
)
if parent_name
parent = Crop.find_by(name: parent_name)
if parent
@crop.update_attributes(parent_id: parent.id)
else
@crop.logger.warn("Warning: no scientific name (not even on parent crop) for #{self}")
end
return if names_to_add.empty?
names_to_add.each do |name|
if ScientificName.find_by(name: name)
# raise "ScientificName #{name} already exists on another crop."
else
ScientificName.create! name: name, crop: @crop, creator: cropbot
end
@crop.logger.warn("Warning: parent crop #{parent_name} not found")
end
end
def add_alternate_names(alternate_names)
# i.e. we actually passed something in, which isn't a given
return if alternate_names.blank?
alternate_names.split(/,\s*/).each do |name|
if AlternateName.find_by(name: name)
# raise "Name already exists: #{name}"
else
AlternateName.create! name: name, crop: @crop, creator: cropbot
end
end
add_scientific_names(scientific_names)
add_alternate_names(alternate_names)
@crop
end
private
def add_scientific_names(scientific_names)
names_to_add = []
if !scientific_names.blank? # i.e. we actually passed something in, which isn't a given
names_to_add = scientific_names.split(/,\s*/)
elsif @crop.parent && !@crop.parent.scientific_names.empty? # pick up from parent
names_to_add = @crop.parent.scientific_names.map(&:name)
else
@crop.logger.warn("Warning: no scientific name (not even on parent crop) for #{self}")
end
def cropbot
@cropbot = Member.find_by!(login_name: 'cropbot') unless @cropbot
@cropbot
rescue
raise "cropbot account not found: run rake db:seed"
return if names_to_add.empty?
names_to_add.each do |name|
sciname = ScientificName.find_by(name: name)
sciname = ScientificName.create! name: name, crop: @crop, creator: cropbot unless sciname
@crop.scientific_names << sciname
end
end
def add_alternate_names(alternate_names)
# i.e. we actually passed something in, which isn't a given
return if alternate_names.blank?
alternate_names.split(/,\s*/).each do |name|
if AlternateName.find_by(name: name)
# raise "Name already exists: #{name}"
else
AlternateName.create! name: name, crop: @crop, creator: cropbot
end
end
end
def cropbot
@cropbot = Member.find_by!(login_name: 'cropbot') unless @cropbot
@cropbot
rescue
raise "cropbot account not found: run rake db:seed"
end
end

View File

@@ -31,7 +31,7 @@ def load_crops
Dir.glob("#{source_path}/crops*.csv").each do |crop_file|
puts "Loading crops from #{crop_file}..."
CSV.foreach(crop_file) do |row|
CsvImporter::Crops.new.import(row)
CsvImporter.new.import_crop(row)
end
end
puts "Finished loading crops"

View File

@@ -33,7 +33,7 @@ namespace :growstuff do
puts "Loading crops from #{@file}..."
CSV.foreach(@file) do |row|
CsvImporter::Crops.new.import(row)
CsvImporter.new.import_crop(row)
end
Rails.cache.delete('full_crop_hierarchy')
puts "Finished loading crops"