mirror of
https://github.com/fspc/BikeShed-1.git
synced 2025-02-28 08:43:23 -05:00
BikeCsvImporter: add status logs
This commit is contained in:
parent
ae79a34652
commit
a6a616cf6d
@ -1,19 +1,11 @@
|
||||
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
|
||||
|
||||
include BikeCsvImporter::Cache
|
||||
include BikeCsvImporter::Cleaner
|
||||
include BikeCsvImporter::BikeAttrs
|
||||
include BikeCsvImporter::Logs
|
||||
|
||||
attr_reader :file
|
||||
|
||||
@ -31,6 +23,16 @@ class BikeCsvImporter
|
||||
check_method = dry_run ? :valid? : :save
|
||||
if bike.try check_method
|
||||
puts "Imported #{bike.shop_id}: #{bike}".green
|
||||
|
||||
logs = new_logs_entries bike, bike_hash
|
||||
logs.each do |log|
|
||||
if log.send check_method
|
||||
puts "\tLog entry created: #{log.inspect}".green
|
||||
else
|
||||
puts "\tLog entry creation failed: #{log.errors.full_messages.join '; '}".red
|
||||
end
|
||||
end
|
||||
|
||||
imported_count += 1
|
||||
else
|
||||
puts "Skipped #{bike.try(:shop_id) || bike_hash.values.first}: #{bike.try(:errors).try(:full_messages).try :join, '; '}".red
|
||||
@ -89,4 +91,8 @@ class BikeCsvImporter
|
||||
def new_bike(bike_hash)
|
||||
Bike.new bike_attrs(bike_hash)
|
||||
end
|
||||
|
||||
def new_logs_entries(bike, bike_hash)
|
||||
%i{ acquired comment gone }.map { |x| send :"log_entry_#{x}", bike, bike_hash }.compact
|
||||
end
|
||||
end
|
||||
|
@ -22,5 +22,10 @@ class BikeCsvImporter
|
||||
@bike_model_cache[model] = BikeModel.where('lower(model) = ?', model.downcase).first
|
||||
end
|
||||
end
|
||||
|
||||
def cached_log_bike_action(action)
|
||||
@log_bike_action_id_cache ||= {}
|
||||
@log_bike_action_id_cache[action] ||= ActsAsLoggable::BikeAction.find_by_action(action)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
41
app/models/bike_csv_importer/logs.rb
Normal file
41
app/models/bike_csv_importer/logs.rb
Normal file
@ -0,0 +1,41 @@
|
||||
class BikeCsvImporter
|
||||
module Logs
|
||||
def log_entry_gone(bike, bike_hash)
|
||||
if clean_value(bike_hash['gone']).to_s =~ /y/i
|
||||
log_entry bike, log_entry_date(clean_value(bike_hash['date out'])), 'COMPLETED', 'Gone'
|
||||
end
|
||||
end
|
||||
|
||||
def log_entry_acquired(bike, bike_hash)
|
||||
if clean_value(bike_hash['date in'])
|
||||
log_entry bike, log_entry_date(clean_value(bike_hash['date in'])), 'ACQUIRED'
|
||||
end
|
||||
end
|
||||
|
||||
def log_entry_comment(bike, bike_hash)
|
||||
if clean_value(bike_hash['comment']).present?
|
||||
log_entry bike, nil, 'NOTE', clean_value(bike_hash['comment'])
|
||||
end
|
||||
end
|
||||
|
||||
def log_entry_date(value)
|
||||
return unless value
|
||||
Date.strptime value, '%m/%d/%y' rescue nil
|
||||
end
|
||||
|
||||
def log_entry(bike, date, type, description = nil)
|
||||
date ||= DateTime.now
|
||||
bike_action = cached_log_bike_action(type)
|
||||
|
||||
ActsAsLoggable::Log.new(
|
||||
loggable_type: bike.class.to_s,
|
||||
loggable_id: bike.id || bike.shop_id.to_i, # for dry run
|
||||
log_action_type: bike_action.class.to_s,
|
||||
log_action_id: bike_action.id,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
description: description,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user