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_name: false)
csv_builder.column("name", humanize_name: 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 collapse

COLUMN_TRANSITIVE_OPTIONS =
[:humanize_name].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ CSVBuilder

Returns a new instance of CSVBuilder.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/active_admin/csv_builder.rb', line 89

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

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



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

def view_context
  @view_context
end

Class Method Details

.default_for_resource(resource) ⇒ Object

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



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

def self.default_for_resource(resource)
  new resource: resource do
    column :id
    resource.content_columns.each { |c| column c }
  end
end

Instance Method Details

#build(controller, csv) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_admin/csv_builder.rb', line 45

def build(controller, csv)
  columns = exec_columns controller.view_context
  bom = options[:byte_order_mark]
  column_names = options.delete(:column_names) { true }
  csv_options = options.except :encoding_options, :humanize_name, :byte_order_mark

  csv << bom if bom

  if column_names
    csv << CSV.generate_line(columns.map { |c| encode c.name, options }, **csv_options)
  end

  controller.send(:in_paginated_batches) do |resource|
    csv << CSV.generate_line(build_row(resource, columns, options), **csv_options)
  end

  csv
end

#build_row(resource, columns, options) ⇒ Object



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

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

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



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

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

#encode(content, options) ⇒ Object



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

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

#exec_columns(view_context = nil) ⇒ Object



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

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