csv_rails 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -0
- data/lib/csv_rails/active_record.rb +17 -12
- data/lib/csv_rails/array.rb +10 -4
- data/lib/csv_rails/version.rb +1 -1
- data/test/csv_rails/active_record_test.rb +7 -6
- data/test/csv_rails/array_test.rb +32 -0
- data/test/dummy/config/locales/ja.yml +3 -0
- data/test/dummy/log/test.log +16561 -0
- metadata +6 -6
data/README.rdoc
CHANGED
@@ -86,4 +86,12 @@ If you do not use :header option, header is using :fields and I18n transfer.
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
+
You also use i18n_scope option
|
90
|
+
# config/locales/ja.yml
|
91
|
+
ja:
|
92
|
+
csv:
|
93
|
+
name: なまえ
|
94
|
+
|
95
|
+
User.where("id < 1").all.to_csv(:i18n_scope => :csv) #=> "なまえ\n"
|
96
|
+
|
89
97
|
Copyright (c) 2012 yalab, released under the MIT license
|
@@ -9,21 +9,26 @@ module CsvRails
|
|
9
9
|
|
10
10
|
module ClassMethods
|
11
11
|
def to_csv(opts={})
|
12
|
-
fields =
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
header = fields.map{|f| human_attribute_name(f) }
|
12
|
+
fields = opts[:fields] || csv_fields
|
13
|
+
scope = opts.delete(:i18n_scope)
|
14
|
+
header = fields.map{|f|
|
15
|
+
if scope
|
16
|
+
I18n.t("#{scope}.#{f}", :default => human_attribute_name(f))
|
17
|
+
else
|
18
|
+
human_attribute_name(f)
|
19
|
+
end
|
20
|
+
}
|
22
21
|
all.to_csv(opts.update(:fields => fields, :header => header))
|
23
22
|
end
|
24
23
|
|
25
|
-
def
|
26
|
-
|
24
|
+
def csv_fields
|
25
|
+
if respond_to?(:attribute_names)
|
26
|
+
attribute_names
|
27
|
+
elsif self.is_a?(::ActiveRecord::Relation)
|
28
|
+
@klass.new.attribute_names
|
29
|
+
else
|
30
|
+
new.attribute_names
|
31
|
+
end
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
data/lib/csv_rails/array.rb
CHANGED
@@ -12,17 +12,23 @@ module CsvRails
|
|
12
12
|
# * <tt>:header</tt> - header
|
13
13
|
# * <tt>:without_header</tt> - total_count
|
14
14
|
# * <tt>:encoding</tt> - encoding
|
15
|
+
# * <tt>:i18n_scope</tt> - i18n scope
|
15
16
|
def to_csv(opts={})
|
16
17
|
fields = opts[:fields]
|
17
18
|
header = if opts[:header]
|
18
19
|
opts.delete(:header)
|
19
|
-
elsif (klass = first.class).respond_to?(:
|
20
|
-
klass.
|
20
|
+
elsif (klass = first.class).respond_to?(:csv_fields)
|
21
|
+
klass.csv_fields
|
21
22
|
else
|
22
|
-
|
23
|
+
scopes = ['csv_rails']
|
24
|
+
scopes << opts[:i18n_scope] if opts[:i18n_scope]
|
25
|
+
fields.map{|f|
|
26
|
+
defaults = scopes.map{|s| "#{s}.#{f}".to_sym }.push(f.to_s)
|
27
|
+
I18n.t(defaults.shift, :default => defaults)
|
28
|
+
} if fields
|
23
29
|
end
|
24
30
|
csv = CSV.generate do |_csv|
|
25
|
-
_csv << header
|
31
|
+
_csv << header if header && !opts[:without_header]
|
26
32
|
each do |element|
|
27
33
|
_csv << element.to_csv_ary(fields, opts)
|
28
34
|
end
|
data/lib/csv_rails/version.rb
CHANGED
@@ -73,12 +73,6 @@ class CsvRails::ActiveRecordTest < ActiveSupport::TestCase
|
|
73
73
|
I18n.locale = :en
|
74
74
|
end
|
75
75
|
|
76
|
-
test ".csv_header with association" do
|
77
|
-
I18n.locale = :ja
|
78
|
-
assert_equal [User.human_attribute_name(:name), Group.human_attribute_name(:name)], User.csv_header([:name, :"groups.first.name"])
|
79
|
-
I18n.locale = :en
|
80
|
-
end
|
81
|
-
|
82
76
|
test ".to_csv with association" do
|
83
77
|
I18n.locale = :ja
|
84
78
|
csv =<<-EOS.gsub(/^\s+/, '')
|
@@ -89,6 +83,13 @@ class CsvRails::ActiveRecordTest < ActiveSupport::TestCase
|
|
89
83
|
I18n.locale = :en
|
90
84
|
end
|
91
85
|
|
86
|
+
test ".to_csv with i18n option" do
|
87
|
+
I18n.locale = :ja
|
88
|
+
translated = [:id, :name].map{|f| I18n.t("csv_rails.#{f}") }.join(',')
|
89
|
+
assert_match /^#{translated}/, User.where('id < 1').to_csv(:i18n_scope => 'csv_rails')
|
90
|
+
I18n.locale = :en
|
91
|
+
end
|
92
|
+
|
92
93
|
test ".to_csv with association using as_csv" do
|
93
94
|
assert_equal @group.created_at_as_csv, User.includes(:groups).to_csv(:fields => [:"groups.first.created_at"], :without_header => true).chomp
|
94
95
|
end
|
@@ -2,13 +2,45 @@
|
|
2
2
|
require 'test_helper'
|
3
3
|
|
4
4
|
class CsvRails::ArrayTest < ActiveSupport::TestCase
|
5
|
+
setup do
|
6
|
+
I18n.locale = :ja
|
7
|
+
@csv_rails_scope = {name: "名前", age: "年齢"}
|
8
|
+
@human_scope = {sex: '性別'}
|
9
|
+
I18n.backend.store_translations(:ja, :csv_rails => @csv_rails_scope, :human => @human_scope)
|
10
|
+
end
|
11
|
+
|
12
|
+
teardown do
|
13
|
+
I18n.locale = :en
|
14
|
+
end
|
15
|
+
|
5
16
|
test ".to_csv with header option" do
|
6
17
|
header = ['foo', 'bar']
|
7
18
|
assert_equal header.join(','), [].to_csv(:header => header).chomp
|
8
19
|
end
|
9
20
|
|
21
|
+
test ".to_csv using I18n csv_rails scope and accept i18n_scope option" do
|
22
|
+
translations = @csv_rails_scope.merge(@human_scope)
|
23
|
+
fields = translations.keys
|
24
|
+
assert_equal translations.values.join(','), [].to_csv(:fields => fields, :i18n_scope => :human).chomp
|
25
|
+
end
|
26
|
+
|
27
|
+
test ".to_csv not stored i18n" do
|
28
|
+
fields = [:sum, :max]
|
29
|
+
assert_equal fields.join(','), [].to_csv(:fields => fields).chomp
|
30
|
+
end
|
31
|
+
|
10
32
|
test ".to_csv accept encoding" do
|
11
33
|
name = "名前"
|
12
34
|
assert_equal name.encode('SJIS'), [].to_csv(:header => [name], :encoding => 'SJIS').chomp
|
13
35
|
end
|
36
|
+
|
37
|
+
test ".to_csv only it includes ActiveRecord instance" do
|
38
|
+
assert_nothing_raised do
|
39
|
+
[User.create(:name => 'satomicchy')].to_csv
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
test ".to_csv using empty array" do
|
44
|
+
assert_equal "", [].to_csv
|
45
|
+
end
|
14
46
|
end
|