Skip to content

Commit 051c387

Browse files
committed
added support for skip_header in builder and dsl. #15
1 parent 429ab93 commit 051c387

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

Diff for: Gemfile.lock

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
activeadmin-axlsx (2.1.2)
4+
activeadmin-axlsx (3.0.0)
55
activeadmin (~> 0.6.0)
66
axlsx
77

@@ -51,11 +51,12 @@ GEM
5151
arbre (1.0.1)
5252
activesupport (>= 3.0.0)
5353
arel (3.0.2)
54+
atomic (1.1.14)
5455
axlsx (2.0.1)
5556
htmlentities (~> 4.3.1)
5657
nokogiri (>= 1.4.1)
5758
rubyzip (~> 1.0.0)
58-
bcrypt-ruby (3.0.1)
59+
bcrypt-ruby (3.1.2)
5960
bourbon (3.1.3)
6061
sass (>= 3.2.0)
6162
thor
@@ -84,11 +85,12 @@ GEM
8485
cucumber (>= 1.1.3)
8586
nokogiri (>= 1.5.0)
8687
database_cleaner (0.9.1)
87-
devise (2.2.3)
88+
devise (3.1.0)
8889
bcrypt-ruby (~> 3.0)
8990
orm_adapter (~> 0.1)
90-
railties (~> 3.1)
91-
warden (~> 1.2.1)
91+
railties (>= 3.2.6, < 5)
92+
thread_safe (~> 0.1)
93+
warden (~> 1.2.3)
9294
diff-lcs (1.1.3)
9395
erubis (2.7.0)
9496
execjs (1.4.0)
@@ -124,7 +126,7 @@ GEM
124126
selenium-webdriver (>= 0.1.3)
125127
jasmine-core (1.2.0)
126128
journey (1.0.4)
127-
jquery-rails (2.2.1)
129+
jquery-rails (3.0.4)
128130
railties (>= 3.0, < 5.0)
129131
thor (>= 0.14, < 2.0)
130132
jslint_on_rails (1.0.7)
@@ -228,12 +230,14 @@ GEM
228230
tilt (~> 1.1, != 1.3.0)
229231
sqlite3 (1.3.6)
230232
thor (0.16.0)
233+
thread_safe (0.1.3)
234+
atomic
231235
tilt (1.3.3)
232-
treetop (1.4.12)
236+
treetop (1.4.15)
233237
polyglot
234238
polyglot (>= 0.3.1)
235-
tzinfo (0.3.37)
236-
warden (1.2.1)
239+
tzinfo (0.3.38)
240+
warden (1.2.3)
237241
rack (>= 1.0)
238242
websocket (1.0.3)
239243
xpath (0.1.4)

Diff for: lib/active_admin/axlsx/builder.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Builder
4141
# end
4242
# @see ActiveAdmin::Axlsx::DSL
4343
def initialize(resource_class, options={}, &block)
44+
@skip_header = false
4445
@columns = resource_columns(resource_class)
4546
parse_options options
4647
instance_eval &block if block_given?
@@ -61,6 +62,11 @@ def header_style=(style_hash)
6162
@header_style = header_style.merge(style_hash)
6263
end
6364

65+
# Indicates that we do not want to serialize the column headers
66+
def skip_header
67+
@skip_header = true
68+
end
69+
6470
# The scope to use when looking up column names to generate the report header
6571
def i18n_scope
6672
@i18n_scope ||= nil
@@ -154,7 +160,7 @@ def clean_up
154160
end
155161

156162
def export_collection(collection)
157-
header_row(collection)
163+
header_row(collection) unless @skip_header
158164
collection.each do |resource|
159165
sheet.add_row resource_data(resource)
160166
end

Diff for: lib/active_admin/axlsx/dsl.rb

+1-1
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, to: :xlsx_builder, prefix: :config
4+
delegate :ingnore_columns, :column, :after_filer, :i18n_scope, :header_style, :skip_header, 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)

Diff for: spec/axlsx/unit/builder_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ module Axlsx
2020
builder.columns.size.should == content_columns.size - 1
2121
end
2222

23+
it 'lets us say we dont want the header' do
24+
builder.skip_header
25+
builder.instance_values["skip_header"].should be_true
26+
end
27+
2328
it 'lets us add custom columns' do
2429
builder.column(:hoge)
2530
builder.columns.size.should == content_columns.size + 2
@@ -48,6 +53,33 @@ module Axlsx
4853
end
4954
end
5055

56+
context 'sheet generation without headers' do
57+
let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }
58+
59+
let!(:posts) { [Post.new(title: 'bob', body: 'is a swell guy', author: users.first)] }
60+
61+
let!(:builder) {
62+
Builder.new(Post, header_style: { sz: 10, fg_color: "FF0000" }, i18n_scope: [:axlsx, :post]) do
63+
skip_header
64+
end
65+
}
66+
67+
before do
68+
User.stub!(:all) { users }
69+
Post.stub!(:all) { posts }
70+
# disable clean up so we can get the package.
71+
builder.stub(:clean_up) { false }
72+
builder.serialize(Post.all)
73+
@package = builder.send(:package)
74+
@collection = builder.collection
75+
end
76+
77+
it 'does not serialize the header' do
78+
not_header = @package.workbook.worksheets.first.rows.first
79+
not_header.cells.first.value.should_not == 'Title'
80+
end
81+
end
82+
5183
context 'Sheet generation with a highly customized configuration.' do
5284

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

Diff for: spec/axlsx/unit/dsl_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Axlsx
1111
column(:author) { |post| post.author.first_name }
1212
before_filter { |sheet| sheet.add_row ['before_filter'] }
1313
after_filter { |sheet| sheet.add_row['after_filter'] }
14+
skip_header
1415
end
1516
end
1617
config.xlsx_builder
@@ -37,6 +38,11 @@ module Axlsx
3738
it "has an after filter set" do
3839
builder.instance_values["after_filter"].should be_a(Proc)
3940
end
41+
42+
it "indicates that the header should be excluded" do
43+
builder.instance_values['skip_header'].should be_true
44+
end
45+
4046
it "updates the header style" do
4147
builder.header_style[:sz].should be(20)
4248
end

0 commit comments

Comments
 (0)