From 74c00b5054ef96ced7bd55753015164f6aaf13ab Mon Sep 17 00:00:00 2001 From: Noel Peden Date: Wed, 9 Apr 2014 14:20:10 -0700 Subject: [PATCH 01/13] Support Rails 4 --- .gitignore | 1 + .travis.yml | 2 +- acts_as_xlsx.gemspec | 4 ++++ lib/acts_as_xlsx/ar.rb | 6 +++++- 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68feb7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Gemfile.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index b78f7c3..f1ebb33 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,5 @@ rvm: - 1.9.3 notifications: - irc: "irc.freenode.org#axlsx + irc: "irc.freenode.org#axlsx" diff --git a/acts_as_xlsx.gemspec b/acts_as_xlsx.gemspec index c606276..f691ff1 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -21,6 +21,10 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'activerecord', '>= 2.3.9' s.add_runtime_dependency 'i18n', '>= 0.4.1' + if RUBY_VERSION == "2.0.0" + s.add_development_dependency 'rake' + s.add_development_dependency 'minitest' + end 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 'bundler' diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 595bd56..8e8ba0a 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -62,7 +62,11 @@ def to_xlsx(options = {}) 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)] + if Rails.version[0] >= '4' + data = options.delete(:data) || where(options[:where]).order(options[:order]).to_a + else + data = options.delete(:data) || [*find(:all, options)] + end data.compact! data.flatten! From a847e834bf3b1e380ed66c21428b72f60d9c645e Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Mon, 16 Jun 2014 17:20:50 +0900 Subject: [PATCH 02/13] basics for r2.0.0 series --- .gitignore | 2 ++ Gemfile | 4 +-- acts_as_xlsx.gemspec | 5 +-- lib/acts_as_xlsx/ar.rb | 26 +++++++------- test/helper.rb | 78 ++++++++++++++++++++--------------------- test/tc_acts_as_xlsx.rb | 18 +++------- 6 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc5547f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Gemfile.lock +acts_as_xlsx.sqlite3.db 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/acts_as_xlsx.gemspec b/acts_as_xlsx.gemspec index c606276..c0e5100 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -6,7 +6,7 @@ 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' @@ -20,7 +20,8 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'axlsx', '>= 1.0.13' s.add_runtime_dependency 'activerecord', '>= 2.3.9' s.add_runtime_dependency 'i18n', '>= 0.4.1' - + + s.add_development_dependency 'rake', ">= 10.l3.2" if ["2.0.0"].include?(RUBY_VERSION) 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 'bundler' diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 595bd56..ca1f6d6 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 @@ -61,28 +61,28 @@ 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) + 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}") } + 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) + r.send(c) end end sheet.add_row row_data, :style=>row_style, :types=>types @@ -96,5 +96,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..d79c085 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,27 @@ class Post < ActiveRecord::Base acts_as_xlsx has_many :comments def ranking - a = Post.find(:all, :order =>"votes desc") + a = Post.find(:all, :order =>"votes desc") 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 } diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index 39283ca..ab7ffc9 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -1,11 +1,11 @@ #!/usr/bin/env ruby -w -require 'test/unit' +require 'minitest/autorun' require "acts_as_xlsx.rb" require 'active_record' require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) -class TestActsAsXlsx < Test::Unit::TestCase +class TestActsAsXlsx < MiniTest::Test class Post < ActiveRecord::Base acts_as_xlsx :columns=>[:name, :title, :content, :votes, :ranking], :i18n => 'activerecord.attributes' @@ -15,10 +15,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 < MiniTest::Test def test_to_xlsx_with_package p = Post.to_xlsx Post.to_xlsx :package=>p, :name=>'another posts' @@ -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,4 @@ 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 - - From e9cffef408a7697031ea637a2c9313beb090f6c0 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Mon, 16 Jun 2014 20:28:52 +0900 Subject: [PATCH 03/13] start building in multiple AR versions support and dehumanization --- .gitignore | 1 + .travis.yml | 6 ++++ Appraisals | 14 +++++++++ README.md | 45 +++++++++++++++++---------- Rakefile | 4 ++- acts_as_xlsx.gemspec | 9 +++--- gemfiles/2.1.gemfile | 8 +++++ gemfiles/2.1.gemfile.lock | 45 +++++++++++++++++++++++++++ gemfiles/2.3.8.gemfile | 8 +++++ gemfiles/2.3.8.gemfile.lock | 48 ++++++++++++++++++++++++++++ gemfiles/3.1.gemfile | 8 +++++ gemfiles/3.1.gemfile.lock | 57 ++++++++++++++++++++++++++++++++++ gemfiles/4.1.gemfile | 8 +++++ gemfiles/4.1.gemfile.lock | 62 +++++++++++++++++++++++++++++++++++++ lib/acts_as_xlsx/ar.rb | 24 +++++++++++--- test/helper.rb | 7 +++-- test/tc_acts_as_xlsx.rb | 4 ++- 17 files changed, 329 insertions(+), 29 deletions(-) create mode 100644 Appraisals create mode 100644 gemfiles/2.1.gemfile create mode 100644 gemfiles/2.1.gemfile.lock create mode 100644 gemfiles/2.3.8.gemfile create mode 100644 gemfiles/2.3.8.gemfile.lock create mode 100644 gemfiles/3.1.gemfile create mode 100644 gemfiles/3.1.gemfile.lock create mode 100644 gemfiles/4.1.gemfile create mode 100644 gemfiles/4.1.gemfile.lock diff --git a/.gitignore b/.gitignore index 7a8bc10..c195fb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Gemfile.lock +.ruby-version acts_as_xlsx.sqlite3.db Gemfile.lock diff --git a/.travis.yml b/.travis.yml index f1ebb33..7df6790 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,12 @@ rvm: - 1.8.7 - 1.9.2 - 1.9.3 + - 2.0.0 + - 2.1.1 +gemfile: + - gemfiles/2.1.0.gemfile + - gemfiles/3.1.0.gemfile + - gemfiles/4.1.0.gemfile notifications: irc: "irc.freenode.org#axlsx" diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..8ec66cb --- /dev/null +++ b/Appraisals @@ -0,0 +1,14 @@ +appraise "2.1" do + gem "activerecord", "~> 2.1.0" + 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/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..f3afbb7 100644 --- a/Rakefile +++ b/Rakefile @@ -21,4 +21,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 cd2772b..cf5e532 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -10,6 +10,7 @@ Gem::Specification.new do |s| 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,20 +19,20 @@ 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.1.0' s.add_runtime_dependency 'i18n', '>= 0.4.1' + if RUBY_VERSION == "2.0.0" s.add_development_dependency 'rake' s.add_development_dependency 'minitest' end + s.add_development_dependency('appraisal') - 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 'bundler' s.add_development_dependency 'sqlite3', "~> 1.3.5" s.add_development_dependency 'yard' s.add_development_dependency 'rdiscount' - + s.add_development_dependency 'yard' s.required_ruby_version = '>= 1.8.7' s.require_path = 'lib' end diff --git a/gemfiles/2.1.gemfile b/gemfiles/2.1.gemfile new file mode 100644 index 0000000..1c7a968 --- /dev/null +++ b/gemfiles/2.1.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 2.1.0" +gem "acts_as_xlsx", :path => "../" + +gemspec :path => "../" diff --git a/gemfiles/2.1.gemfile.lock b/gemfiles/2.1.gemfile.lock new file mode 100644 index 0000000..f4e0fef --- /dev/null +++ b/gemfiles/2.1.gemfile.lock @@ -0,0 +1,45 @@ +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: + activerecord (2.1.2) + activesupport (= 2.1.2) + activesupport (2.1.2) + appraisal (1.0.0) + bundler + rake + thor (>= 0.14.0) + axlsx (2.0.1) + htmlentities (~> 4.3.1) + nokogiri (>= 1.4.1) + rubyzip (~> 1.0.0) + htmlentities (4.3.2) + i18n (0.6.9) + mini_portile (0.6.0) + 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) + yard (0.8.7.4) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 2.1.0) + acts_as_xlsx! + appraisal + bundler + rdiscount + sqlite3 (~> 1.3.5) + yard diff --git a/gemfiles/2.3.8.gemfile b/gemfiles/2.3.8.gemfile new file mode 100644 index 0000000..1c7a968 --- /dev/null +++ b/gemfiles/2.3.8.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 2.1.0" +gem "acts_as_xlsx", :path => "../" + +gemspec :path => "../" diff --git a/gemfiles/2.3.8.gemfile.lock b/gemfiles/2.3.8.gemfile.lock new file mode 100644 index 0000000..08c65b9 --- /dev/null +++ b/gemfiles/2.3.8.gemfile.lock @@ -0,0 +1,48 @@ +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: + activerecord (2.1.2) + activesupport (= 2.1.2) + activesupport (2.1.2) + appraisal (1.0.0) + bundler + rake + thor (>= 0.14.0) + axlsx (2.0.1) + htmlentities (~> 4.3.1) + nokogiri (>= 1.4.1) + rubyzip (~> 1.0.0) + htmlentities (4.3.2) + i18n (0.6.9) + 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) + yard (0.8.7.4) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 2.1.0) + acts_as_xlsx! + appraisal + bundler + minitest + rake + rdiscount + sqlite3 (~> 1.3.5) + yard 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..ece7fb6 --- /dev/null +++ b/gemfiles/3.1.gemfile.lock @@ -0,0 +1,57 @@ +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 + 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..8ded55c --- /dev/null +++ b/gemfiles/4.1.gemfile.lock @@ -0,0 +1,62 @@ +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 (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 + rdiscount + sqlite3 (~> 1.3.5) + yard diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index bfb0e45..3d0200f 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -43,7 +43,9 @@ module SingletonMethods # @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? @@ -61,13 +63,23 @@ 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) + sheet_name = options.delete(:name) + skip_humanization = options.delete(:skip_humanization) - if Rails.version[0] >= '4' - data = options.delete(:data) || where(options[:where]).order(options[:order]).to_a + unless sheet_name + if i18n + sheet_name = I18n.t("#{i18n}.#{table_name.underscore}") + else + sheet_name = skip_humanization ? table_name : table_name.humanize + end + end + + if Gem::Version.new(ActiveRecord::VERSION::STRING) > Gem::Version.new('2.3.8') + data = (options.delete(:data) || where(options[:where]).order(options[:order])).to_a else data = options.delete(:data) || [*find(:all, options)] end + data.compact! data.flatten! @@ -77,7 +89,9 @@ def to_xlsx(options = {}) col_labels = if i18n columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") } else - columns.map { |c| c.to_s.humanize } + columns.map do |c| + skip_humanization ? c..to_s : c.to_s.humanize + end end sheet.add_row col_labels, :style=>header_style diff --git a/test/helper.rb b/test/helper.rb index d79c085..9ab1cda 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -46,7 +46,11 @@ 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('2.3.8') + a = Post.order("votes desc") + else + a = Post.find(:all, :order =>"votes desc") + end a.index(self) + 1 end def last_comment @@ -69,4 +73,3 @@ def last_comment comments << Comment.new(:content => "Only until someone posts better!", :post => posts[1], :author=>authors[0]) comments.each { |c| c.save } - diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index ab7ffc9..5fd791d 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -3,6 +3,8 @@ require "acts_as_xlsx.rb" require 'active_record' +puts "Testing against version #{ActiveRecord::VERSION::STRING}" + require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) class TestActsAsXlsx < MiniTest::Test @@ -40,7 +42,7 @@ def test_to_xslx_vanilla end def test_to_xslx_with_provided_data - p = Post.to_xlsx :data => Post.where(:title => "This is the first post").all + p = Post.to_xlsx :data => Post.where(:title => "This is the first post") 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 From d387807d5fb3cb51ccc05641500dadf71c8c3309 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 01:10:42 +0900 Subject: [PATCH 04/13] modify version conditionals --- .gitignore | 1 + Appraisals | 4 ++-- Rakefile | 2 +- gemfiles/2.3.gemfile | 8 ++++++++ lib/acts_as_xlsx/ar.rb | 3 +-- test/helper.rb | 2 +- test/tc_acts_as_xlsx.rb | 25 ++++++++++++++++++++----- 7 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 gemfiles/2.3.gemfile diff --git a/.gitignore b/.gitignore index c195fb0..25cf7e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +gemfiles/*.lock Gemfile.lock .ruby-version acts_as_xlsx.sqlite3.db diff --git a/Appraisals b/Appraisals index 8ec66cb..b2f6a2d 100644 --- a/Appraisals +++ b/Appraisals @@ -1,5 +1,5 @@ -appraise "2.1" do - gem "activerecord", "~> 2.1.0" +appraise "2.3" do + gem "activerecord", "~> 2.3.8" gem "acts_as_xlsx", :path => "../" end diff --git a/Rakefile b/Rakefile index f3afbb7..904cc2e 100644 --- a/Rakefile +++ b/Rakefile @@ -8,7 +8,7 @@ task :gendoc do system "yardoc" end -task :test do +task :test do require 'rake/testtask' Rake::TestTask.new do |t| t.libs << 'test' 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/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 3d0200f..cdee127 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -73,8 +73,7 @@ def to_xlsx(options = {}) sheet_name = skip_humanization ? table_name : table_name.humanize end end - - if Gem::Version.new(ActiveRecord::VERSION::STRING) > Gem::Version.new('2.3.8') + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') data = (options.delete(:data) || where(options[:where]).order(options[:order])).to_a else data = options.delete(:data) || [*find(:all, options)] diff --git a/test/helper.rb b/test/helper.rb index 9ab1cda..60f934c 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -46,7 +46,7 @@ class Post < ActiveRecord::Base acts_as_xlsx has_many :comments def ranking - if Gem::Version.new(ActiveRecord::VERSION::STRING) > Gem::Version.new('2.3.8') + 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") diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index 5fd791d..7734de0 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -1,13 +1,22 @@ #!/usr/bin/env ruby -w -require 'minitest/autorun' -require "acts_as_xlsx.rb" +# require 'active_record' +if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') + require 'minitest/autorun' + T = MiniTest::Test +else + require 'test/unit' + T = Test::Unit::TestCase +end +require "acts_as_xlsx.rb" + +puts "Using Ruby #{RUBY_VERSION}" puts "Testing against version #{ActiveRecord::VERSION::STRING}" require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) -class TestActsAsXlsx < MiniTest::Test +class TestActsAsXlsx < T class Post < ActiveRecord::Base acts_as_xlsx :columns=>[:name, :title, :content, :votes, :ranking], :i18n => 'activerecord.attributes' @@ -19,7 +28,7 @@ def test_xlsx_options end end -class TestToXlsx < MiniTest::Test +class TestToXlsx < T def test_to_xlsx_with_package p = Post.to_xlsx Post.to_xlsx :package=>p, :name=>'another posts' @@ -42,7 +51,13 @@ def test_to_xslx_vanilla end def test_to_xslx_with_provided_data - p = Post.to_xlsx :data => Post.where(:title => "This is the first post") + + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') + data = Post.where(:title => "This is the first post") + else + data = Post.find(:all, conditions: {:title => "This is the first post"}) + end + p = Post.to_xlsx :data => data 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 From 1737af10c2c720113db99c8008f01f35673ddf71 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 02:09:53 +0900 Subject: [PATCH 05/13] automate all appraisal tests --- .travis.yml | 2 -- Rakefile | 5 ++++- gemfiles/2.3.8.gemfile | 8 -------- test/helper.rb | 8 ++++++++ test/tc_acts_as_xlsx.rb | 12 ++---------- 5 files changed, 14 insertions(+), 21 deletions(-) delete mode 100644 gemfiles/2.3.8.gemfile diff --git a/.travis.yml b/.travis.yml index 7df6790..2d6ea24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ bundler_args: --binstubs rvm: - - 1.8.7 - - 1.9.2 - 1.9.3 - 2.0.0 - 2.1.1 diff --git a/Rakefile b/Rakefile index 904cc2e..4fef4ed 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,7 @@ +require "bundler/setup" +require 'appraisal' +require 'bundler/gem_tasks' 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 @@ -21,6 +23,7 @@ task :release => :build do system "gem push acts_as_xlsx-#{Axlsx::Ar::VERSION}.gem" end +task :default => :test if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"] task :default => :appraisal end diff --git a/gemfiles/2.3.8.gemfile b/gemfiles/2.3.8.gemfile deleted file mode 100644 index 1c7a968..0000000 --- a/gemfiles/2.3.8.gemfile +++ /dev/null @@ -1,8 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "activerecord", "~> 2.1.0" -gem "acts_as_xlsx", :path => "../" - -gemspec :path => "../" diff --git a/test/helper.rb b/test/helper.rb index 60f934c..334d54e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -73,3 +73,11 @@ def last_comment 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 7734de0..802b4d2 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -1,19 +1,11 @@ #!/usr/bin/env ruby -w # require 'active_record' -if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') - require 'minitest/autorun' - T = MiniTest::Test -else - require 'test/unit' - T = Test::Unit::TestCase -end - -require "acts_as_xlsx.rb" - 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 < T From 001b193d7b537c4437ac3c34ab6af78b88955610 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 02:10:47 +0900 Subject: [PATCH 06/13] specify travis script --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2d6ea24..e218ce8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,8 @@ gemfile: - gemfiles/3.1.0.gemfile - gemfiles/4.1.0.gemfile +script: "bundle exec rake test" + notifications: irc: "irc.freenode.org#axlsx" From 79c81ae34d1df73d0b7be3daffcc2404f3c0861e Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 11:41:15 +0900 Subject: [PATCH 07/13] fiddle with appraisals --- .travis.yml | 14 +++++++++++--- Appraisals | 1 + Rakefile | 4 +--- acts_as_xlsx.gemspec | 9 ++------- gemfiles/3.1.gemfile.lock | 1 + gemfiles/4.1.gemfile.lock | 1 + 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index e218ce8..afea918 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,17 @@ rvm: - 2.0.0 - 2.1.1 gemfile: - - gemfiles/2.1.0.gemfile - - gemfiles/3.1.0.gemfile - - gemfiles/4.1.0.gemfile + - gemfiles/2.3.gemfile + - gemfiles/3.1.gemfile + - gemfiles/4.1.gemfile + +matrix: + exclude: + - ruby: 2.0.0 + gemfile: gemfile/2.3.0.gemfile + - ruby: 2.1.1 + gemfile: gemfile/2.3.0.gemfile + script: "bundle exec rake test" diff --git a/Appraisals b/Appraisals index b2f6a2d..591745a 100644 --- a/Appraisals +++ b/Appraisals @@ -1,3 +1,4 @@ +# must exclude Ruby over 1.9.3 appraise "2.3" do gem "activerecord", "~> 2.3.8" gem "acts_as_xlsx", :path => "../" diff --git a/Rakefile b/Rakefile index 4fef4ed..4eaadc1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,12 +1,11 @@ require "bundler/setup" require 'appraisal' -require 'bundler/gem_tasks' 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 @@ -23,7 +22,6 @@ task :release => :build do system "gem push acts_as_xlsx-#{Axlsx::Ar::VERSION}.gem" end -task :default => :test if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"] task :default => :appraisal end diff --git a/acts_as_xlsx.gemspec b/acts_as_xlsx.gemspec index cf5e532..413c4ec 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -22,17 +22,12 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'activerecord', '>= 2.1.0' s.add_runtime_dependency 'i18n', '>= 0.4.1' - if RUBY_VERSION == "2.0.0" - s.add_development_dependency 'rake' - s.add_development_dependency 'minitest' - end 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.add_development_dependency 'yard' - s.required_ruby_version = '>= 1.8.7' + s.required_ruby_version = '>= 1.9.3' s.require_path = 'lib' end diff --git a/gemfiles/3.1.gemfile.lock b/gemfiles/3.1.gemfile.lock index ece7fb6..59c712b 100644 --- a/gemfiles/3.1.gemfile.lock +++ b/gemfiles/3.1.gemfile.lock @@ -52,6 +52,7 @@ DEPENDENCIES acts_as_xlsx! appraisal bundler + rake rdiscount sqlite3 (~> 1.3.5) yard diff --git a/gemfiles/4.1.gemfile.lock b/gemfiles/4.1.gemfile.lock index 8ded55c..eef4c4d 100644 --- a/gemfiles/4.1.gemfile.lock +++ b/gemfiles/4.1.gemfile.lock @@ -57,6 +57,7 @@ DEPENDENCIES acts_as_xlsx! appraisal bundler + rake rdiscount sqlite3 (~> 1.3.5) yard From 717629e950d91de79c8cbcfedfd7849e58522abd Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 11:43:38 +0900 Subject: [PATCH 08/13] exclude invalid ruby versions and cleanup --- .travis.yml | 4 ++-- gemfiles/2.1.gemfile | 8 ------- gemfiles/2.1.gemfile.lock | 45 ---------------------------------- gemfiles/2.3.8.gemfile.lock | 48 ------------------------------------- 4 files changed, 2 insertions(+), 103 deletions(-) delete mode 100644 gemfiles/2.1.gemfile delete mode 100644 gemfiles/2.1.gemfile.lock delete mode 100644 gemfiles/2.3.8.gemfile.lock diff --git a/.travis.yml b/.travis.yml index afea918..9e062c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,9 @@ gemfile: matrix: exclude: - - ruby: 2.0.0 + - rvm: 2.0.0 gemfile: gemfile/2.3.0.gemfile - - ruby: 2.1.1 + - rvm: 2.1.1 gemfile: gemfile/2.3.0.gemfile diff --git a/gemfiles/2.1.gemfile b/gemfiles/2.1.gemfile deleted file mode 100644 index 1c7a968..0000000 --- a/gemfiles/2.1.gemfile +++ /dev/null @@ -1,8 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "activerecord", "~> 2.1.0" -gem "acts_as_xlsx", :path => "../" - -gemspec :path => "../" diff --git a/gemfiles/2.1.gemfile.lock b/gemfiles/2.1.gemfile.lock deleted file mode 100644 index f4e0fef..0000000 --- a/gemfiles/2.1.gemfile.lock +++ /dev/null @@ -1,45 +0,0 @@ -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: - activerecord (2.1.2) - activesupport (= 2.1.2) - activesupport (2.1.2) - appraisal (1.0.0) - bundler - rake - thor (>= 0.14.0) - axlsx (2.0.1) - htmlentities (~> 4.3.1) - nokogiri (>= 1.4.1) - rubyzip (~> 1.0.0) - htmlentities (4.3.2) - i18n (0.6.9) - mini_portile (0.6.0) - 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) - yard (0.8.7.4) - -PLATFORMS - ruby - -DEPENDENCIES - activerecord (~> 2.1.0) - acts_as_xlsx! - appraisal - bundler - rdiscount - sqlite3 (~> 1.3.5) - yard diff --git a/gemfiles/2.3.8.gemfile.lock b/gemfiles/2.3.8.gemfile.lock deleted file mode 100644 index 08c65b9..0000000 --- a/gemfiles/2.3.8.gemfile.lock +++ /dev/null @@ -1,48 +0,0 @@ -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: - activerecord (2.1.2) - activesupport (= 2.1.2) - activesupport (2.1.2) - appraisal (1.0.0) - bundler - rake - thor (>= 0.14.0) - axlsx (2.0.1) - htmlentities (~> 4.3.1) - nokogiri (>= 1.4.1) - rubyzip (~> 1.0.0) - htmlentities (4.3.2) - i18n (0.6.9) - 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) - yard (0.8.7.4) - -PLATFORMS - ruby - -DEPENDENCIES - activerecord (~> 2.1.0) - acts_as_xlsx! - appraisal - bundler - minitest - rake - rdiscount - sqlite3 (~> 1.3.5) - yard From 0fa8d554b2ab691d3de20e4a3c9bfcd3b90731ed Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 11:58:50 +0900 Subject: [PATCH 09/13] correct travis matrix exclusions --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e062c9..ae7d1a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,9 @@ gemfile: matrix: exclude: - rvm: 2.0.0 - gemfile: gemfile/2.3.0.gemfile + gemfile: gemfiles/2.3.0.gemfile - rvm: 2.1.1 - gemfile: gemfile/2.3.0.gemfile + gemfile: gemfiles/2.3.0.gemfile script: "bundle exec rake test" From db3731bb15a50cc776ae8937d4c1f9103aebbbd8 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 11:59:53 +0900 Subject: [PATCH 10/13] correct travis matrix exclusions again.. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ae7d1a2..b1baa85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,9 @@ gemfile: matrix: exclude: - rvm: 2.0.0 - gemfile: gemfiles/2.3.0.gemfile + gemfile: gemfiles/2.3.gemfile - rvm: 2.1.1 - gemfile: gemfiles/2.3.0.gemfile + gemfile: gemfiles/2.3.gemfile script: "bundle exec rake test" From 1b9a4b80883586ac364d5567e150e5a6ab080e89 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 17 Jun 2014 12:18:48 +0900 Subject: [PATCH 11/13] document where/order options and force ar 2.3 or higher --- acts_as_xlsx.gemspec | 2 +- lib/acts_as_xlsx/ar.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/acts_as_xlsx.gemspec b/acts_as_xlsx.gemspec index 413c4ec..cc0db17 100644 --- a/acts_as_xlsx.gemspec +++ b/acts_as_xlsx.gemspec @@ -19,7 +19,7 @@ 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.1.0' + s.add_runtime_dependency 'activerecord', '>= 2.3.8' s.add_runtime_dependency 'i18n', '>= 0.4.1' s.add_development_dependency('appraisal') diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index cdee127..2cbef36 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -37,7 +37,8 @@ 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. From 58771c0aef2fb8f20a488b4bee4f048a8746a4a4 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 18 Jun 2014 18:03:51 +0900 Subject: [PATCH 12/13] refactor via method extraction and spec new features --- gemfiles/4.1.gemfile.lock | 2 +- lib/acts_as_xlsx/ar.rb | 109 +++++++++++++++++++++----------------- test/tc_acts_as_xlsx.rb | 12 +++-- 3 files changed, 70 insertions(+), 53 deletions(-) diff --git a/gemfiles/4.1.gemfile.lock b/gemfiles/4.1.gemfile.lock index eef4c4d..1e0d04a 100644 --- a/gemfiles/4.1.gemfile.lock +++ b/gemfiles/4.1.gemfile.lock @@ -2,7 +2,7 @@ PATH remote: .. specs: acts_as_xlsx (1.0.6) - activerecord (>= 2.1.0) + activerecord (>= 2.3.8) axlsx (>= 1.0.13) i18n (>= 0.4.1) diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index 2cbef36..b363c16 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -21,10 +21,10 @@ 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 @@ -53,61 +53,74 @@ def to_xlsx(options = {}) 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 + 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]) - i18n = options.delete(:i18n) || self.xlsx_i18n - columns = options.delete(:columns) || self.xlsx_columns + package.workbook.add_worksheet(name: sheet_name) do |sheet| + xlsx_write_header sheet, write_options + xlsx_write_body sheet, write_options + end - 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) - skip_humanization = options.delete(:skip_humanization) + package + end - unless sheet_name - if i18n - sheet_name = I18n.t("#{i18n}.#{table_name.underscore}") - else - sheet_name = skip_humanization ? table_name : table_name.humanize - 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 - if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') - data = (options.delete(:data) || where(options[:where]).order(options[:order])).to_a - else - data = options.delete(:data) || [*find(:all, options)] + 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 - 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 do |c| - skip_humanization ? c..to_s : c.to_s.humanize - end - 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 + 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 diff --git a/test/tc_acts_as_xlsx.rb b/test/tc_acts_as_xlsx.rb index 802b4d2..5dad699 100644 --- a/test/tc_acts_as_xlsx.rb +++ b/test/tc_acts_as_xlsx.rb @@ -43,13 +43,11 @@ def test_to_xslx_vanilla end def test_to_xslx_with_provided_data - if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.0.0') - data = Post.where(:title => "This is the first post") + p = Post.to_xlsx where: {title: "This is the first post"} else - data = Post.find(:all, conditions: {:title => "This is the first post"}) + p = Post.to_xlsx conditions: {title: "This is the first post"} end - p = Post.to_xlsx :data => data 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 @@ -69,6 +67,12 @@ 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 From a780c6334dd19324820e258c56d7154261f3ed90 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 18 Jun 2014 18:33:53 +0900 Subject: [PATCH 13/13] fix typo --- lib/acts_as_xlsx/ar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/acts_as_xlsx/ar.rb b/lib/acts_as_xlsx/ar.rb index b363c16..f987b70 100644 --- a/lib/acts_as_xlsx/ar.rb +++ b/lib/acts_as_xlsx/ar.rb @@ -107,7 +107,7 @@ def xlsx_records(options={}) 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) + %w(conditions order group limit offset joins include select from readonly lock).include?(key.to_s) end records = [*find(:all, find_options)] end