From a6a616cf6d9d9118b3ba9cdc8c9dc160cd8488b9 Mon Sep 17 00:00:00 2001 From: Ilya Konanykhin Date: Mon, 20 Mar 2017 22:38:14 +0600 Subject: [PATCH] BikeCsvImporter: add status logs --- app/models/bike_csv_importer.rb | 24 ++++++++++------ app/models/bike_csv_importer/cache.rb | 5 ++++ app/models/bike_csv_importer/logs.rb | 41 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 app/models/bike_csv_importer/logs.rb diff --git a/app/models/bike_csv_importer.rb b/app/models/bike_csv_importer.rb index ed04149..07fce50 100644 --- a/app/models/bike_csv_importer.rb +++ b/app/models/bike_csv_importer.rb @@ -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 diff --git a/app/models/bike_csv_importer/cache.rb b/app/models/bike_csv_importer/cache.rb index 960f37a..a90c947 100644 --- a/app/models/bike_csv_importer/cache.rb +++ b/app/models/bike_csv_importer/cache.rb @@ -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 diff --git a/app/models/bike_csv_importer/logs.rb b/app/models/bike_csv_importer/logs.rb new file mode 100644 index 0000000..a965c61 --- /dev/null +++ b/app/models/bike_csv_importer/logs.rb @@ -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