Sha256: 743b266bebda166d7c5795410cf1b3e742d5d460227726b67ffa9cb3e408bd92

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "active_record"
require_relative "action_csv/version"

module ActionCsv
  class Error < StandardError; end
  COLUMNS = YAML.safe_load_file "./config/column_names.yml"
  def self.export_csv model_name
    singularized_name = model_name.singularize.underscore
    file_dir = "./db/csv"
    FileUtiles.mkdir_p file_dir unless Dir.exist? file_dir
    file_path = ENV["RACK_ENV"] == "production" ? "#{file_dir}#{model_name.pluralize.underscore}_production.csv" : "#{file_dir}#{model_name.pluralize.underscore}.csv"
    CSV.open(file_path, "w") do |csv|
      if COLUMNS[singularized_name].present?
        return "Columns number doesn't match! Please check ./config/column_names.yml !" unless COLUMNS[singularized_name].size == Object.const_get(singularized_name.camelize).column_names.size
        csv << class_name.column_names.map { |c| COLUMNS[singularized_name][c] }
      else
        csv << class_name.column_names
      end
      class_name.all.reverse.each do |item|
        csv << item.attributes.values
      end
    end
    puts "Export Success!:#{file_path}"
    true
  rescue StandardError => error
    error.backtrace
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
action_csv-0.1.1 lib/action_csv.rb