Class: ActiveAdmin::CSVBuilder
- Inherits:
-
Object
- Object
- ActiveAdmin::CSVBuilder
- 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
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #build(controller, csv) ⇒ Object
- #build_row(resource, columns, options) ⇒ Object
- #column(name, options = {}, &block) ⇒ Object
- #encode(content, options) ⇒ Object
- #exec_columns(view_context = nil) ⇒ Object
-
#initialize(options = {}, &block) ⇒ CSVBuilder
constructor
A new instance of CSVBuilder.
- #method_missing(method, *args, &block) ⇒ Object
- #sanitize(content) ⇒ Object
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( = {}, &block) @resource = .delete(:resource) @columns = [] @options = ActiveAdmin.application..merge @block = block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/active_admin/csv_builder.rb', line 93 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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
30 31 32 |
# File 'lib/active_admin/csv_builder.rb', line 30 def columns @columns end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
30 31 32 |
# File 'lib/active_admin/csv_builder.rb', line 30 def @options end |
#view_context ⇒ Object (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 = [:byte_order_mark] column_names = .delete(:column_names) { true } = .except :encoding_options, :humanize_name, :byte_order_mark csv << bom if bom if column_names csv << CSV.generate_line(columns.map { |c| sanitize(encode(c.name, )) }, **) end controller.send(:in_paginated_batches) do |resource| csv << CSV.generate_line(build_row(resource, columns, ), **) 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, ) columns.map do |column| sanitize(encode(call_method_or_proc_on(resource, column.data), )) end end |
#column(name, options = {}, &block) ⇒ Object
41 42 43 |
# File 'lib/active_admin/csv_builder.rb', line 41 def column(name, = {}, &block) @columns << Column.new(name, @resource, .merge(), 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, ) if [:encoding] if [:encoding_options] content.to_s.encode [:encoding], **[:encoding_options] else content.to_s.encode [: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 |