Skip to content

Commit

Permalink
added support for skip_header in builder and dsl. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
randym committed Oct 12, 2013
1 parent 429ab93 commit 051c387
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
22 changes: 13 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
activeadmin-axlsx (2.1.2)
activeadmin-axlsx (3.0.0)
activeadmin (~> 0.6.0)
axlsx

Expand Down Expand Up @@ -51,11 +51,12 @@ GEM
arbre (1.0.1)
activesupport (>= 3.0.0)
arel (3.0.2)
atomic (1.1.14)
axlsx (2.0.1)
htmlentities (~> 4.3.1)
nokogiri (>= 1.4.1)
rubyzip (~> 1.0.0)
bcrypt-ruby (3.0.1)
bcrypt-ruby (3.1.2)
bourbon (3.1.3)
sass (>= 3.2.0)
thor
Expand Down Expand Up @@ -84,11 +85,12 @@ GEM
cucumber (>= 1.1.3)
nokogiri (>= 1.5.0)
database_cleaner (0.9.1)
devise (2.2.3)
devise (3.1.0)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (~> 3.1)
warden (~> 1.2.1)
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.4.0)
Expand Down Expand Up @@ -124,7 +126,7 @@ GEM
selenium-webdriver (>= 0.1.3)
jasmine-core (1.2.0)
journey (1.0.4)
jquery-rails (2.2.1)
jquery-rails (3.0.4)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
jslint_on_rails (1.0.7)
Expand Down Expand Up @@ -228,12 +230,14 @@ GEM
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
thor (0.16.0)
thread_safe (0.1.3)
atomic
tilt (1.3.3)
treetop (1.4.12)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.37)
warden (1.2.1)
tzinfo (0.3.38)
warden (1.2.3)
rack (>= 1.0)
websocket (1.0.3)
xpath (0.1.4)
Expand Down
8 changes: 7 additions & 1 deletion lib/active_admin/axlsx/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Builder
# end
# @see ActiveAdmin::Axlsx::DSL
def initialize(resource_class, options={}, &block)
@skip_header = false
@columns = resource_columns(resource_class)
parse_options options
instance_eval &block if block_given?
Expand All @@ -61,6 +62,11 @@ def header_style=(style_hash)
@header_style = header_style.merge(style_hash)
end

# Indicates that we do not want to serialize the column headers
def skip_header
@skip_header = true
end

# The scope to use when looking up column names to generate the report header
def i18n_scope
@i18n_scope ||= nil
Expand Down Expand Up @@ -154,7 +160,7 @@ def clean_up
end

def export_collection(collection)
header_row(collection)
header_row(collection) unless @skip_header
collection.each do |resource|
sheet.add_row resource_data(resource)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/axlsx/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveAdmin
module Axlsx
module DSL
delegate :ingnore_columns, :column, :after_filer, :i18n_scope, :header_style, to: :xlsx_builder, prefix: :config
delegate :ingnore_columns, :column, :after_filer, :i18n_scope, :header_style, :skip_header, to: :xlsx_builder, prefix: :config
# @see ActiveAdmin::Axlsx::Builder
def xlsx(options={}, &block)
config.xlsx_builder = ActiveAdmin::Axlsx::Builder.new(config.resource_class, options, &block)
Expand Down
32 changes: 32 additions & 0 deletions spec/axlsx/unit/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Axlsx
builder.columns.size.should == content_columns.size - 1
end

it 'lets us say we dont want the header' do
builder.skip_header
builder.instance_values["skip_header"].should be_true
end

it 'lets us add custom columns' do
builder.column(:hoge)
builder.columns.size.should == content_columns.size + 2
Expand Down Expand Up @@ -48,6 +53,33 @@ module Axlsx
end
end

context 'sheet generation without headers' do
let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }

let!(:posts) { [Post.new(title: 'bob', body: 'is a swell guy', author: users.first)] }

let!(:builder) {
Builder.new(Post, header_style: { sz: 10, fg_color: "FF0000" }, i18n_scope: [:axlsx, :post]) do
skip_header
end
}

before do
User.stub!(:all) { users }
Post.stub!(:all) { posts }
# disable clean up so we can get the package.
builder.stub(:clean_up) { false }
builder.serialize(Post.all)
@package = builder.send(:package)
@collection = builder.collection
end

it 'does not serialize the header' do
not_header = @package.workbook.worksheets.first.rows.first
not_header.cells.first.value.should_not == 'Title'
end
end

context 'Sheet generation with a highly customized configuration.' do

let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }
Expand Down
6 changes: 6 additions & 0 deletions spec/axlsx/unit/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Axlsx
column(:author) { |post| post.author.first_name }
before_filter { |sheet| sheet.add_row ['before_filter'] }
after_filter { |sheet| sheet.add_row['after_filter'] }
skip_header
end
end
config.xlsx_builder
Expand All @@ -37,6 +38,11 @@ module Axlsx
it "has an after filter set" do
builder.instance_values["after_filter"].should be_a(Proc)
end

it "indicates that the header should be excluded" do
builder.instance_values['skip_header'].should be_true
end

it "updates the header style" do
builder.header_style[:sz].should be(20)
end
Expand Down

0 comments on commit 051c387

Please sign in to comment.