diff options
| -rw-r--r-- | app/models/simple_exporter.rb | 13 | ||||
| -rw-r--r-- | app/models/simple_interface.rb | 6 | ||||
| -rw-r--r-- | app/models/simple_json_exporter.rb | 14 |
3 files changed, 14 insertions, 19 deletions
diff --git a/app/models/simple_exporter.rb b/app/models/simple_exporter.rb index 402d450f0..93a773430 100644 --- a/app/models/simple_exporter.rb +++ b/app/models/simple_exporter.rb @@ -16,13 +16,8 @@ class SimpleExporter < SimpleInterface @statuses = "" - if ENV["NO_TRANSACTION"] - process_collection - else - ActiveRecord::Base.transaction do - process_collection - end - end + process_collection + self.status ||= :success rescue SimpleInterface::FailedOperation self.status = :failed @@ -138,6 +133,10 @@ class SimpleExporter < SimpleInterface flatten_hash(v).map do |h_k, h_v| h["#{k}.#{h_k}".to_sym] = h_v end + elsif v.is_a? ActiveRecord::Base + flatten_hash(v.attributes).map do |h_k, h_v| + h["#{k}.#{h_k}".to_sym] = h_v + end else h[k] = v end diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb index e533dc744..5997a6dd3 100644 --- a/app/models/simple_interface.rb +++ b/app/models/simple_interface.rb @@ -81,11 +81,11 @@ class SimpleInterface < ActiveRecord::Base else @messages << [time, msg] end - print_state + print_state true end def output_filepath - File.join @output_dir, "#{self.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" + @output_filepath ||= File.join @output_dir, "#{self.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" end def write_output_to_csv @@ -191,7 +191,7 @@ class SimpleInterface < ActiveRecord::Base msg += colorize "=== MESSAGES (#{@messages.count}) ===\n", :green msg += "[...]\n" if @messages.count > lines_count msg += @messages.last(lines_count).map do |m| - "[#{"%.5f" % m[0]}]\t" + m[1].truncate(@status_width) + "[#{"%.5f" % m[0]}]\t" + m[1].truncate(@status_width - 10) end.join("\n") msg += "\n"*[lines_count-@messages.count, 0].max end diff --git a/app/models/simple_json_exporter.rb b/app/models/simple_json_exporter.rb index 6402efea4..024d75c97 100644 --- a/app/models/simple_json_exporter.rb +++ b/app/models/simple_json_exporter.rb @@ -21,13 +21,7 @@ class SimpleJsonExporter < SimpleExporter @statuses = "" - if ENV["NO_TRANSACTION"] - process_collection - else - ActiveRecord::Base.transaction do - process_collection - end - end + process_collection self.status ||= :success rescue SimpleInterface::FailedOperation self.status = :failed @@ -56,13 +50,15 @@ class SimpleJsonExporter < SimpleExporter log "Export will be written in #{filepath}" if collection.is_a?(ActiveRecord::Relation) && collection.model.column_names.include?("id") + log "Using paginated collection", color: :green ids = collection.pluck :id ids.in_groups_of(configuration.batch_size).each do |batch_ids| - collection.where(id: batch_ids).each do |item| + collection.where(id: batch_ids.compact).each do |item| handle_item item end end else + log "Using non-paginated collection", color: :orange collection.each{|item| handle_item item } end print_state true @@ -98,7 +94,7 @@ class SimpleJsonExporter < SimpleExporter map_item_to_rows(item).each_with_index do |item, i| @number_of_lines = number_of_lines + i serialized_item = {} - @current_row = item.attributes + @current_row = item.attributes.symbolize_keys @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present? @new_status = nil |
