Class: ActiveAdmin::Xls::Builder

Inherits:
Object
  • Object
show all
Includes:
MethodOrProcHelper
Defined in:
lib/active_admin/xls/builder.rb

Overview

Builder for xls data.

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_class, options = {}, &block) ⇒ Builder

Returns a new instance of Builder.

Examples:

ActiveAdmin::Xls::Builder.new(Post, i18n: [:xls]) do
  delete_columns :id, :created_at, :updated_at
  column(:author_name) { |post| post.author.name }
  after_filter do |sheet|
    # todo
  end
end

Parameters:

  • resource_class (Class)

    The resource this builder generate column information for.

  • options (Hash) (defaults to: {})

    the options for the builder

  • block (Block)

    Block given will evaluated against this instance of Builder. That means you can call any method on the builder from within that block.

Options Hash (options):

  • :header_format (Hash)

    A hash of format properties to apply to the header row. Any properties specified will be merged with the default header styles.

  • :i18n_scope (Array)

    the I18n scope to use when looking up localized column headers.

See Also:



32
33
34
35
36
37
38
39
40
# File 'lib/active_admin/xls/builder.rb', line 32

def initialize(resource_class, options = {}, &block)
  @skip_header = false
  @resource_class = resource_class
  @columns = []
  @columns_loaded = false
  @column_updates = []
  parse_options options
  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

#collectionObject (readonly)

Note:

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_scopeObject

Note:

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.

Examples:

With DSL

xls do
  after_filter do |sheet|
    row_number = sheet.dimensions[1]
    sheet.update_row(row_number)
    row_number += 1
    sheet.update_row(row_number, 'Author Name', 'Number of Posts')
    users = collection.map(&:author).uniq(&:id)
    users.each do |user|
      row_number += 1
      sheet.update_row(row_number,
                       "#{user.first_name} #{user.last_name}",
                       user.posts.size)
    end
  end
end

Yield Parameters:

  • sheet (Spreadsheet::Worksheet)

    the worksheet where the collection has been serialized



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.

Examples:

with DSL

xls do
  before_filter do |sheet|
    users = collection.map(&:author)
    users.each do |user|
      user.first_name = 'Set In Proc' if user.first_name == 'bob'
    end
    row_number = sheet.dimensions[1]
    sheet.update_row(row_number, 'Created', Time.zone.now)
    row_number += 1
    sheet.update_row(row_number, '')
  end
end

Yield Parameters:

  • sheet (Spreadsheet::Worksheet)

    the worksheet where the collection has been serialized



155
156
157
# File 'lib/active_admin/xls/builder.rb', line 155

def before_filter(&block)
  @before_filter = block
end

#clear_columnsObject 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.

Examples:

Using alias whitelist

Builder.new(Post, header_style: {}, i18n_scope: %i[xls post]) do
  whitelist
  column :title
end


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

Examples:

With block

xls(i18n_scope: [:rspec], header_style: { size: 20 }) do
  delete_columns :id, :created_at
  column(:author) { |post| post.author.first_name }
end

With default value

Builder.new(Post, header_style: {}, i18n_scope: %i[xls post]) do
  whitelist
  column :title
end

Parameters:

  • name (Symbol)

    The name of the column.

  • block (Proc)

    A block of code that is executed on the resource when generating row data for this 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

#columnsArray<Column>

Returns the columns the builder will serialize.

Returns:

  • (Array<Column>)

    columns configued on the builder.



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.

Examples:

In Builder.new

options = {
  header_style: { size: 10, color: 'red' },
  i18n_scope: %i[xls post]
}
Builder.new(Post, options) do
  delete_columns :id, :created_at, :updated_at
  column(:author) do |resource|
    "#{resource.author.first_name} #{resource.author.last_name}"
  end
end


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_formatHash 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

Examples:

In Builder.new

options = {
  header_format: { weight: :bold },
  i18n_scope: %i[xls post]
}
Builder.new(Post, options) do
  skip_header
end

With DSL

ActiveAdmin.register Post do
  xls(header_format: { weight: :bold }, i18n_scope: %i[xls post]) do
    skip_header
  end
end

Simple DSL without block

xls header_format: { weight: :bold }

See Also:



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

Examples:

config.xls_builder.only_columns :title, :author


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

Parameters:

  • collection (Enumerable)

    list of resources to serialize

  • view_context (defaults to: nil)

    object on which unknown methods may be executed

Returns:

  • (Spreadsheet::Workbook)


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_headerObject

Indicates that we do not want to serialize the column headers

Examples:

In Builder.new

options = {
  header_format: { weight: :bold },
  i18n_scope: %i[xls post]
}
Builder.new(Post, options) do
  skip_header
end

With DSL

ActiveAdmin.register Post do
  xls(header_format: { weight: :bold }, i18n_scope: %i[xls post]) do
    skip_header
  end
end


99
100
101
# File 'lib/active_admin/xls/builder.rb', line 99

def skip_header
  @skip_header = true
end