diff options
| author | Randy Morgan <[email protected]> | 2011-11-28 18:52:27 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2011-11-28 18:52:27 +0900 |
| commit | 51a57d7ef89ab5c5eea4341d8e3728fbf9d560f9 (patch) | |
| tree | 60bcb46811fcbca96d1838d523b90f2f76a32453 /lib | |
| parent | f76f7e3eebd1bdb423c798ca3b14dc7d5038c222 (diff) | |
| download | caxlsx-51a57d7ef89ab5c5eea4341d8e3728fbf9d560f9.tar.gz caxlsx-51a57d7ef89ab5c5eea4341d8e3728fbf9d560f9.zip | |
removing unused CGI require and adding a few more options to active record plugin
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/ar.rb | 33 |
2 files changed, 24 insertions, 11 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 0a84a7e1..da6874de 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -29,7 +29,7 @@ require 'zip/zip' #core dependencies require 'bigdecimal' require 'time' -require 'CGI' + # xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine. module Axlsx diff --git a/lib/axlsx/ar.rb b/lib/axlsx/ar.rb index f2e116c1..2f11228a 100644 --- a/lib/axlsx/ar.rb +++ b/lib/axlsx/ar.rb @@ -13,7 +13,11 @@ module Axlsx module ClassMethods # adds in the instance and singleton methods - def acts_as_axlsx(options={}) + def acts_as_axlsx(options={}) + @xlsx_reject = options.delete(:reject) || [] + @xlsx_only = options.delete(:only) || [] + @xlsx_methods = options.delete(:methods) || [] + @i18n = options.delete(:i18n) || false include Axlsx::Ar::InstanceMethods extend Axlsx::Ar::SingletonMethods end @@ -24,38 +28,47 @@ module Axlsx # Maps the AR class to an Axlsx package # options are passed into AR find - # @param [Symbol, :all, etc. + # @param [Symbol, Integer] :all, :first, id etc. # @option options [Integer] header_style to apply to the first row of field names # @option options [Array, Symbol] an array of Axlsx types for each cell in data rows or a single type that will be applied to all types. # @option options [Integer, Array] style The style to pass to Worksheet#add_row + # @option options [Array] reject The names fo columns to exclude from the report # @see Worksheet#add_row def to_xlsx(number = :all, options = {}) row_style = options.delete(:style) header_style = options.delete(:header_style) || row_style types = options.delete(:types) + @xlsx_reject << options.delete(:reject) unless options[:reject].nil? + - data = [*find(number, options)] - data.compact! - data.flatten! - columns = data.first.attributes.keys p = Package.new 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? - + + data = [*find(number, options)] + data.compact! + data.flatten! + return p if data.empty? + @xlsx_columns = data.first.attributes.keys - @xlsx_reject.map { |r| r = r.to_s } p.workbook.add_worksheet(:name=>table_name.humanize) do |sheet| - sheet.add_row columns, :style=>header_style + col_labels = @i18n == false ? @xlsx_columns : @xlsx_columns.map { |c| I18n.t("#{@i18n}.#{self.name.underscore}.#{c}") } + + sheet.add_row col_labels, :style=>header_style data.each do |r| - sheet.add_row r.attributes.values, :style=>row_style, :types=>types + row_data = @xlsx_columns.map { |c| r.attributes[c] } + sheet.add_row row_data, :style=>row_style, :types=>types end end p end - end # Empty module - I really like ruports way of allowing :include, :only, :exclude # and am looking to add something like that in the next release module InstanceMethods + def to_xlsx(options={}) + self.class.to_xlsx(self.id, options) + end end |
