diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 595bd56..979b3d6 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -1,21 +1,21 @@ # -*- coding: utf-8 -*- -# Axlsx is a gem or generating excel spreadsheets with charts, images and many other features. -# +# Axlsx is a gem or generating excel spreadsheets with charts, images and many other features. +# # acts_as_xlsx provides integration into active_record for Axlsx. -# +# require 'axlsx' -# Adding to the Axlsx module +# Adding to the Axlsx module # @see http://github.com/randym/axlsx module Axlsx # === Overview # This module defines the acts_as_xlsx class method and provides to_xlsx support to both AR classes and instances module Ar - + def self.included(base) # :nodoc: base.send :extend, ClassMethods end - + # Class methods for the mixin module ClassMethods @@ -26,8 +26,9 @@ module ClassMethods # class MyModel < ActiveRecord::Base # acts_as_xlsx :columns=> [:id, :created_at, :updated_at], :i18n => 'activerecord.attributes' def acts_as_xlsx(options={}) - cattr_accessor :xlsx_i18n, :xlsx_columns + cattr_accessor :xlsx_i18n, :xlsx_columns, :xlsx_humanize_column_names self.xlsx_i18n = options.delete(:i18n) || false + self.xlsx_humanize_column_names = options.delete(:humanize_column_names) || false self.xlsx_columns = options.delete(:columns) extend Axlsx::Ar::SingletonMethods end @@ -61,28 +62,31 @@ def to_xlsx(options = {}) row_style = p.workbook.styles.add_style(row_style) unless row_style.nil? header_style = p.workbook.styles.add_style(header_style) unless header_style.nil? i18n = self.xlsx_i18n == true ? 'activerecord.attributes' : i18n - sheet_name = options.delete(:name) || (i18n ? I18n.t("#{i18n}.#{table_name.underscore}") : table_name.humanize) + humanize_column_names = (self.xlsx_humanize_column_names == true) || false + sheet_name = options.delete(:name) || (i18n ? I18n.t("#{i18n}.#{table_name.underscore}") : table_name.humanize) data = options.delete(:data) || [*find(:all, options)] data.compact! data.flatten! return p if data.empty? p.workbook.add_worksheet(:name=>sheet_name) do |sheet| - + col_labels = if i18n - columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") } - else - columns.map { |c| c.to_s.humanize } - end - + columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") } + elsif humanize_column_names + columns.map { |c| c.to_s.humanize } + else + columns + end + sheet.add_row col_labels, :style=>header_style - + data.each do |r| row_data = columns.map do |c| if c.to_s =~ /\./ v = r; c.to_s.split('.').each { |method| v = v.send(method) }; v else - r.send(c) + r.send(c) end end sheet.add_row row_data, :style=>row_style, :types=>types diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index 39283ca..961b79b 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -15,7 +15,6 @@ def test_xlsx_options assert_equal([:name, :title, :content, :votes, :ranking], Post.xlsx_columns) assert_equal('activerecord.attributes', Post.xlsx_i18n) end - end class TestToXlsx < Test::Unit::TestCase @@ -27,7 +26,7 @@ def test_to_xlsx_with_package def test_to_xlsx_with_name p = Post.to_xlsx :name=>'bob' - assert_equal(p.workbook.worksheets.first.name, 'bob') + assert_equal(p.workbook.worksheets.first.name, 'bob') end def test_xlsx_columns @@ -39,14 +38,12 @@ def test_to_xslx_vanilla assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value) assert_equal(2,p.workbook.worksheets.first.rows.last.cells.first.value) end - - + def test_to_xslx_with_provided_data p = Post.to_xlsx :data => Post.where(:title => "This is the first post").all assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value) assert_equal(1,p.workbook.worksheets.first.rows.last.cells.first.value) end - def test_columns p = Post.to_xlsx :columns => [:name, :title, :content, :votes] @@ -69,9 +66,6 @@ def test_chained_method assert_equal("Name", sheet.rows.first.cells.first.value) assert_equal(Post.last.comments.last.author.name, sheet.rows.last.cells.last.value) end - - - end