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 { |sheet|

  }
end
@see ActiveAdmin::xls::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 within that block.

  • [Hash] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options



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

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)



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

#collectionObject (readonly)

Note:

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_scopeObject

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_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.



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

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.



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

#columnsObject

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_formatHash Also known as: header_style

The default header style

Returns:

  • (Hash)


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

Returns:

  • (Spreadsheet::Workbook)


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_headerObject

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