Class: ActiveAdmin::Axlsx::Builder

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

Overview

Builder for xlsx data using the axlsx gem.

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::Axlsx:Builder.new(Post, i18n: [:axlsx]) do
  delete_columns :id, :created_at, :updated_at
  column(:author_name) { |post| post.author.name }
  column(:
  after_filter { |sheet|
    sheet.add_row []

    sheet.add_row ['Author Name', 'Number of Posts'], :style => self.header_style
    data = labels = []
    User.all.each do |user|
      data << [user.posts.size]
      labels << user.name
      sheet.add_row [labels.last, data.last]
    end
    chart_color =  %w(88F700, 279CAC, B2A200, FD66A3, F20062, C8BA2B, 67E6F8, DFFDB9, FFE800, B6F0F8)
    sheet.add_chart(Axlsx::Pie3DChart, :title => "post by author") do |chart|
      chart.add_series :data => data, :labels => labels, :colors => chart_color
      chart.start_at 2, sheet.rows.size
      chart.end_at 3, sheet.rows.size + 20
    end
  }
end
@see ActiveAdmin::Axlsx::DSL

Parameters:

  • resource_class

    The resource this builder generate column information for.

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

    the options for this builder

  • Any (Block)

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

  • [Hash] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options



43
44
45
46
47
# File 'lib/active_admin/axlsx/builder.rb', line 43

def initialize(resource_class, options={}, &block)
  @columns = resource_columns(resource_class)
  parse_options options
  instance_eval &block if block_given?
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.



95
96
97
# File 'lib/active_admin/axlsx/builder.rb', line 95

def collection
  @collection
end

#columnsObject (readonly)

The columns this builder will be serializing



90
91
92
# File 'lib/active_admin/axlsx/builder.rb', line 90

def columns
  @columns
end

Instance Method Details

#after_filter(&block) ⇒ Object

The stored block that will be executed after your report is generated.



80
81
82
# File 'lib/active_admin/axlsx/builder.rb', line 80

def after_filter(&block)
  @after_filter = block
end

#before_filter(&block) ⇒ Object

the stored block that will be executed before your report is generated.



85
86
87
# File 'lib/active_admin/axlsx/builder.rb', line 85

def before_filter(&block)
  @before_filter = block
end

#clear_columnsObject

removes all columns from the builder. This is useful when you want to only render specific columns. To remove specific columns use ignore_column.



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

def clear_columns
  @columns = []
end

#column(name, &block) ⇒ Object

Add a column

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.



107
108
109
# File 'lib/active_admin/axlsx/builder.rb', line 107

def column(name, &block)
  @columns << Column.new(name, block)
end

#delete_columns(*column_names) ⇒ Object

removes columns by name each column_name should be a symbol



113
114
115
# File 'lib/active_admin/axlsx/builder.rb', line 113

def delete_columns(*column_names)
  @columns.delete_if { |column| column_names.include?(column.name) }
end

#header_styleHash

The default header style

Returns:

  • (Hash)


51
52
53
# File 'lib/active_admin/axlsx/builder.rb', line 51

def header_style
  @header_style ||= { :bg_color => '00', :fg_color => 'FF', :sz => 12, :alignment => { :horizontal => :center } }
end

#header_style=(style_hash) ⇒ Object

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 create and apply style.



60
61
62
# File 'lib/active_admin/axlsx/builder.rb', line 60

def header_style=(style_hash)
  @header_style = header_style.merge(style_hash)
end

#i18n_scopeObject

The scope to use when looking up column names to generate the report header



65
66
67
# File 'lib/active_admin/axlsx/builder.rb', line 65

def i18n_scope
  @i18n_scope ||= nil
end

#i18n_scope=(scope) ⇒ Object

Note:

If you do not set this, the column name will be titleized.

This is the I18n scope that will be used when looking up your colum 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



75
76
77
# File 'lib/active_admin/axlsx/builder.rb', line 75

def i18n_scope=(scope)
  @i18n_scope = scope
end

#serialize(collection) ⇒ Axlsx::Package

Serializes the collection provided

Returns:

  • (Axlsx::Package)


119
120
121
122
123
124
125
# File 'lib/active_admin/axlsx/builder.rb', line 119

def serialize(collection)
  @collection = collection
  apply_filter @before_filter
  export_collection(collection)
  apply_filter @after_filter
  to_stream
end