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.column(:name, humanize: false)
csv_builder.column("name", humanize: false) { |resource| resource.full_name }

csv_builder = CSVBuilder.new col_sep: ";"
csv_builder = CSVBuilder.new humanize_name: false
csv_builder.column :id

Defined Under Namespace

Classes: Column

Constant Summary

COLUMN_TRANSITIVE_OPTIONS =
[:humanize_name].freeze

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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



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

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)



77
78
79
80
81
82
83
# File 'lib/active_admin/csv_builder.rb', line 77

def method_missing(method, *args, &block)
  if @view_context.respond_to? method
    @view_context.public_send method, *args, &block
  else
    super
  end
end

Instance Attribute Details

- (Object) columns (readonly)

Returns the value of attribute columns



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

def columns
  @columns
end

- (Object) options (readonly)

Returns the value of attribute options



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

def options
  @options
end

- (Object) view_context (readonly)

Returns the value of attribute view_context



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

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



22
23
24
25
26
27
28
29
# File 'lib/active_admin/csv_builder.rb', line 22

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) build(view_context, receiver)



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_admin/csv_builder.rb', line 44

def build(view_context, receiver)
  options = ActiveAdmin.application.csv_options.merge self.options
  columns = exec_columns view_context

  receiver << CSV.generate_line(columns.map{ |c| encode c.name, options }, options)

  view_context.send(:collection).find_each do |resource|
    resource = view_context.send :apply_decorator, resource
    receiver << CSV.generate_line(build_row(resource, columns, options), options)
  end
end

- (Object) build_row(resource, columns, options)



63
64
65
66
67
# File 'lib/active_admin/csv_builder.rb', line 63

def build_row(resource, columns, options)
  columns.map do |column|
    encode call_method_or_proc_on(resource, column.data), options
  end
end

- (Object) column(name, options = {}, &block)



40
41
42
# File 'lib/active_admin/csv_builder.rb', line 40

def column(name, options={}, &block)
  @columns << Column.new(name, @resource, column_transitive_options.merge(options), block)
end

- (Object) encode(content, options)



69
70
71
72
73
74
75
# File 'lib/active_admin/csv_builder.rb', line 69

def encode(content, options)
  if options[:encoding]
    content.to_s.encode! options[:encoding], options[:encoding_options]
  else
    content
  end
end

- (Object) exec_columns(view_context = nil)



56
57
58
59
60
61
# File 'lib/active_admin/csv_builder.rb', line 56

def exec_columns(view_context = nil)
  @view_context = view_context
  @columns = [] # we want to re-render these every instance
  instance_exec &@block if @block.present?
  columns
end