csv_rails 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -91,6 +91,17 @@ If you do not use :header option, header is using :fields and I18n transfer.
91
91
  end
92
92
  end
93
93
 
94
+ And you can use tsv. Both tsv and csv can accept row_sep option.
95
+
96
+ # app/controllers/user_controller.rb
97
+ def index
98
+ @users = User.all
99
+ respond_to do |format|
100
+ format.csv{ render csv: @users, :row_sep => "\r\n" }
101
+ format.tsv{ render tsv: @users, :row_sep => "\r\n" }
102
+ end
103
+ end
104
+
94
105
  You also use i18n_scope option
95
106
  # config/locales/ja.yml
96
107
  ja:
@@ -11,7 +11,9 @@ module CsvRails
11
11
  # * <tt>:without_header</tt> - total_count
12
12
  # * <tt>:encoding</tt> - encoding
13
13
  # * <tt>:i18n_scope</tt> - i18n scope
14
- def to_csv(opts={})
14
+
15
+ def separated_values(sep, opts)
16
+ row_sep = opts.delete(:row_sep) || :auto
15
17
  klass = first.class
16
18
  fields = if opts[:fields]
17
19
  opts.delete(:fields)
@@ -33,7 +35,7 @@ module CsvRails
33
35
  I18n.t(defaults.shift, :default => defaults)
34
36
  }
35
37
  end
36
- csv = CSV.generate do |_csv|
38
+ csv = CSV.generate(:col_sep => sep, :row_sep => row_sep) do |_csv|
37
39
  _csv << header if header && !opts[:without_header]
38
40
  each do |element|
39
41
  _csv << element.to_csv_ary(fields, opts)
@@ -41,5 +43,13 @@ module CsvRails
41
43
  end
42
44
  opts[:encoding] ? csv.encode(opts[:encoding]) : csv
43
45
  end
46
+
47
+ def to_csv(opts={})
48
+ separated_values(",", opts)
49
+ end
50
+
51
+ def to_tsv(opts={})
52
+ separated_values("\t", opts)
53
+ end
44
54
  end
45
55
  end
@@ -1,3 +1,3 @@
1
1
  module CsvRails
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
data/lib/csv_rails.rb CHANGED
@@ -13,8 +13,13 @@ if defined?(Mongoid)
13
13
  Mongoid::Document.send(:include, CsvRails::Mongoid)
14
14
  end
15
15
 
16
- ActionController::Renderers.add :csv do |obj, options|
17
- filename = options[:filename] || File.basename(request.path)
18
- send_data obj.to_csv(options), :type => Mime::CSV,
19
- :disposition => "attachment; filename=#{filename}.csv"
16
+ Mime::Type.register "text/tsv", :tsv
17
+
18
+ [:csv, :tsv].each do |format|
19
+ ActionController::Renderers.add format do |obj, options|
20
+ filename = options[:filename] || File.basename(request.path)
21
+ send_data obj.send("to_#{format}", options), :type => Mime.const_get(format.to_s.upcase),
22
+ :disposition => "attachment; filename=#{filename}.#{format}"
23
+ end
20
24
  end
25
+
@@ -93,4 +93,8 @@ class CsvRails::ActiveRecordTest < ActiveSupport::TestCase
93
93
  test ".to_csv with association using as_csv" do
94
94
  assert_equal @group.created_at_as_csv, User.includes(:groups).to_csv(:fields => [:"groups.first.created_at"], :without_header => true).chomp
95
95
  end
96
+
97
+ test ".to_csv with row_sep option" do
98
+ assert_match /\r\n/, User.all.to_csv(:row_sep => "\r\n")
99
+ end
96
100
  end
@@ -29,6 +29,12 @@ class CsvRails::ArrayTest < ActiveSupport::TestCase
29
29
  assert_equal fields.join(','), [].to_csv(:fields => fields).chomp
30
30
  end
31
31
 
32
+ test ".to_tsv not stored i18n" do
33
+ fields = [:sum, :max]
34
+ assert_equal fields.join("\t"), [].to_tsv(:fields => fields).chomp
35
+ end
36
+
37
+
32
38
  test ".to_csv accept encoding" do
33
39
  name = "名前"
34
40
  assert_equal name.encode('SJIS'), [].to_csv(:header => [name], :encoding => 'SJIS').chomp
@@ -49,4 +55,10 @@ class CsvRails::ArrayTest < ActiveSupport::TestCase
49
55
 
50
56
  assert_equal " type,\"\",タイトル,本文\n#{post._type},#{post._id},#{post.title},\"#{post.body}\"\n", [post].to_csv
51
57
  end
58
+
59
+ test ".to_tsv only it includes Mongoid instance" do
60
+ post = Post.create(:title => 'this is csv_rails', :body => "line\nline\nline\n")
61
+
62
+ assert_equal " type\t\"\"\tタイトル\t本文\n#{post._type}\t#{post._id}\t#{post.title}\t\"#{post.body}\"\n", [post].to_tsv
63
+ end
52
64
  end
@@ -4,6 +4,7 @@ class UsersController < ApplicationController
4
4
  respond_to do |format|
5
5
  format.html
6
6
  format.csv { render csv: @users, fields: [:id, :name, :age, :"groups.first.name"], without_header: true }
7
+ format.tsv { render tsv: @users, fields: [:id, :name, :age, :"groups.first.name"], without_header: true }
7
8
  end
8
9
  end
9
10
 
Binary file
@@ -1672,3 +1672,17 @@ Started GET "/users/sjis.csv" for 127.0.0.1 at 2012-03-28 18:56:29 +0900
1672
1672
  Rendered text template (0.0ms)
1673
1673
  Sent data (1.5ms)
1674
1674
  Completed 200 OK in 401ms (Views: 186.9ms | ActiveRecord: 18.8ms)
1675
+ MONGODB [DEBUG] Logging level is currently :debug which could negatively impact client-side performance. You should set your logging level no lower than :info in production.
1676
+ MONGODB (0ms) admin['$cmd'].find({:ismaster=>1}).limit(-1)
1677
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
1678
+  (0.4ms) select sqlite_version(*)
1679
+  (23.4ms) CREATE TABLE "groups" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "created_at" datetime, "updated_at" datetime) 
1680
+  (17.2ms) CREATE TABLE "memberships" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "group_id" integer, "created_at" datetime, "updated_at" datetime)
1681
+  (17.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "age" integer, "secret" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
1682
+  (17.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
1683
+  (0.1ms) PRAGMA index_list("schema_migrations")
1684
+  (17.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
1685
+  (0.1ms) SELECT version FROM "schema_migrations"
1686
+  (17.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20120328072740')
1687
+  (17.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20120323112247')
1688
+  (17.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20120328072253')