Skip to content

Commit f11d357

Browse files
committed
add whitelist builder and dsl option #2
1 parent 06b16f8 commit f11d357

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/active_admin/axlsx/builder.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ def clear_columns
106106
@columns = []
107107
end
108108

109+
# Clears the default columns array so you can whitelist only the columns you
110+
# want to export
111+
def whitelist
112+
@columns = []
113+
end
114+
109115
# Add a column
110116
# @param [Symbol] name The name of the column.
111117
# @param [Proc] block A block of code that is executed on the resource

lib/active_admin/axlsx/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ActiveAdmin
22
module Axlsx
33
module DSL
4-
delegate :ingnore_columns, :column, :after_filer, :i18n_scope, :header_style, :skip_header, to: :xlsx_builder, prefix: :config
4+
delegate :ingnore_columns, :column, :after_filer, :i18n_scope, :header_style, :skip_header, :white_list, to: :xlsx_builder, prefix: :config
55
# @see ActiveAdmin::Axlsx::Builder
66
def xlsx(options={}, &block)
77
config.xlsx_builder = ActiveAdmin::Axlsx::Builder.new(config.resource_class, options, &block)

spec/axlsx/unit/builder_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ module Axlsx
8080
end
8181
end
8282

83+
context 'whitelisted sheet generation' do
84+
let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }
85+
86+
let!(:posts) { [Post.new(title: 'bob', body: 'is a swell guy', author: users.first)] }
87+
88+
let!(:builder) {
89+
Builder.new(Post, header_style: { sz: 10, fg_color: "FF0000" }, i18n_scope: [:axlsx, :post]) do
90+
skip_header
91+
whitelist
92+
column :title
93+
end
94+
}
95+
96+
before do
97+
User.stub!(:all) { users }
98+
Post.stub!(:all) { posts }
99+
# disable clean up so we can get the package.
100+
builder.stub(:clean_up) { false }
101+
builder.serialize(Post.all)
102+
@package = builder.send(:package)
103+
@collection = builder.collection
104+
end
105+
106+
it 'does not serialize the header' do
107+
sheet = @package.workbook.worksheets.first
108+
sheet.rows.first.cells.size.should == 1
109+
sheet.rows.first.cells.first.value.should == @collection.first.title
110+
end
111+
end
112+
83113
context 'Sheet generation with a highly customized configuration.' do
84114

85115
let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }

0 commit comments

Comments
 (0)