module ActionCsv

Constants

COLUMNS
VERSION

Public Class Methods

export_csv(model_name) click to toggle source
# File lib/action_csv.rb, line 10
def self.export_csv model_name
  singularized_name = model_name.singularize.underscore
  file_dir = "./db/csv/"
  FileUtils.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
get_tables() click to toggle source
# File lib/action_csv.rb, line 32
def self.get_tables
  path = "./db/schema.rb"
  tables = []
  File.open(path, "r") do |f|
    f.each_line.with_index do |line, i|
      tables << line.split("\"")[1] if line.include?("create_table")
    end
  end
  puts tables
  tables
end