Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 9 commits
  • 10 files changed
  • 0 commit comments
  • 2 contributors
View
2  .gitignore
@@ -0,0 +1,2 @@
+bc-to_xls-*.gem
+test/*.log
View
55 Rakefile
@@ -0,0 +1,55 @@
+require 'rubygems'
+begin
+ require 'jeweler'
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
+ exit 1
+end
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'rcov/rcovtask'
+
+Jeweler::Tasks.new do |s|
+ s.name = "bc-to_xls"
+ s.summary = "to_xls from http://github.com/arydjmal/to_xls with i18n support"
+ s.description = s.summary
+ s.email = "info@byclosure.com"
+ s.homepage = "http://github.com/Byclosure/to_xls"
+ s.authors = ["João Jesus", "Vasco Andrade e Silva"]
+ s.add_dependency "activerecord", ['>= 1.1']
+ s.has_rdoc = true
+ s.extra_rdoc_files = [ "README.rdoc"]
+ s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
+ s.test_files = Dir['test/**/*.{yml,rb}']
+end
+Jeweler::GemcutterTasks.new
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the awesome_nested_set plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs += ['lib', 'test']
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the awesome_nested_set plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ToXls'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+namespace :test do
+ desc "just rcov minus html output"
+ Rcov::RcovTask.new(:coverage) do |t|
+ t.libs << 'test'
+ t.test_files = FileList['test/**/*_test.rb']
+ t.output_dir = 'coverage'
+ t.verbose = true
+ t.rcov_opts = %w(--exclude test,/usr/lib/ruby,/Library/Ruby --sort coverage)
+ end
+end
View
1  VERSION
@@ -0,0 +1 @@
+0.1.0
View
58 bc-to_xls.gemspec
@@ -0,0 +1,58 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE
+# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{bc-to_xls}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jo\303\243o Jesus", "Vasco Andrade e Silva"]
+ s.date = %q{2010-02-14}
+ s.description = %q{to_xls from http://github.com/arydjmal/to_xls with i18n support}
+ s.email = %q{info@byclosure.com}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ ".gitignore",
+ "MIT-LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "bc-to_xls.gemspec",
+ "lib/to_xls.rb",
+ "rails/init.rb",
+ "test/db/database.yml",
+ "test/db/schema.rb",
+ "test/test_helper.rb",
+ "test/to_xls_test.rb",
+ "test/user_model.rb"
+ ]
+ s.homepage = %q{http://github.com/Byclosure/to_xls}
+ s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{to_xls from http://github.com/arydjmal/to_xls with i18n support}
+ s.test_files = [
+ "test/db/database.yml",
+ "test/db/schema.rb",
+ "test/test_helper.rb",
+ "test/to_xls_test.rb",
+ "test/user_model.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<activerecord>, [">= 1.1"])
+ else
+ s.add_dependency(%q<activerecord>, [">= 1.1"])
+ end
+ else
+ s.add_dependency(%q<activerecord>, [">= 1.1"])
+ end
+end
View
19 lib/to_xls.rb
@@ -4,28 +4,33 @@ def to_xls(options = {})
output = '<?xml version="1.0" encoding="UTF-8"?><Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"><Worksheet ss:Name="Sheet1"><Table>'
if self.any?
-
- attributes = self.first.attributes.keys.map { |c| c.to_sym }
+ instance = self.first
+ attributes = instance.attributes.keys.sort.map { |c| c.to_sym }
if options[:only]
# the "& attributes" get rid of invalid columns
- columns = options[:only].to_a & attributes
+ columns = Array(options[:only]) & attributes
else
- columns = attributes - options[:except].to_a
+ columns = attributes - Array(options[:except])
end
-
+
columns += options[:methods].to_a
if columns.any?
unless options[:headers] == false
output << "<Row>"
- columns.each { |column| output << "<Cell><Data ss:Type=\"String\">#{column}</Data></Cell>" }
+ columns.each do |column|
+ output << "<Cell><Data ss:Type=\"String\">#{instance.class.human_attribute_name(column)}</Data></Cell>"
+ end
output << "</Row>"
end
self.each do |item|
output << "<Row>"
- columns.each { |column| output << "<Cell><Data ss:Type=\"#{item.send(column).kind_of?(Integer) ? 'Number' : 'String'}\">#{item.send(column)}</Data></Cell>" }
+ columns.each do |column|
+ value = item.send(column)
+ output << "<Cell><Data ss:Type=\"#{value.kind_of?(Integer) ? 'Number' : 'String'}\">#{value}</Data></Cell>"
+ end
output << "</Row>"
end
end
View
0  init.rb → rails/init.rb
File renamed without changes
View
18 test/db/database.yml
@@ -0,0 +1,18 @@
+sqlite3:
+ adapter: sqlite3
+ database: to_xls.sqlite3.db
+sqlite3mem:
+ adapter: sqlite3
+ database: ":memory:"
+postgresql:
+ adapter: postgresql
+ username: postgres
+ password: postgres
+ database: to_xls_test
+ min_messages: ERROR
+mysql:
+ adapter: mysql
+ host: localhost
+ username: root
+ password:
+ database: to_xls_test
View
7 test/db/schema.rb
@@ -0,0 +1,7 @@
+ActiveRecord::Schema.define(:version => 0) do
+
+ create_table :users, :force => true do |t|
+ t.column :name, :string
+ t.column :age, :integer
+ end
+end
View
27 test/test_helper.rb
@@ -0,0 +1,27 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+plugin_test_dir = File.dirname(__FILE__)
+RAILS_ROOT = plugin_test_dir
+
+require 'rubygems'
+require 'test/unit'
+require 'active_support'
+require 'multi_rails_init'
+require "test_help"
+require plugin_test_dir + '/../rails/init.rb'
+
+TestCaseClass = ActiveSupport::TestCase rescue Test::Unit::TestCase
+
+ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
+
+ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
+ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
+ActiveRecord::Migration.verbose = false
+load(File.join(plugin_test_dir, "db", "schema.rb"))
+
+Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file }
+
+class TestCaseClass #:nodoc:
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+end
View
114 test/to_xls_test.rb
@@ -1,10 +1,37 @@
-require 'test/unit'
-require 'rubygems'
-require File.dirname(__FILE__) + '/../lib/to_xls'
-require File.dirname(__FILE__) + '/user_model'
+require 'test_helper'
-class ToXlsTest < Test::Unit::TestCase
+class User < ActiveRecord::Base
+ def is_old?
+ age > 22
+ end
+end
+class ToXlsTest < TestCaseClass
+ def xls_doc(content)
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">#{content}</Workbook>"
+ end
+
+ def worksheet(name, content)
+ "<Worksheet ss:Name=\"#{name}\">#{content}</Worksheet>"
+ end
+
+ def worksheet_doc(worksheet_name, content)
+ xls_doc(worksheet(worksheet_name, content))
+ end
+
+ def cell(type, content)
+ "<Cell><Data ss:Type=\"#{type}\">#{content}</Data></Cell>"
+ end
+
+ def row(*cells)
+ "<Row>#{cells.join}</Row>"
+ end
+
+ def table(*rows)
+ "<Table>#{rows.join}</Table>"
+ end
+
+ # *****
def setup
@users = []
@users << User.new(:id => 1, :name => 'Ary', :age => 24)
@@ -12,39 +39,96 @@ def setup
end
def test_with_empty_array
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", [].to_xls )
+ assert_equal( worksheet_doc("Sheet1", table), [].to_xls )
end
def test_with_no_options
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls )
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Age"), cell("String", "Name")),
+ row(cell("Number", 24), cell("String", "Ary")),
+ row(cell("Number", 21), cell("String", "Nati")))),
+ @users.to_xls
+ )
end
def test_with_no_headers
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:headers => false) )
+ assert_equal(worksheet_doc("Sheet1", table(
+ row(cell("Number", 24), cell("String", "Ary")),
+ row(cell("Number", 21), cell("String", "Nati")))),
+ @users.to_xls(:headers => false)
+ )
end
def test_with_only
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => :name) )
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Name")),
+ row(cell("String", "Ary")),
+ row(cell("String", "Nati")))),
+ @users.to_xls(:only => :name)
+ )
end
def test_with_empty_only
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", @users.to_xls(:only => "") )
+ assert_equal( worksheet_doc("Sheet1", table), @users.to_xls(:only => "") )
end
def test_with_only_and_wrong_column_names
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => [:name, :yoyo]) )
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Name")),
+ row(cell("String", "Ary")),
+ row(cell("String", "Nati")))),
+ @users.to_xls(:only => [:name, :yoyo])
+ )
end
def test_with_except
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name]) )
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Age")),
+ row(cell("Number", 24)),
+ row(cell("Number", 21)))),
+ @users.to_xls(:except => [:id, :name])
+ )
end
def test_with_except_and_only_should_listen_to_only
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name], :only => :name) )
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Name")),
+ row(cell("String", "Ary")),
+ row(cell("String", "Nati")))),
+ @users.to_xls(:except => [:id, :name], :only => :name)
+ )
end
- def test_with_except
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell><Cell><Data ss:Type=\"String\">is_old?</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:methods => [:is_old?]) )
+ def test_with_methods
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Age"), cell("String", "Name"), cell("String", "Is old?")),
+ row(cell("Number", 24), cell("String", "Ary"), cell("String", "true")),
+ row(cell("Number", 21), cell("String", "Nati"), cell("String", "false")))),
+ @users.to_xls(:methods => [:is_old?])
+ )
+ end
+
+ def test_with_i18n
+ old_locale = I18n.locale
+ I18n.locale = "pt-PT"
+ I18n.backend.store_translations("pt-PT", {
+ "activerecord" => {
+ "attributes" => {
+ "user" => {
+ "name" => "Nome",
+ "age" => "Idade"
+ }
+ }
+ }
+ })
+ assert_equal( worksheet_doc("Sheet1", table(
+ row(cell("String", "Idade"), cell("String", "Nome")),
+ row(cell("Number", 24), cell("String", "Ary")),
+ row(cell("Number", 21), cell("String", "Nati")))),
+ @users.to_xls
+ )
+ ensure
+ I18n.locale = old_locale
end
end

No commit comments for this range

Something went wrong with that request. Please try again.