Browse Source

BikeCsvImporter: add dry run

master
Ilya Konanykhin 8 years ago
parent
commit
0083dd9bca
  1. 16
      app/models/bike_csv_importer.rb
  2. 8
      lib/tasks/import.rake

16
app/models/bike_csv_importer.rb

@ -1,14 +1,15 @@
require 'csv'
class BikeCsvImporter
attr_reader :file
attr_reader :file, :dry_run
def initialize(file)
def initialize(file, dry_run)
@file = file
@dry_run = !!dry_run
end
def run
result = {imported_count: 0, skipped_shop_ids: []}
result = {imported_count: 0, skipped_errors: {}}
@bike_purpose_cache = {}
@bike_brand_cache = {}
@ -16,10 +17,11 @@ class BikeCsvImporter
fetch do |bike_hash|
bike = import_bike bike_hash
if bike.try :persisted?
check_method = dry_run ? :valid? : :persisted?
if bike.try check_method
result[:imported_count] += 1
else
result[:skipped_shop_ids].push bike.try(:shop_id) || bike_hash.values.first
result[:skipped_errors][bike.try(:shop_id) || bike_hash.values.first] = bike.try(:errors).try(:messages)
end
end
@ -90,7 +92,9 @@ class BikeCsvImporter
# I realize the log entry stuff is likely complicated and time consuming. At a minimum, the most important columns to import are the following: Velocipede Number, Program, Gone, Make, Model. To avoid creating the log entry for "Gone", we would instead just set 'gone' to true.
def import_bike(bike_hash)
bike = Bike.new bike_attrs(bike_hash)
#bike.save
#bike.save unless dry_run
raise 'TODO save' unless dry_run
bike
end
def bike_attrs(bike_hash)

8
lib/tasks/import.rake

@ -1,11 +1,11 @@
namespace :import do
namespace :bikes do
# Imports bikes info from CSV file
task :csv, [:file] => :environment do |t, args|
file = args[:file]
next puts "Usage: rake #{t.name}[$csv_file_path]" unless file
task :csv, [:file, :dry_run] => :environment do |t, args|
file, dry_run = args.values_at :file, :dry_run
next puts "Usage: rake #{t.name}[$csv_file_path[,$dry_run=dry]]" unless file
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
pp BikeCsvImporter.new(file).run
pp BikeCsvImporter.new(file, dry_run == 'dry').run
end
# Analyze a single field from CSV file

Loading…
Cancel
Save