Class: ActiveAdmin::CSVBuilder
- Inherits:
-
Object
- Object
- ActiveAdmin::CSVBuilder
- Defined in:
- lib/active_admin/csv_builder.rb
Overview
Defined Under Namespace
Classes: Column
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
-
#column(name, &block) ⇒ Object
Add a column.
-
#initialize(options = {}, &block) ⇒ CSVBuilder
constructor
A new instance of CSVBuilder.
- #method_missing(method, *args, &block) ⇒ Object
-
#render_columns(view_context = nil) ⇒ Object
Runs the ‘csv` dsl block and render our columns Called from `index.csv.erb`, which passes in the current view context.
Constructor Details
#initialize(options = {}, &block) ⇒ CSVBuilder
Returns a new instance of CSVBuilder.
30 31 32 33 |
# File 'lib/active_admin/csv_builder.rb', line 30 def initialize(={}, &block) @resource = .delete(:resource) @columns, @options, @block = [], , block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/active_admin/csv_builder.rb', line 53 def method_missing(method, *args, &block) if @view_context.respond_to?(method) @view_context.send(method, *args, &block) else super end end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
28 29 30 |
# File 'lib/active_admin/csv_builder.rb', line 28 def columns @columns end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/active_admin/csv_builder.rb', line 28 def @options end |
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
28 29 30 |
# File 'lib/active_admin/csv_builder.rb', line 28 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
19 20 21 22 23 24 25 26 |
# File 'lib/active_admin/csv_builder.rb', line 19 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
#column(name, &block) ⇒ Object
Add a column
36 37 38 |
# File 'lib/active_admin/csv_builder.rb', line 36 def column(name, &block) @columns << Column.new(name, @resource, block) end |
#render_columns(view_context = nil) ⇒ Object
Runs the ‘csv` dsl block and render our columns Called from `index.csv.erb`, which passes in the current view context. This provides methods that could be called in the views to be called within the CSV block. Any method not defined on the CSV builder will instead be sent to the view context in order to emulate the capabilities of the `index` DSL.
46 47 48 49 50 51 |
# File 'lib/active_admin/csv_builder.rb', line 46 def render_columns(view_context = nil) @view_context = view_context @columns = [] # we want to re-render these every instance instance_eval &@block if @block.present? columns end |