Class: ActiveAdmin::Xls::Builder
- Inherits:
-
Object
- Object
- ActiveAdmin::Xls::Builder
- Includes:
- MethodOrProcHelper
- Defined in:
- lib/active_admin/xls/builder.rb
Overview
Builder for xls data.
Defined Under Namespace
Classes: Column
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
The collection we are serializing.
-
#i18n_scope ⇒ Object
The scope to use when looking up column names to generate the report header.
Instance Method Summary collapse
-
#after_filter(&block) ⇒ Object
The stored block that will be executed after your report is generated.
-
#before_filter(&block) ⇒ Object
the stored block that will be executed before your report is generated.
-
#clear_columns ⇒ Object
(also: #whitelist)
removes all columns from the builder.
-
#column(name, &block) ⇒ Object
Add a column.
-
#columns ⇒ Object
The columns this builder will be serializing.
-
#delete_columns(*column_names) ⇒ Object
removes columns by name each column_name should be a symbol.
-
#header_format ⇒ Hash
(also: #header_style)
The default header style.
-
#header_format=(format_hash) ⇒ Object
(also: #header_style=)
This has can be used to override the default header style for your sheet.
-
#initialize(resource_class, options = {}, &block) ⇒ Builder
constructor
A new instance of Builder.
-
#only_columns(*column_names) ⇒ Object
remove all columns, and add columns by name each column_name should be a symbol.
-
#serialize(collection, view_context = nil) ⇒ Spreadsheet::Workbook
Serializes the collection provided.
-
#skip_header ⇒ Object
Indicates that we do not want to serialize the column headers.
Constructor Details
#initialize(resource_class, options = {}, &block) ⇒ Builder
Returns a new instance of Builder.
30 31 32 33 34 35 36 37 38 |
# File 'lib/active_admin/xls/builder.rb', line 30 def initialize(resource_class, = {}, &block) @skip_header = false @resource_class = resource_class @columns = [] @columns_loaded = false @column_updates = [] instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments) ⇒ Object (private)
285 286 287 288 289 290 291 |
# File 'lib/active_admin/xls/builder.rb', line 285 def method_missing(method_name, *arguments) if @view_context.respond_to? method_name @view_context.send method_name, *arguments else super end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
This is only available after serialize has been called,
The collection we are serializing. and is reset on each subsequent call.
97 98 99 |
# File 'lib/active_admin/xls/builder.rb', line 97 def collection @collection end |
#i18n_scope ⇒ Object
The scope to use when looking up column names to generate the report header
66 67 68 |
# File 'lib/active_admin/xls/builder.rb', line 66 def i18n_scope @i18n_scope end |
Instance Method Details
#after_filter(&block) ⇒ Object
The stored block that will be executed after your report is generated.
77 78 79 |
# File 'lib/active_admin/xls/builder.rb', line 77 def after_filter(&block) @after_filter = block end |
#before_filter(&block) ⇒ Object
the stored block that will be executed before your report is generated.
82 83 84 |
# File 'lib/active_admin/xls/builder.rb', line 82 def before_filter(&block) @before_filter = block end |
#clear_columns ⇒ Object Also known as: whitelist
removes all columns from the builder. This is useful when you want to only render specific columns. To remove specific columns use ignore_column.
102 103 104 105 106 107 |
# File 'lib/active_admin/xls/builder.rb', line 102 def clear_columns @columns_loaded = true @column_updates = [] @columns = [] end |
#column(name, &block) ⇒ Object
Add a column
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/active_admin/xls/builder.rb', line 117 def column(name, &block) if @columns_loaded columns << Column.new(name, block) else column_lambda = lambda do column(name, &block) end @column_updates << column_lambda end end |
#columns ⇒ Object
The columns this builder will be serializing
87 88 89 90 91 92 |
# File 'lib/active_admin/xls/builder.rb', line 87 def columns # execute each update from @column_updates # set @columns_loaded = true load_columns unless @columns_loaded @columns end |
#delete_columns(*column_names) ⇒ Object
removes columns by name each column_name should be a symbol
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/active_admin/xls/builder.rb', line 130 def delete_columns(*column_names) if @columns_loaded columns.delete_if { |column| column_names.include?(column.name) } else delete_lambda = lambda do delete_columns(*column_names) end @column_updates << delete_lambda end end |
#header_format ⇒ Hash Also known as: header_style
The default header style
42 43 44 |
# File 'lib/active_admin/xls/builder.rb', line 42 def header_format @header_format ||= {} end |
#header_format=(format_hash) ⇒ Object Also known as: header_style=
This has can be used to override the default header style for your sheet. Any values you provide will be merged with the default styles. Precidence is given to your hash for more details on how to create and apply style.
53 54 55 |
# File 'lib/active_admin/xls/builder.rb', line 53 def header_format=(format_hash) @header_format = header_format.merge(format_hash) end |
#only_columns(*column_names) ⇒ Object
remove all columns, and add columns by name each column_name should be a symbol
143 144 145 146 147 148 |
# File 'lib/active_admin/xls/builder.rb', line 143 def only_columns(*column_names) clear_columns column_names.each do |column_name| column column_name end end |
#serialize(collection, view_context = nil) ⇒ Spreadsheet::Workbook
Serializes the collection provided
152 153 154 155 156 157 158 159 160 |
# File 'lib/active_admin/xls/builder.rb', line 152 def serialize(collection, view_context = nil) @collection = collection @view_context = view_context load_columns unless @columns_loaded apply_filter @before_filter export_collection(collection) apply_filter @after_filter to_stream end |
#skip_header ⇒ Object
Indicates that we do not want to serialize the column headers
60 61 62 |
# File 'lib/active_admin/xls/builder.rb', line 60 def skip_header @skip_header = true end |