Sha256: 8eccfcda9250e969fed99a0b95eb34318f20fe6db9e7632ae5d70246e2a596a3

Contents?: true

Size: 1.57 KB

Versions: 16

Compression:

Stored size: 1.57 KB

Contents

require 'csv'

module AbAdmin
  module Utils
    class CsvDocument
      include AbAdmin::Utils::EvalHelpers

      def initialize(source, options = {})
        @source = source
        @options = options
        @klass = @options.delete(:klass) || extract_klass
      end

      def default_columns
        @default_columns ||= @klass.column_names
      end

      def column_data
        @columns_names ||= @options[:column_data] || default_columns
      end

      def columns_names
        (@options[:column_names] || default_columns).map { |column| column.is_a?(Symbol) ? @klass.human_attribute_name(column) : column }
      end

      def filename
        @filename ||= [@options[:filename] || "#{@klass.model_name.plural}-#{Time.now.strftime('%Y-%m-%d')}", '.csv'].join
      end

      def render
        ::CSV.generate(col_sep: @options[:column_separator] || ',') do |csv|
          csv << columns_names

          each_record do |item|
            csv << column_data.map { |column| AbAdmin.pretty_data call_method_or_proc_on(item, column, exec: false) }
          end
        end
      end

      def each_record
        if @source.respond_to?(:find_each)
          @source.find_each do |item|
            yield item
          end
        else
          Array(@source).each do |item|
            yield item
          end
        end
      end

      protected

      def extract_klass
        if @source.respond_to?(:klass)
          @source.klass
        elsif @source.is_a?(Array)
          @source.first.try(:class)
        else
          @source.class
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ab_admin-0.8.3 lib/ab_admin/utils/csv_document.rb
ab_admin-0.8.2 lib/ab_admin/utils/csv_document.rb
ab_admin-0.8.1 lib/ab_admin/utils/csv_document.rb
ab_admin-0.8.0 lib/ab_admin/utils/csv_document.rb
ab_admin-0.7.0 lib/ab_admin/utils/csv_document.rb
ab_admin-0.6.1 lib/ab_admin/utils/csv_document.rb
ab_admin-0.6.0 lib/ab_admin/utils/csv_document.rb
ab_admin-0.5.0 lib/ab_admin/utils/csv_document.rb
ab_admin-0.4.0 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.6 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.5 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.4 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.3 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.2 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.1 lib/ab_admin/utils/csv_document.rb
ab_admin-0.3.0 lib/ab_admin/utils/csv_document.rb