Permalink
Browse files

Ruby 1.9 compability

  • Loading branch information...
1 parent 5ab4056 commit d715c8167f8f64c5422348d303f3c8dc3b7c2c2b @vidmantas vidmantas committed Feb 21, 2010
Showing with 43 additions and 21 deletions.
  1. +10 −5 README.rdoc
  2. +5 −10 Rakefile
  3. +12 −3 lib/csv_builder.rb
  4. +2 −0 lib/transliterating_filter.rb
  5. +3 −1 spec/controllers/csv_builder_spec.rb
  6. +11 −2 spec/spec_helper.rb
View
@@ -5,10 +5,9 @@ dynamically generated CSV files from your application.
== Requirements
-CSV Builder requires Rails v2.1 or higher.
+CSV Builder requires Rails v2.1-2.3.x for Ruby 1.8.x or Rails v2.3.6 for Ruby 1.9.x
-It also depends upon the FasterCSV gem http://fastercsv.rubyforge.org,
-which you can install with
+If you run Ruby 1.8.6/7 it also depends upon the FasterCSV gem, which you can install with
$ sudo gem install fastercsv
@@ -30,8 +29,7 @@ Then add the gem dependency in your config:
== Example
-CSV template files are suffixed with '.csv.csvbuilder', for example
-'index.csv.csvbuilder'
+CSV template files are suffixed with '.csv.csvbuilder', for example 'index.csv.csvbuilder'
Add rows to your CSV file in the template by pushing arrays of columns into the
csv object.
@@ -75,4 +73,11 @@ including a snippet like the following in your mailer method
end
+== Troubleshooting
+
+There's a known bug of encoding error in Ruby 1.9 and Rails 2.3.5
+For more details see https://rails.lighthouseapp.com/projects/8994/tickets/2188-i18n-fails-with-multibyte-strings-in-ruby-19-similar-to-2038
+This ticket is marked to be solved in Rails 2.3.6
+
+
Copyright (c) 2008 Econsultancy.com and 2009 Vidmantas Kabošis, released under the MIT license
View
@@ -3,15 +3,8 @@ require 'rake/testtask'
require 'rake/rdoctask'
desc 'Default: run unit tests.'
-task :default => :test
+task :default => :spec
-desc 'Test the csv_builder plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
desc 'Generate documentation for the csv_builder plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -30,14 +23,16 @@ rescue LoadError
gem 'rspec-rails', '>= 1.0.0'
require 'spec/rake/spectask'
rescue LoadError
- puts "[csv builder:] RSpec - or one of it's dependencies - is not available. Install it with: sudo gem install rspec-rails"
+ puts "RSpec - or one of it's dependencies - is not available. Install it with: sudo gem install rspec-rails"
end
end
if defined?(Spec)
- desc 'Test the formtastic plugin.'
+ desc 'Test the csv builder'
Spec::Rake::SpecTask.new('spec') do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ["-c"]
end
end
+
+
View
@@ -1,4 +1,13 @@
-require 'fastercsv'
+# encoding: utf-8
+
+if RUBY_VERSION.to_f >= 1.9
+ require 'csv'
+ FCSV = CSV
+else
+ require 'fastercsv'
+ FCSV = FasterCSV
+end
+
require 'iconv'
require 'transliterating_filter'
@@ -31,7 +40,7 @@ class CsvBuilder < TemplateHandler
def compile(template)
<<-EOV
begin
- output = FasterCSV.generate(@csv_options || {}) do |faster_csv|
+ output = FCSV.generate(@csv_options || {}) do |faster_csv|
csv = TransliteratingFilter.new(faster_csv, @input_encoding || 'UTF-8', @output_encoding || 'LATIN1')
#{template.source}
end
@@ -53,7 +62,7 @@ def compile(template)
output
rescue Exception => e
- RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
+ Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
raise e
end
EOV
@@ -1,3 +1,5 @@
+#encoding: utf-8
+
class TransliteratingFilter
# Transliterate into the required encoding if necessary
def initialize(faster_csv, input_encoding = 'UTF-8', output_encoding = 'LATIN1')
@@ -1,3 +1,5 @@
+#encoding: utf-8
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
class CsvBuilderReportsController < ApplicationController
@@ -60,7 +62,7 @@ def encoding
it "sets filename" do
get 'complex', :format => 'csv'
- response.headers['Content-Disposition'].should match(/attachment; filename=some_complex_filename.csv/)
+ response.headers['Content-Disposition'].should match(/filename=some_complex_filename.csv/)
end
end
end
View
@@ -1,4 +1,5 @@
# encoding: utf-8
+
require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
@@ -8,7 +9,15 @@
require 'action_controller'
require 'action_view'
require 'spec/rails'
-require 'fastercsv'
+
+if RUBY_VERSION.to_f >= 1.9
+ require 'csv'
+ FCSV = CSV
+else
+ require 'fastercsv'
+ FCSV = FasterCSV
+end
+
Spec::Runner.configure do |config|
end
@@ -24,7 +33,7 @@
]
def generate(options = {}, data = TEST_DATA)
- FasterCSV.generate(options) do |csv|
+ FCSV.generate(options) do |csv|
data.each do |row|
csv << row
end

0 comments on commit d715c81

Please sign in to comment.