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 I18n scope that will be used when looking up your column names in the current I18n locale.
Instance Method Summary collapse
-
#after_filter {|sheet| ... } ⇒ Object
The stored block that will be executed after your report is generated.
-
#before_filter {|sheet| ... } ⇒ 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 ⇒ Array<Column>
Returns the columns the builder will serialize.
-
#delete_columns(*column_names) ⇒ Object
Removes columns by name.
-
#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
Removes all columns, and add columns by name.
-
#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.
32 33 34 35 36 37 38 39 40 |
# File 'lib/active_admin/xls/builder.rb', line 32 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)
403 404 405 406 407 408 409 |
# File 'lib/active_admin/xls/builder.rb', line 403 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.
173 174 175 |
# File 'lib/active_admin/xls/builder.rb', line 173 def collection @collection end |
#i18n_scope ⇒ Object
If you do not set this, the column name will be titleized.
The I18n scope that will be used when looking up your column names in the current I18n locale. If you set it to [:active_admin, :resources, :posts] the serializer will render the value at active_admin.resources.posts.title in the current translations
110 111 112 |
# File 'lib/active_admin/xls/builder.rb', line 110 def i18n_scope @i18n_scope end |
Instance Method Details
#after_filter {|sheet| ... } ⇒ Object
The stored block that will be executed after your report is generated.
133 134 135 |
# File 'lib/active_admin/xls/builder.rb', line 133 def after_filter(&block) @after_filter = block end |
#before_filter {|sheet| ... } ⇒ Object
the stored block that will be executed before your report is generated.
155 156 157 |
# File 'lib/active_admin/xls/builder.rb', line 155 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.
184 185 186 187 188 189 |
# File 'lib/active_admin/xls/builder.rb', line 184 def clear_columns @columns_loaded = true @column_updates = [] @columns = [] end |
#column(name, &block) ⇒ Object
Add a column
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/active_admin/xls/builder.rb', line 211 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 ⇒ Array<Column>
Returns the columns the builder will serialize.
162 163 164 165 166 167 |
# File 'lib/active_admin/xls/builder.rb', line 162 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.
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/active_admin/xls/builder.rb', line 236 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
46 47 48 |
# File 'lib/active_admin/xls/builder.rb', line 46 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. Precedence is given to your hash
76 77 78 |
# File 'lib/active_admin/xls/builder.rb', line 76 def header_format=(format_hash) @header_format = header_format.merge(format_hash) end |
#only_columns(*column_names) ⇒ Object
Removes all columns, and add columns by name. Each column_name should be a symbol
252 253 254 255 256 257 |
# File 'lib/active_admin/xls/builder.rb', line 252 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
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/active_admin/xls/builder.rb', line 264 def serialize(collection, view_context = nil) @collection = collection @view_context = view_context book = Spreadsheet::Workbook.new sheet = book.create_worksheet load_columns unless @columns_loaded apply_filter @before_filter, sheet export_collection collection, sheet apply_filter @after_filter, sheet to_stream book end |
#skip_header ⇒ Object
Indicates that we do not want to serialize the column headers
99 100 101 |
# File 'lib/active_admin/xls/builder.rb', line 99 def skip_header @skip_header = true end |