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.


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

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


94
95
96
97
98
99
100
# File 'lib/active_admin/csv_builder.rb', line 94

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


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

def columns
  @columns
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#view_contextObject (readonly)

Returns the value of attribute view_context


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

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


22
23
24
25
26
27
# 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 { |c| column c }
  end
end

Instance Method Details

#build(controller, csv) ⇒ Object


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

def build(controller, csv)
  @collection = controller.send :find_collection, except: :pagination
  columns = exec_columns controller.view_context
  bom = options.delete :byte_order_mark
  column_names = options.delete(:column_names) { true }
  csv_options = options.except :encoding_options, :humanize_name

  csv << bom if bom

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

  ActiveRecord::Base.uncached do
    (1..paginated_collection.total_pages).each do |page|
      paginated_collection(page).each do |resource|
        resource = controller.send :apply_decorator, resource
        csv << CSV.generate_line(build_row(resource, columns, options), **csv_options)
      end
    end
  end

  csv
end

#build_row(resource, columns, options) ⇒ Object


76
77
78
79
80
# File 'lib/active_admin/csv_builder.rb', line 76

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


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

#encode(content, options) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_admin/csv_builder.rb', line 82

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


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

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