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
# File 'lib/active_admin/csv_builder.rb', line 33

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

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


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

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.name.to_sym }
  end
end

Instance Method Details

#build(controller, csv) ⇒ Object


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

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

  csv << bom if bom

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

  (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

  csv
end

#build_row(resource, columns, options) ⇒ Object


73
74
75
76
77
# File 'lib/active_admin/csv_builder.rb', line 73

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


38
39
40
# File 'lib/active_admin/csv_builder.rb', line 38

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

#encode(content, options) ⇒ Object


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

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

#exec_columns(view_context = nil) ⇒ Object


66
67
68
69
70
71
# File 'lib/active_admin/csv_builder.rb', line 66

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