Sha256: a2f907c0df454538f53f2d22434cce2a7edb45647d3bb00779f1cecbe47fe786

Contents?: true

Size: 1.3 KB

Versions: 47

Compression:

Stored size: 1.3 KB

Contents

module ActiveAdmin
  # CSVBuilder stores CSV configuration
  #
  # Usage example:
  #
  #   csv_builder = CSVBuilder.new
  #   csv_builder.column :id
  #   csv_builder.column("Name") { |resource| resource.full_name }
  #
  #   csv_builder = CSVBuilder.new :separator => ";"
  #   csv_builder.column :id
  #
  #
  class CSVBuilder

    # Return a default CSVBuilder for a resource
    # The CSVBuilder's columns would be Id followed by this
    # resource's content columns
    def self.default_for_resource(resource)
      new.tap do |csv_builder|
        csv_builder.column(:id)
        resource.content_columns.each do |content_column|
          csv_builder.column(content_column.name.to_sym)
        end
      end
    end

    attr_reader :columns, :column_separator, :options

    def initialize(options={}, &block)
      @columns          = []
      @column_separator = options.delete(:separator)
      @options          = options.delete(:options)
      instance_eval &block if block_given?
    end

    # Add a column
    def column(name, &block)
      @columns << Column.new(name, block)
    end

    class Column
      attr_reader :name, :data
      
      def initialize(name, block = nil)
        @name = name.is_a?(Symbol) ? name.to_s.titleize : name
        @data = block || name.to_sym
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
lalala-4.0.0.dev.59 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
lalala-4.0.0.dev.58 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
lalala-4.0.0.dev.57 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
lalala-4.0.0.dev.56 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
lalala-4.0.0.dev.50 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
lalala-4.0.0.dev.46 vendor/deps/active_admin/lib/active_admin/csv_builder.rb
activeadmin-0.5.1 lib/active_admin/csv_builder.rb