Sha256: 3970e1447f842eee4301cb55883e1c0f4076f1e5416ed517ae0ce475457ef163

Contents?: true

Size: 831 Bytes

Versions: 2

Compression:

Stored size: 831 Bytes

Contents

# frozen_string_literal: true

require_relative "iprog_export_model_to_xlsx/version"
require "axlsx"

module IprogExportModelToXlsx
  class Error < StandardError; end

  def export(file_path, options = {})
    exclude_columns = options[:exclude_columns] || []
    limit           = options[:limit]
    attributes      = attribute_names - exclude_columns
    package         = Axlsx::Package.new

    package.workbook.add_worksheet(name: name) do |sheet|
      # Headers
      sheet.add_row attributes

      # Data
      scope = all
      scope = scope.limit(limit) if limit
      scope.each do |record|
        sheet.add_row record.attributes.slice(*attributes).values
      end
    end

    package.serialize(file_path)
  rescue StandardError => e
    rails Iprog::Arde::Error, "Failed to export to XLSX: #{e.message}"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iprog_export_model_to_xlsx-0.1.1 lib/iprog_export_model_to_xlsx.rb
iprog_export_model_to_xlsx-0.1.0 lib/iprog_export_model_to_xlsx.rb