Skip to content

Commit

Permalink
Merge pull request #113 from mcgregordan/master
Browse files Browse the repository at this point in the history
Update template handler for Rails 6
  • Loading branch information
straydogstudio committed May 6, 2019
2 parents d3f06de + ac3e01b commit 6bd62bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/axlsx_rails/template_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def default_format
Rails.version.to_f >= 5 ? Mime[:xlsx] : Mime::XLSX
end

def call(template)
def call(template, source = nil)
builder = StringIO.new
builder << "require 'axlsx';"
builder << "xlsx_author = defined?(xlsx_author).nil? ? nil : xlsx_author;"
Expand All @@ -21,7 +21,7 @@ def call(template)
builder << ":author => xlsx_author,"
builder << ":created_at => xlsx_created_at,"
builder << ":use_shared_strings => xlsx_use_shared_strings);"
builder << template.source
builder << source || template.source
builder << ";xlsx_package.to_stream.string;"
builder.string
end
Expand Down
11 changes: 10 additions & 1 deletion spec/axlsx_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
expect(handler.default_format).to eq(mime_type)
end

it "compiles to an excel spreadsheet" do
it "compiles to an excel spreadsheet when passing in a source" do
xlsx_package, wb = nil
source = "wb = xlsx_package.workbook;\nwb.add_worksheet(name: 'Test') do |sheet|\n\tsheet.add_row ['four', 'five', 'six']\n\tsheet.add_row ['d', 'e', 'f']\nend\n"
eval( AB.new.call template, source )
xlsx_package.serialize('/tmp/axlsx_temp.xlsx')
expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
expect(wb.cell(2,3)).to eq('c')
end

it "compiles to an excel spreadsheet when inferring source from template " do
xlsx_package, wb = nil
eval( AB.new.call template )
xlsx_package.serialize('/tmp/axlsx_temp.xlsx')
Expand Down

0 comments on commit 6bd62bc

Please sign in to comment.