Class: ActiveAdmin::CSVBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/csv_builder.rb

Overview

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

Defined Under Namespace

Classes: Column

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CSVBuilder) initialize(options = {}, &block)

Returns a new instance of CSVBuilder



30
31
32
33
# File 'lib/active_admin/csv_builder.rb', line 30

def initialize(options={}, &block)
  @resource = options.delete(:resource)
  @columns, @options, @block = [], options, block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block)



53
54
55
56
57
58
59
# 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

Instance Attribute Details

- (Object) columns (readonly)

Returns the value of attribute columns



28
29
30
# File 'lib/active_admin/csv_builder.rb', line 28

def columns
  @columns
end

- (Object) options (readonly)

Returns the value of attribute options



28
29
30
# File 'lib/active_admin/csv_builder.rb', line 28

def options
  @options
end

- (Object) view_context (readonly)

Returns the value of attribute view_context



28
29
30
# File 'lib/active_admin/csv_builder.rb', line 28

def view_context
  @view_context
end

Class Method Details

+ (Object) default_for_resource(resource)

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



19
20
21
22
23
24
25
26
# 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

Instance Method Details

- (Object) column(name, &block)

Add a column



36
37
38
# File 'lib/active_admin/csv_builder.rb', line 36

def column(name, &block)
  @columns << Column.new(name, @resource, block)
end

- (Object) render_columns(view_context = nil)

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.



46
47
48
49
50
51
# 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