mirror of
https://github.com/fspc/BikeShed-1.git
synced 2025-02-28 08:43:23 -05:00
BikeCsvImporter: fix incorrect method signatures
This commit is contained in:
parent
0a338a2485
commit
c1ca501204
@ -1,24 +1,32 @@
|
|||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
|
# Gone -> If "Yes", set 'gone' to true, then create a Log entry like the following:
|
||||||
|
# id | loggable_id | loggable_type | logger_id | logger_type | context | start_date | end_date | description | log_action_id | log_action_type | created_at | updated_at
|
||||||
|
# 18 | 1 | Bike | 4 | User | | 2017-02-03 23:27:00 | 2017-02-03 23:27:00 | Gone | 5 | ActsAsLoggable::BikeAction | 2017-02-03 23:27:36.8387 | 2017-02-03 23:27:36.8387
|
||||||
|
# See https://github.com/spacemunkay/BikeShed/blob/master/app/components/bike_logs.rb#L12-L18 for example. Use user_id 1 for current_user_id (1 should be the admin ID I think). Use "Date Out" column for start_date & end_date. Set action_id to "COMPLETED".
|
||||||
|
#
|
||||||
|
# Date In -> Create a bike log entry with start_date & end_date with same value as "Date In". Set action_id to "AQUIRED"
|
||||||
|
# Date Out -> Should be the start_date & end_date value for "Gone" column mentioned above.
|
||||||
|
# Comment -> Create a bike log entry with action_id "NOTE". The log 'description' should be the value of 'Comment'.
|
||||||
|
|
||||||
class BikeCsvImporter
|
class BikeCsvImporter
|
||||||
|
|
||||||
include BikeCsvImporter::Cache
|
include BikeCsvImporter::Cache
|
||||||
include BikeCsvImporter::Cleaner
|
include BikeCsvImporter::Cleaner
|
||||||
include BikeCsvImporter::BikeAttrs
|
include BikeCsvImporter::BikeAttrs
|
||||||
|
|
||||||
attr_reader :file, :dry_run
|
attr_reader :file
|
||||||
|
|
||||||
def initialize(file, dry_run)
|
def initialize(file)
|
||||||
@file = file
|
@file = file
|
||||||
@dry_run = !!dry_run
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run(dry_run)
|
||||||
result = {imported: {}, skipped: {}}
|
result = {imported: {}, skipped: {}}
|
||||||
|
|
||||||
fetch do |bike_hash|
|
fetch do |bike_hash|
|
||||||
bike = import_bike bike_hash
|
bike = new_bike bike_hash
|
||||||
check_method = dry_run ? :valid? : :persisted?
|
check_method = dry_run ? :valid? : :save
|
||||||
if bike.try check_method
|
if bike.try check_method
|
||||||
result[:imported][bike.shop_id] = bike.inspect
|
result[:imported][bike.shop_id] = bike.inspect
|
||||||
else
|
else
|
||||||
@ -65,18 +73,7 @@ class BikeCsvImporter
|
|||||||
@header.zip(row).to_h
|
@header.zip(row).to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
# Gone -> If "Yes", set 'gone' to true, then create a Log entry like the following:
|
def new_bike(bike_hash)
|
||||||
# id | loggable_id | loggable_type | logger_id | logger_type | context | start_date | end_date | description | log_action_id | log_action_type | created_at | updated_at
|
Bike.new bike_attrs(bike_hash)
|
||||||
# 18 | 1 | Bike | 4 | User | | 2017-02-03 23:27:00 | 2017-02-03 23:27:00 | Gone | 5 | ActsAsLoggable::BikeAction | 2017-02-03 23:27:36.8387 | 2017-02-03 23:27:36.8387
|
|
||||||
# See https://github.com/spacemunkay/BikeShed/blob/master/app/components/bike_logs.rb#L12-L18 for example. Use user_id 1 for current_user_id (1 should be the admin ID I think). Use "Date Out" column for start_date & end_date. Set action_id to "COMPLETED".
|
|
||||||
#
|
|
||||||
# Date In -> Create a bike log entry with start_date & end_date with same value as "Date In". Set action_id to "AQUIRED"
|
|
||||||
# Date Out -> Should be the start_date & end_date value for "Gone" column mentioned above.
|
|
||||||
# Comment -> Create a bike log entry with action_id "NOTE". The log 'description' should be the value of 'Comment'.
|
|
||||||
def import_bike(bike_hash)
|
|
||||||
bike = Bike.new bike_attrs(bike_hash)
|
|
||||||
#bike.save unless dry_run
|
|
||||||
raise 'TODO save' unless dry_run
|
|
||||||
bike
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ namespace :import do
|
|||||||
file, dry_run = args.values_at :file, :dry_run
|
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 "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
|
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
||||||
pp BikeCsvImporter.new(file, dry_run == 'dry').run
|
BikeCsvImporter.new(file).run dry_run == 'dry'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Analyze a single field from CSV file
|
# Analyze a single field from CSV file
|
||||||
@ -13,7 +13,7 @@ namespace :import do
|
|||||||
file, field = args.values_at :file, :field
|
file, field = args.values_at :file, :field
|
||||||
next puts "Usage: rake #{t.name}[$csv_file_path[,\"$field_name\"]]" unless file
|
next puts "Usage: rake #{t.name}[$csv_file_path[,\"$field_name\"]]" unless file
|
||||||
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
||||||
pp BikeCsvImporter.new(file).analyze field ? [field] : []
|
BikeCsvImporter.new(file).analyze field ? [field] : []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user