Sha256: 93658bd6d327b51b846da4a9d219778dc29bd7b85875b3d09bcae82866f0bec0

Contents?: true

Size: 1.06 KB

Versions: 18

Compression:

Stored size: 1.06 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 }
  #
  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

    def initialize(&block)
      @columns = []
      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

18 entries across 18 versions & 4 rubygems

Version Path
activeadmin-0.4.4 lib/active_admin/csv_builder.rb
activeadmin-0.4.3 lib/active_admin/csv_builder.rb
activeadmin-0.4.2 lib/active_admin/csv_builder.rb
activeadmin-0.4.1 lib/active_admin/csv_builder.rb
activeadmin-0.4.0 lib/active_admin/csv_builder.rb
andrewroth_activeadmin-0.3.4.4 lib/active_admin/csv_builder.rb
andrewroth_activeadmin-0.3.4.3 lib/active_admin/csv_builder.rb
andrewroth_activeadmin-0.3.4.2 lib/active_admin/csv_builder.rb
andrewroth_activeadmin-0.3.4.1 lib/active_admin/csv_builder.rb
andrewroth_activeadmin-0.3.4 lib/active_admin/csv_builder.rb
activeadmin-0.3.4 lib/active_admin/csv_builder.rb
activeadmin-0.3.3 lib/active_admin/csv_builder.rb
activeadmin-0.3.2 lib/active_admin/csv_builder.rb
activeadmin-0.3.1 lib/active_admin/csv_builder.rb
activeadmin-0.3.0 lib/active_admin/csv_builder.rb
nsm-activeadmin-0.2.2 lib/active_admin/csv_builder.rb
saulolso-activeadmin-0.2.2.1 lib/active_admin/csv_builder.rb
saulolso-activeadmin-0.2.2 lib/active_admin/csv_builder.rb