diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25cf7e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +gemfiles/*.lock +Gemfile.lock +.ruby-version +acts_as_xlsx.sqlite3.db +Gemfile.lock diff --git a/.travis.yml b/.travis.yml index b78f7c3..b1baa85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,23 @@ bundler_args: --binstubs rvm: - - 1.8.7 - - 1.9.2 - 1.9.3 + - 2.0.0 + - 2.1.1 +gemfile: + - gemfiles/2.3.gemfile + - gemfiles/3.1.gemfile + - gemfiles/4.1.gemfile + +matrix: + exclude: + - rvm: 2.0.0 + gemfile: gemfiles/2.3.gemfile + - rvm: 2.1.1 + gemfile: gemfiles/2.3.gemfile + + +script: "bundle exec rake test" notifications: - irc: "irc.freenode.org#axlsx + irc: "irc.freenode.org#axlsx" diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..591745a --- /dev/null +++ b/Appraisals @@ -0,0 +1,15 @@ +# must exclude Ruby over 1.9.3 +appraise "2.3" do + gem "activerecord", "~> 2.3.8" + gem "acts_as_xlsx", :path => "../" +end + +appraise "3.1" do + gem "activerecord", "~> 3.1.0" + gem "acts_as_xlsx", :path => "../" +end + +appraise "4.1" do + gem "activerecord", "~> 4.1.0" + gem "acts_as_xlsx", :path => "../" +end diff --git a/Gemfile b/Gemfile index df87cd4..851fabc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,2 @@ -source :rubygems -gemspec \ No newline at end of file +source 'https://rubygems.org' +gemspec diff --git a/README.md b/README.md index 37efa47..1a35a4e 100644 --- a/README.md +++ b/README.md @@ -2,28 +2,29 @@ Acts as xlsx: Office Open XML Spreadsheet Generation plugin for active record ==================================== [![Build Status](https://secure.travis-ci.org/randym/acts_as_xlsx.png)](http://travis-ci.org/randym/acts_as_xlsx/) -**IRC**: [irc.freenode.net / #axlsx](irc://irc.freenode.net/axlsx) -**Git**: [http://github.com/randym/acts_as_xlsx](http://github.com/randym/acts_as_xlsx) -**Author**: Randy Morgan -**Copyright**: 2011 -**License**: MIT License -**Latest Version**: 1.0.6 -**Ruby Version**: 1.8.7 - 1.9.3 -**Release Date**: July 27th 2012 +**IRC**: [irc.freenode.net / #axlsx](irc://irc.freenode.net/axlsx) +**Git**: [http://github.com/randym/acts_as_xlsx](http://github.com/randym/acts_as_xlsx) +**Author**: Randy Morgan +**Copyright**: 2011 +**License**: MIT License +**Latest Version**: 2.0.0 +**Ruby Version**: 1.9.3 - 2.1.0 +**Release Date**: June 16th 2014 Synopsis -------- Acts_as_xlsx is an active record plugin for Axlsx. It makes generating excel spreadsheets from any subclass of ActiveRecord::Base as simple as a couple of lines of code. + Feature List ------------ - + **1. Mixes into active record base to provide to_xlsx **2. Can work at the end of any series of finder methods. - -**3. Can accept any set of find options + +**3. Can accept any set of find options **4. Automates localization of column heading with i18n support @@ -37,28 +38,32 @@ Feature List **9. Allows you to specify the Axlsx package to add your data to so you can create a single workbook with a sheet for each to_xlsx call. + Installing ---------- To install, use the following command: $ gem install acts_as_xlsx - + + Usage ----- ###Examples -See the Guides here: + +See the Guides here: [http://axlsx.blogspot.com/] (http://axlsx.blogspot.com/) For examples on how to use axlsx for custom styles, charts, images and more see: -[http://github.com/randym/axlsx](http://github.com/randym/axlsx) +[http://github.com/randym/axlsx](http://github.com/randym/axlsx) ###Documentation + This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use: gem install yard @@ -68,9 +73,15 @@ This gem is 100% documented with YARD, an exceptional documentation library. To ###Specs This gem has 100% coverage using Test::Unit - + + Changelog --------- +- ** June.17.14** 1.1.0 release + - Added support for ActvieRecord > 2.3.8, which removed support for ::Base#all + - Added where and order options for more recent versions of ActiveRecord + - Added skip_humanization option to allow you to output the sheet and column names directly. + - **July.27.12**: 1.0.6 release - conditionaly register XLSX mime type @@ -85,8 +96,10 @@ Changelog Please see the {file:CHANGELOG.md} document for past release information. + Copyright --------- -Acts_as_xlsx © 2011 by [Randy Morgan](mailto:digial.ipseity@gmail.com). Acts_as_xlsx is +Acts_as_xlsx © 201l-2014 by [Randy Morgan](mailto:digial.ipseity@gmail.com). Acts_as_xlsx is licensed under the MIT license. Please see the {file:LICENSE} document for more information. + diff --git a/Rakefile b/Rakefile index d893eca..4eaadc1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,15 @@ +require "bundler/setup" +require 'appraisal' require File.expand_path(File.dirname(__FILE__) + '/lib/acts_as_xlsx/version.rb') - task :build => :gendoc do system "gem build acts_as_xlsx.gemspec" end -task :gendoc do +task :gendoc do system "yardoc" end -task :test do +task :test do require 'rake/testtask' Rake::TestTask.new do |t| t.libs << 'test' @@ -21,4 +22,6 @@ task :release => :build do system "gem push acts_as_xlsx-#{Axlsx::Ar::VERSION}.gem" end -task :default => :test \ No newline at end of file +if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"] + task :default => :appraisal +end diff --git a/acts_as_xlsx.gemspec b/acts_as_xlsx.gemspec index c606276..cc0db17 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -6,10 +6,11 @@ Gem::Specification.new do |s| s.author = "Randy Morgan" s.email = 'digital.ipseity@gmail.com' s.homepage = 'https://github.com/randym/acts_as_xlsx' - s.platform = Gem::Platform::RUBY + s.platform = Gem::Platform::RUBY s.date = Time.now.strftime('%Y-%m-%d') s.summary = "ActiveRecord support for Axlsx" s.has_rdoc = 'acts_as_xlsx' + s.license = 'MIT' s.description = <<-eof acts_as_xlsx lets you turn any ActiveRecord::Base inheriting class into an excel spreadsheet. It can be added to any finder method or scope chain and can use localized column and sheet names with I18n. @@ -18,16 +19,15 @@ Gem::Specification.new do |s| s.test_files = Dir.glob("{test/**/*}") s.add_runtime_dependency 'axlsx', '>= 1.0.13' - s.add_runtime_dependency 'activerecord', '>= 2.3.9' + s.add_runtime_dependency 'activerecord', '>= 2.3.8' s.add_runtime_dependency 'i18n', '>= 0.4.1' - - s.add_development_dependency 'rake', "0.8.7" if RUBY_VERSION == "1.9.2" - s.add_development_dependency 'rake', "~> 0.9" if ["1.9.3", "1.8.7"].include?(RUBY_VERSION) + + s.add_development_dependency('appraisal') + s.add_development_dependency 'rake' s.add_development_dependency 'bundler' s.add_development_dependency 'sqlite3', "~> 1.3.5" s.add_development_dependency 'yard' s.add_development_dependency 'rdiscount' - - s.required_ruby_version = '>= 1.8.7' + s.required_ruby_version = '>= 1.9.3' s.require_path = 'lib' end diff --git a/gemfiles/2.3.gemfile b/gemfiles/2.3.gemfile new file mode 100644 index 0000000..576ca85 --- /dev/null +++ b/gemfiles/2.3.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 2.3.8" +gem "acts_as_xlsx", :path => "../" + +gemspec :path => "../" diff --git a/gemfiles/3.1.gemfile b/gemfiles/3.1.gemfile new file mode 100644 index 0000000..76e988e --- /dev/null +++ b/gemfiles/3.1.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 3.1.0" +gem "acts_as_xlsx", :path => "../" + +gemspec :path => "../" diff --git a/gemfiles/3.1.gemfile.lock b/gemfiles/3.1.gemfile.lock new file mode 100644 index 0000000..59c712b --- /dev/null +++ b/gemfiles/3.1.gemfile.lock @@ -0,0 +1,58 @@ +PATH + remote: .. + specs: + acts_as_xlsx (1.0.6) + activerecord (>= 2.1.0) + axlsx (>= 1.0.13) + i18n (>= 0.4.1) + +GEM + remote: https://rubygems.org/ + specs: + activemodel (3.1.12) + activesupport (= 3.1.12) + builder (~> 3.0.0) + i18n (~> 0.6) + activerecord (3.1.12) + activemodel (= 3.1.12) + activesupport (= 3.1.12) + arel (~> 2.2.3) + tzinfo (~> 0.3.29) + activesupport (3.1.12) + multi_json (~> 1.0) + appraisal (1.0.0) + bundler + rake + thor (>= 0.14.0) + arel (2.2.3) + axlsx (2.0.1) + htmlentities (~> 4.3.1) + nokogiri (>= 1.4.1) + rubyzip (~> 1.0.0) + builder (3.0.4) + htmlentities (4.3.2) + i18n (0.6.9) + mini_portile (0.6.0) + multi_json (1.10.1) + nokogiri (1.6.2.1) + mini_portile (= 0.6.0) + rake (10.3.2) + rdiscount (2.1.7.1) + rubyzip (1.0.0) + sqlite3 (1.3.9) + thor (0.19.1) + tzinfo (0.3.39) + yard (0.8.7.4) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 3.1.0) + acts_as_xlsx! + appraisal + bundler + rake + rdiscount + sqlite3 (~> 1.3.5) + yard diff --git a/gemfiles/4.1.gemfile b/gemfiles/4.1.gemfile new file mode 100644 index 0000000..0e93138 --- /dev/null +++ b/gemfiles/4.1.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 4.1.0" +gem "acts_as_xlsx", :path => "../" + +gemspec :path => "../" diff --git a/gemfiles/4.1.gemfile.lock b/gemfiles/4.1.gemfile.lock new file mode 100644 index 0000000..1e0d04a --- /dev/null +++ b/gemfiles/4.1.gemfile.lock @@ -0,0 +1,63 @@ +PATH + remote: .. + specs: + acts_as_xlsx (1.0.6) + activerecord (>= 2.3.8) + axlsx (>= 1.0.13) + i18n (>= 0.4.1) + +GEM + remote: https://rubygems.org/ + specs: + activemodel (4.1.1) + activesupport (= 4.1.1) + builder (~> 3.1) + activerecord (4.1.1) + activemodel (= 4.1.1) + activesupport (= 4.1.1) + arel (~> 5.0.0) + activesupport (4.1.1) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) + appraisal (1.0.0) + bundler + rake + thor (>= 0.14.0) + arel (5.0.1.20140414130214) + axlsx (2.0.1) + htmlentities (~> 4.3.1) + nokogiri (>= 1.4.1) + rubyzip (~> 1.0.0) + builder (3.2.2) + htmlentities (4.3.2) + i18n (0.6.9) + json (1.8.1) + mini_portile (0.6.0) + minitest (5.3.4) + nokogiri (1.6.2.1) + mini_portile (= 0.6.0) + rake (10.3.2) + rdiscount (2.1.7.1) + rubyzip (1.0.0) + sqlite3 (1.3.9) + thor (0.19.1) + thread_safe (0.3.4) + tzinfo (1.2.1) + thread_safe (~> 0.1) + yard (0.8.7.4) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 4.1.0) + acts_as_xlsx! + appraisal + bundler + rake + rdiscount + sqlite3 (~> 1.3.5) + yard diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 595bd56..f987b70 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -1,30 +1,30 @@ # -*- 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 # defines the class method to inject to_xlsx # @option options [Array, Symbol] columns an array of symbols defining the columns and methods to call in generating sheet data for each row. - # @option options [String] i18n (default nil) The path to search for localization. When this is specified your i18n.t will be used to determine the labels for columns. + # @option options [String, Boolean] i18n (default nil) The path to search for localization. When this is specified your i18n.t will be used to determine the labels for columns. If this option is set to true, the default activerecord.attributes scope is always applied, regardless of what is passed in to to_xlsx. # @example # class MyModel < ActiveRecord::Base - # acts_as_xlsx :columns=> [:id, :created_at, :updated_at], :i18n => 'activerecord.attributes' + # acts_as_xlsx columns: [:id, :created_at, :updated_at], i18n: 'activerecord.attributes' def acts_as_xlsx(options={}) cattr_accessor :xlsx_i18n, :xlsx_columns self.xlsx_i18n = options.delete(:i18n) || false @@ -37,58 +37,90 @@ def acts_as_xlsx(options={}) module SingletonMethods # Maps the AR class to an Axlsx package - # options are passed into AR find + # If you are using AR 3.0 or higher, where and order options are used when retrieving models. + # If you are on an earlier version, any option not listed here is passed into find(:all, options). # @param [Array, Array] columns as an array of symbols or a symbol that defines the attributes or methods to render in the sheet. # @option options [Integer] header_style to apply to the first row of field names # @option options [Array, Symbol] types 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 [String] i18n The path to i18n attributes. (usually activerecord.attributes) - # @option options [Package] package An Axlsx::Package. When this is provided the output will be added to the package as a new sheet. # @option options [String] name This will be used to name the worksheet added to the package. If it is not provided the name of the table name will be humanized when i18n is not specified or the I18n.t for the table name. + # @option options [Package] package An Axlsx::Package. When this is provided the output will be added to the package as a new sheet. + # @option options [String] name This will be used to name the worksheet added to the package. If it is not provided the name of the table name will be humanized when i18n is not specified or the I18n.t for the table name. + # @option options [Boolean] skip_humanization When true, column names will be used 'as is' in the sheet header and sheet name when she sheet name is not specified. # @see Worksheet#add_row def to_xlsx(options = {}) if self.xlsx_columns.nil? self.xlsx_columns = self.column_names.map { |c| c = c.to_sym } end - row_style = options.delete(:style) - header_style = options.delete(:header_style) || row_style - types = [options.delete(:types) || []].flatten - - i18n = options.delete(:i18n) || self.xlsx_i18n - columns = options.delete(:columns) || self.xlsx_columns - - p = options.delete(:package) || 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? - i18n = self.xlsx_i18n == true ? 'activerecord.attributes' : i18n - 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 - - 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) - end + package = options[:package] || Package.new + write_options = options.clone.merge(xlsx_style(package, options)) + sheet_name = options[:name] || xlsx_label_for(table_name, xlsx_i18n, options[:skip_humanization]) + + package.workbook.add_worksheet(name: sheet_name) do |sheet| + xlsx_write_header sheet, write_options + xlsx_write_body sheet, write_options + end + + package + end + + private + + def xlsx_i18n + self.xlsx_i18n == true ? 'activerecord.attributes' : options.delete(:i18n) || self.xlsx_i18n + end + + def xlsx_style(package, options) + row_style = package.workbook.styles.add_style(options[:style]) if options[:style] + header_style = package.workbook.styles.add_style(options[:header_style]) if options[:header_style] + {header_style: header_style, row_style: row_style} + end + + def xlsx_write_header(sheet, options={}) + namespace = xlsx_i18n ? "#{xlsx_i18n}.#{self.name.underscore}" : false + columns = options[:columns] || self.xlsx_columns + column_labels = columns.map do |column| + xlsx_label_for(column, namespace, options[:skip_humanization]) + end + sheet.add_row column_labels, :style => options[:header_style] + end + + def xlsx_write_body(sheet, options={}) + columns = options[:columns] || self.xlsx_columns + method_chains = columns.map { |column| column.to_s.split('.') } + types = [*options[:types]] + xlsx_records(options).map do |record| + row_data = method_chains.map do |chain| + chain.reduce(record) do |receiver, method| + receiver.send(method) + end + end + sheet.add_row row_data, style: options[:row_style], types: types + end + end + + def xlsx_records(options={}) + records = [*options.delete(:data)] + if records.empty? + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') + records = [*where(options.delete(:where)).order(options.delete(:order))] + else + find_options = options.select do |key, value| + %w(conditions order group limit offset joins include select from readonly lock).include?(key.to_s) end - sheet.add_row row_data, :style=>row_style, :types=>types + records = [*find(:all, find_options)] end end - p + records.compact.flatten + end + + def xlsx_label_for(key, namespace, skip_humanization) + if namespace + I18n.t("#{namespace}.#{key}") + else + skip_humanization ? key.to_s : key.to_s.humanize + end end end end @@ -96,5 +128,3 @@ def to_xlsx(options = {}) require 'active_record' ActiveRecord::Base.send :include, Axlsx::Ar - - diff --git a/test/helper.rb b/test/helper.rb index fff260e..334d54e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,36 +1,36 @@ - config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) - ActiveRecord::Base.establish_connection(config['sqlite3']) - ActiveRecord::Schema.define(:version => 0) do - begin +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) +ActiveRecord::Base.establish_connection(config['sqlite3']) +ActiveRecord::Schema.define(:version => 0) do + begin drop_table :author, :force => true drop_table :authors, :force => true drop_table :comments, :force => true drop_table :posts, :force => true - rescue - #dont really care if the tables are not dropped - end - - create_table(:authors, :force => true) do |t| - t.string :name - end + rescue + #dont really care if the tables are not dropped + end - create_table(:comments, :force => true) do |t| - t.text :content - t.integer :post_id - t.integer :author_id - t.timestamps - end + create_table(:authors, :force => true) do |t| + t.string :name + end - create_table(:posts, :force => true) do |t| - t.string :name - t.string :title - t.text :content - t.integer :votes - t.timestamps - end + create_table(:comments, :force => true) do |t| + t.text :content + t.integer :post_id + t.integer :author_id + t.timestamps + end + create_table(:posts, :force => true) do |t| + t.string :name + t.string :title + t.text :content + t.integer :votes + t.timestamps end - + +end + class Author < ActiveRecord::Base acts_as_xlsx has_many :comments @@ -46,27 +46,38 @@ class Post < ActiveRecord::Base acts_as_xlsx has_many :comments def ranking - a = Post.find(:all, :order =>"votes desc") + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') + a = Post.order("votes desc") + else + a = Post.find(:all, :order =>"votes desc") + end a.index(self) + 1 - end + end def last_comment self.comments.last.content end end - posts = [] - posts << Post.new(:name => "first post", :title => "This is the first post", :content=> "I am a very good first post!", :votes => 1) - posts << Post.new(:name => "second post", :title => "This is the second post", :content=> "I am the best post!", :votes => 7) - posts.each { |p| p.save! } +posts = [] +posts << Post.new(:name => "first post", :title => "This is the first post", :content=> "I am a very good first post!", :votes => 1) +posts << Post.new(:name => "second post", :title => "This is the second post", :content=> "I am the best post!", :votes => 7) +posts.each { |p| p.save! } - authors = [] - authors << Author.new(:name => 'bob') - authors << Author.new(:name => 'joe') +authors = [] +authors << Author.new(:name => 'bob') +authors << Author.new(:name => 'joe') - comments = [] - comments << Comment.new(:post => posts[0], :content => "wow, that was a nice post!", :author=>authors[1]) - comments << Comment.new(:content => "Are you really the best post?", :post => posts[1], :author=>authors[0]) - comments << Comment.new(:content => "Only until someone posts better!", :post => posts[1], :author=>authors[0]) - comments.each { |c| c.save } +comments = [] +comments << Comment.new(:post => posts[0], :content => "wow, that was a nice post!", :author=>authors[1]) +comments << Comment.new(:content => "Are you really the best post?", :post => posts[1], :author=>authors[0]) +comments << Comment.new(:content => "Only until someone posts better!", :post => posts[1], :author=>authors[0]) +comments.each { |c| c.save } +if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('4.0.0') + require 'minitest/autorun' + T = MiniTest::Test +else + require 'test/unit' + T = Test::Unit::TestCase +end diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index 39283ca..5dad699 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -1,11 +1,14 @@ #!/usr/bin/env ruby -w -require 'test/unit' -require "acts_as_xlsx.rb" +# require 'active_record' +puts "Using Ruby #{RUBY_VERSION}" +puts "Testing against version #{ActiveRecord::VERSION::STRING}" + +require "acts_as_xlsx.rb" require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) -class TestActsAsXlsx < Test::Unit::TestCase +class TestActsAsXlsx < T class Post < ActiveRecord::Base acts_as_xlsx :columns=>[:name, :title, :content, :votes, :ranking], :i18n => 'activerecord.attributes' @@ -15,10 +18,9 @@ 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 +class TestToXlsx < T def test_to_xlsx_with_package p = Post.to_xlsx Post.to_xlsx :package=>p, :name=>'another posts' @@ -27,7 +29,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 +41,16 @@ 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 + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') + p = Post.to_xlsx where: {title: "This is the first post"} + else + p = Post.to_xlsx conditions: {title: "This is the first post"} + end 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] @@ -63,15 +67,16 @@ def test_method_in_columns assert_equal(Post.last.ranking, sheet.rows.last.cells.last.value) end + def test_dehumanization + p = Post.to_xlsx :columns=>[:name, :votes, :content, :ranking], skip_humanization: true + sheet = p.workbook.worksheets.first + assert_equal(sheet.rows.first.cells.size, Post.xlsx_columns.size - 3) + assert_equal("name",sheet.rows.first.cells.first.value) + end def test_chained_method p = Post.to_xlsx :columns=>[:name, :votes, :content, :ranking, :'comments.last.content', :'comments.first.author.name'] sheet = p.workbook.worksheets.first 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 - -