class ActiveAdmin::CSVBuilder

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 col_sep: ";"
csv_builder.column :id

Attributes

columns[R]
options[R]
view_context[R]

Public Class Methods

default_for_resource(resource) click to toggle source

Return a default CSVBuilder for a resource The CSVBuilder’s columns would be Id followed by this resource’s content columns

# File lib/active_admin/csv_builder.rb, line 19
def self.default_for_resource(resource)
  new(resource: resource) do
    column(:id)
    resource.content_columns.each do |content_column|
      column(content_column.name.to_sym)
    end
  end
end
new(options={}, &block) click to toggle source
# File lib/active_admin/csv_builder.rb, line 30
def initialize(options={}, &block)
  @resource = options.delete(:resource)
  @columns, @options, @block = [], options, block
end

Public Instance Methods

column(name, &block) click to toggle source

Add a column

# File lib/active_admin/csv_builder.rb, line 36
def column(name, &block)
  @columns << Column.new(name, @resource, block)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/active_admin/csv_builder.rb, line 53
def method_missing(method, *args, &block)
  if @view_context.respond_to?(method)
    @view_context.send(method, *args, &block)
  else
    super
  end
end
render_columns(view_context = nil) click to toggle source

Runs the ‘csv` dsl block and render our columns Called from `index.csv.erb`, which passes in the current view context. This provides methods that could be called in the views to be called within the CSV block. Any method not defined on the CSV builder will instead be sent to the view context in order to emulate the capabilities of the `index` DSL.

# File lib/active_admin/csv_builder.rb, line 46
def render_columns(view_context = nil)
  @view_context = view_context
  @columns = [] # we want to re-render these every instance
  instance_eval &@block if @block.present?
  columns
end