kriss-gettext_i18n 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +8 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +78 -0
- data/VERSION.yml +4 -0
- data/gettext_i18n.gemspec +86 -0
- data/init.rb +1 -0
- data/lib/gettext_i18n.rb +25 -0
- data/lib/globalize/backend/gettext.rb +26 -0
- data/lib/globalize/i18n.rb +46 -0
- data/lib/rails/extend.rb +7 -0
- data/lib/ruby/string.rb +15 -0
- data/lib/ruby/symbol.rb +15 -0
- data/test/gettext_i18n_test.rb +114 -0
- data/test/locales/en.yml +2 -0
- data/test/locales/pl.yml +6 -0
- data/test/locales/root.yml +2 -0
- data/test/test_helper.rb +13 -0
- data/vendor/globalize2/LICENSE +21 -0
- data/vendor/globalize2/README.textile +202 -0
- data/vendor/globalize2/Rakefile +22 -0
- data/vendor/globalize2/generators/db_backend.rb +0 -0
- data/vendor/globalize2/generators/templates/db_backend_migration.rb +25 -0
- data/vendor/globalize2/init.rb +10 -0
- data/vendor/globalize2/lib/globalize/backend/chain.rb +102 -0
- data/vendor/globalize2/lib/globalize/backend/pluralizing.rb +37 -0
- data/vendor/globalize2/lib/globalize/backend/static.rb +59 -0
- data/vendor/globalize2/lib/globalize/i18n/missing_translations_log_handler.rb +41 -0
- data/vendor/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb +27 -0
- data/vendor/globalize2/lib/globalize/load_path.rb +63 -0
- data/vendor/globalize2/lib/globalize/locale/fallbacks.rb +63 -0
- data/vendor/globalize2/lib/globalize/locale/language_tag.rb +81 -0
- data/vendor/globalize2/lib/globalize/model/active_record/adapter.rb +96 -0
- data/vendor/globalize2/lib/globalize/model/active_record/translated.rb +161 -0
- data/vendor/globalize2/lib/globalize/model/active_record.rb +38 -0
- data/vendor/globalize2/lib/globalize/translation.rb +32 -0
- data/vendor/globalize2/lib/locale/root.yml +3 -0
- data/vendor/globalize2/lib/rails_edge_load_path_patch.rb +40 -0
- data/vendor/globalize2/notes.textile +51 -0
- data/vendor/globalize2/test/backends/chained_test.rb +175 -0
- data/vendor/globalize2/test/backends/pluralizing_test.rb +63 -0
- data/vendor/globalize2/test/backends/static_test.rb +143 -0
- data/vendor/globalize2/test/data/locale/all.yml +2 -0
- data/vendor/globalize2/test/data/locale/de-DE.yml +2 -0
- data/vendor/globalize2/test/data/locale/en-US/module.yml +2 -0
- data/vendor/globalize2/test/data/locale/en-US.yml +2 -0
- data/vendor/globalize2/test/data/locale/fi-FI/module.yml +2 -0
- data/vendor/globalize2/test/data/locale/root.yml +0 -0
- data/vendor/globalize2/test/data/no_globalize_schema.rb +11 -0
- data/vendor/globalize2/test/data/post.rb +24 -0
- data/vendor/globalize2/test/data/schema.rb +39 -0
- data/vendor/globalize2/test/i18n/missing_translations_test.rb +36 -0
- data/vendor/globalize2/test/load_path_test.rb +49 -0
- data/vendor/globalize2/test/locale/fallbacks_test.rb +154 -0
- data/vendor/globalize2/test/locale/language_tag_test.rb +130 -0
- data/vendor/globalize2/test/model/active_record/migration_test.rb +78 -0
- data/vendor/globalize2/test/model/active_record/sti_translated_test.rb +75 -0
- data/vendor/globalize2/test/model/active_record/translated_test.rb +458 -0
- data/vendor/globalize2/test/test_helper.rb +36 -0
- data/vendor/globalize2/test/translation_test.rb +54 -0
- metadata +121 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
|
2
|
+
require 'active_record'
|
3
|
+
require 'globalize/model/active_record'
|
4
|
+
|
5
|
+
# Hook up model translation
|
6
|
+
ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
|
7
|
+
|
8
|
+
# Load Post model
|
9
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
|
10
|
+
|
11
|
+
class MigrationTest < ActiveSupport::TestCase
|
12
|
+
def setup
|
13
|
+
reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb'))
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'globalize table added' do
|
17
|
+
assert !Post.connection.table_exists?( :post_translations )
|
18
|
+
assert !Post.connection.index_exists?( :post_translations, :post_id )
|
19
|
+
Post.create_translation_table! :subject => :string, :content => :text
|
20
|
+
assert Post.connection.table_exists?( :post_translations )
|
21
|
+
assert Post.connection.index_exists?( :post_translations, :post_id )
|
22
|
+
columns = Post.connection.columns( :post_translations )
|
23
|
+
assert locale = columns.detect {|c| c.name == 'locale' }
|
24
|
+
assert_equal :string, locale.type
|
25
|
+
assert subject = columns.detect {|c| c.name == 'subject' }
|
26
|
+
assert_equal :string, subject.type
|
27
|
+
assert content = columns.detect {|c| c.name == 'content' }
|
28
|
+
assert_equal :text, content.type
|
29
|
+
assert post_id = columns.detect {|c| c.name == 'post_id' }
|
30
|
+
assert_equal :integer, post_id.type
|
31
|
+
assert created_at = columns.detect {|c| c.name == 'created_at' }
|
32
|
+
assert_equal :datetime, created_at.type
|
33
|
+
assert updated_at = columns.detect {|c| c.name == 'updated_at' }
|
34
|
+
assert_equal :datetime, updated_at.type
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'globalize table dropped' do
|
38
|
+
assert !Post.connection.table_exists?( :post_translations )
|
39
|
+
assert !Post.connection.index_exists?( :post_translations, :post_id )
|
40
|
+
Post.create_translation_table! :subject => :string, :content => :text
|
41
|
+
assert Post.connection.table_exists?( :post_translations )
|
42
|
+
assert Post.connection.index_exists?( :post_translations, :post_id )
|
43
|
+
Post.drop_translation_table!
|
44
|
+
assert !Post.connection.table_exists?( :post_translations )
|
45
|
+
assert !Post.connection.index_exists?( :post_translations, :post_id )
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'exception on untranslated field inputs' do
|
49
|
+
assert_raise Globalize::Model::UntranslatedMigrationField do
|
50
|
+
Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'exception on missing field inputs' do
|
55
|
+
assert_raise Globalize::Model::MigrationMissingTranslatedField do
|
56
|
+
Post.create_translation_table! :content => :text
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test 'exception on bad input type' do
|
61
|
+
assert_raise Globalize::Model::BadMigrationFieldType do
|
62
|
+
Post.create_translation_table! :subject => :string, :content => :integer
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'create_translation_table! should not be called on non-translated models' do
|
67
|
+
assert_raise NoMethodError do
|
68
|
+
Blog.create_translation_table! :name => :string
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
test 'drop_translation_table! should not be called on non-translated models' do
|
73
|
+
assert_raise NoMethodError do
|
74
|
+
Blog.drop_translation_table!
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
|
2
|
+
require 'active_record'
|
3
|
+
require 'globalize/model/active_record'
|
4
|
+
|
5
|
+
# Hook up model translation
|
6
|
+
ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
|
7
|
+
|
8
|
+
# Load Post model
|
9
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
|
10
|
+
|
11
|
+
class StiTranslatedTest < ActiveSupport::TestCase
|
12
|
+
def setup
|
13
|
+
I18n.locale = :'en-US'
|
14
|
+
I18n.fallbacks.clear
|
15
|
+
reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
I18n.fallbacks.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
test "works with simple dynamic finders" do
|
23
|
+
foo = Child.create :content => 'foo'
|
24
|
+
Child.create :content => 'bar'
|
25
|
+
child = Child.find_by_content('foo')
|
26
|
+
assert_equal foo, child
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'change attribute on globalized model' do
|
30
|
+
child = Child.create :content => 'foo'
|
31
|
+
assert_equal [], child.changed
|
32
|
+
child.content = 'bar'
|
33
|
+
assert_equal [ 'content' ], child.changed
|
34
|
+
child.content = 'baz'
|
35
|
+
assert_member 'content', child.changed
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'change attribute on globalized model after locale switching' do
|
39
|
+
child = Child.create :content => 'foo'
|
40
|
+
assert_equal [], child.changed
|
41
|
+
child.content = 'bar'
|
42
|
+
I18n.locale = :de
|
43
|
+
assert_equal [ 'content' ], child.changed
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'fallbacks with lots of locale switching' do
|
47
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
48
|
+
child = Child.create :content => 'foo'
|
49
|
+
|
50
|
+
I18n.locale = :'de-DE'
|
51
|
+
assert_equal 'foo', child.content
|
52
|
+
|
53
|
+
I18n.locale = :'en-US'
|
54
|
+
child.update_attribute :content, 'bar'
|
55
|
+
|
56
|
+
I18n.locale = :'de-DE'
|
57
|
+
assert_equal 'bar', child.content
|
58
|
+
end
|
59
|
+
|
60
|
+
test "saves all locales, even after locale switching" do
|
61
|
+
child = Child.new :content => 'foo'
|
62
|
+
I18n.locale = 'de-DE'
|
63
|
+
child.content = 'bar'
|
64
|
+
I18n.locale = 'he-IL'
|
65
|
+
child.content = 'baz'
|
66
|
+
child.save
|
67
|
+
I18n.locale = 'en-US'
|
68
|
+
child = Child.first
|
69
|
+
assert_equal 'foo', child.content
|
70
|
+
I18n.locale = 'de-DE'
|
71
|
+
assert_equal 'bar', child.content
|
72
|
+
I18n.locale = 'he-IL'
|
73
|
+
assert_equal 'baz', child.content
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,458 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
|
2
|
+
require 'active_record'
|
3
|
+
require 'globalize/model/active_record'
|
4
|
+
|
5
|
+
# Hook up model translation
|
6
|
+
ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
|
7
|
+
|
8
|
+
# Load Post model
|
9
|
+
require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
|
10
|
+
|
11
|
+
class TranslatedTest < ActiveSupport::TestCase
|
12
|
+
def setup
|
13
|
+
I18n.locale = :'en-US'
|
14
|
+
I18n.fallbacks.clear
|
15
|
+
reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
|
16
|
+
ActiveRecord::Base.locale = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
I18n.fallbacks.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
test "modifiying translated fields" do
|
24
|
+
post = Post.create :subject => 'foo'
|
25
|
+
assert_equal 'foo', post.subject
|
26
|
+
post.subject = 'bar'
|
27
|
+
assert_equal 'bar', post.subject
|
28
|
+
end
|
29
|
+
|
30
|
+
test "modifiying translated fields while switching locales" do
|
31
|
+
post = Post.create :subject => 'foo'
|
32
|
+
assert_equal 'foo', post.subject
|
33
|
+
I18n.locale = :'de-DE'
|
34
|
+
post.subject = 'bar'
|
35
|
+
assert_equal 'bar', post.subject
|
36
|
+
I18n.locale = :'en-US'
|
37
|
+
assert_equal 'foo', post.subject
|
38
|
+
I18n.locale = :'de-DE'
|
39
|
+
post.subject = 'bar'
|
40
|
+
end
|
41
|
+
|
42
|
+
test "has post_translations" do
|
43
|
+
post = Post.create
|
44
|
+
assert_nothing_raised { post.globalize_translations }
|
45
|
+
end
|
46
|
+
|
47
|
+
test "has German post_translations" do
|
48
|
+
I18n.locale = :de
|
49
|
+
post = Post.create :subject => 'foo'
|
50
|
+
assert_equal 1, post.globalize_translations.size
|
51
|
+
I18n.locale = :en
|
52
|
+
assert_equal 1, post.globalize_translations.size
|
53
|
+
end
|
54
|
+
|
55
|
+
test "returns the value passed to :subject" do
|
56
|
+
post = Post.new
|
57
|
+
assert_equal 'foo', (post.subject = 'foo')
|
58
|
+
end
|
59
|
+
|
60
|
+
test "translates subject and content into en-US" do
|
61
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
62
|
+
assert_equal 'foo', post.subject
|
63
|
+
assert_equal 'bar', post.content
|
64
|
+
assert post.save
|
65
|
+
post.reload
|
66
|
+
assert_equal 'foo', post.subject
|
67
|
+
assert_equal 'bar', post.content
|
68
|
+
end
|
69
|
+
|
70
|
+
test "finds a German post" do
|
71
|
+
post = Post.create :subject => 'foo (en)', :content => 'bar'
|
72
|
+
I18n.locale = 'de-DE'
|
73
|
+
post = Post.first
|
74
|
+
post.subject = 'baz (de)'
|
75
|
+
post.save
|
76
|
+
assert_equal 'baz (de)', Post.first.subject
|
77
|
+
I18n.locale = :'en-US'
|
78
|
+
assert_equal 'foo (en)', Post.first.subject
|
79
|
+
end
|
80
|
+
|
81
|
+
test "saves an English post and loads test correctly" do
|
82
|
+
assert_nil Post.first
|
83
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
84
|
+
assert post.save
|
85
|
+
post = Post.first
|
86
|
+
assert_equal 'foo', post.subject
|
87
|
+
assert_equal 'bar', post.content
|
88
|
+
end
|
89
|
+
|
90
|
+
test "updates an attribute" do
|
91
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
92
|
+
post.update_attribute :subject, 'baz'
|
93
|
+
assert_equal 'baz', Post.first.subject
|
94
|
+
end
|
95
|
+
|
96
|
+
test "update_attributes failure" do
|
97
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
98
|
+
assert !post.update_attributes( { :subject => '' } )
|
99
|
+
assert_nil post.reload.attributes['subject']
|
100
|
+
assert_equal 'foo', post.subject
|
101
|
+
end
|
102
|
+
|
103
|
+
test "validates presence of :subject" do
|
104
|
+
post = Post.new
|
105
|
+
assert !post.save
|
106
|
+
|
107
|
+
post = Post.new :subject => 'foo'
|
108
|
+
assert post.save
|
109
|
+
end
|
110
|
+
|
111
|
+
test "returns the value for the correct locale, after locale switching" do
|
112
|
+
post = Post.create :subject => 'foo'
|
113
|
+
I18n.locale = 'de-DE'
|
114
|
+
post.subject = 'bar'
|
115
|
+
post.save
|
116
|
+
I18n.locale = 'en-US'
|
117
|
+
post = Post.first
|
118
|
+
assert_equal 'foo', post.subject
|
119
|
+
I18n.locale = 'de-DE'
|
120
|
+
assert_equal 'bar', post.subject
|
121
|
+
end
|
122
|
+
|
123
|
+
test "keeping one field in new locale when other field is changed" do
|
124
|
+
I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
|
125
|
+
post = Post.create :subject => 'foo'
|
126
|
+
I18n.locale = 'de-DE'
|
127
|
+
post.content = 'bar'
|
128
|
+
assert_equal 'foo', post.subject
|
129
|
+
end
|
130
|
+
|
131
|
+
test "modifying non-required field in a new locale" do
|
132
|
+
I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
|
133
|
+
post = Post.create :subject => 'foo'
|
134
|
+
I18n.locale = 'de-DE'
|
135
|
+
post.content = 'bar'
|
136
|
+
assert post.save
|
137
|
+
end
|
138
|
+
|
139
|
+
test "returns the value for the correct locale, after locale switching, without saving" do
|
140
|
+
post = Post.create :subject => 'foo'
|
141
|
+
I18n.locale = 'de-DE'
|
142
|
+
post.subject = 'bar'
|
143
|
+
I18n.locale = 'en-US'
|
144
|
+
assert_equal 'foo', post.subject
|
145
|
+
I18n.locale = 'de-DE'
|
146
|
+
assert_equal 'bar', post.subject
|
147
|
+
end
|
148
|
+
|
149
|
+
test "saves all locales, even after locale switching" do
|
150
|
+
post = Post.new :subject => 'foo'
|
151
|
+
I18n.locale = 'de-DE'
|
152
|
+
post.subject = 'bar'
|
153
|
+
I18n.locale = 'he-IL'
|
154
|
+
post.subject = 'baz'
|
155
|
+
post.save
|
156
|
+
I18n.locale = 'en-US'
|
157
|
+
post = Post.first
|
158
|
+
assert_equal 'foo', post.subject
|
159
|
+
I18n.locale = 'de-DE'
|
160
|
+
assert_equal 'bar', post.subject
|
161
|
+
I18n.locale = 'he-IL'
|
162
|
+
assert_equal 'baz', post.subject
|
163
|
+
end
|
164
|
+
|
165
|
+
test "resolves a simple fallback" do
|
166
|
+
I18n.locale = 'de-DE'
|
167
|
+
post = Post.create :subject => 'foo'
|
168
|
+
I18n.locale = 'de'
|
169
|
+
post.subject = 'baz'
|
170
|
+
post.content = 'bar'
|
171
|
+
post.save
|
172
|
+
I18n.locale = 'de-DE'
|
173
|
+
assert_equal 'foo', post.subject
|
174
|
+
assert_equal 'bar', post.content
|
175
|
+
end
|
176
|
+
|
177
|
+
test "resolves a simple fallback without reloading" do
|
178
|
+
I18n.locale = 'de-DE'
|
179
|
+
post = Post.new :subject => 'foo'
|
180
|
+
I18n.locale = 'de'
|
181
|
+
post.subject = 'baz'
|
182
|
+
post.content = 'bar'
|
183
|
+
I18n.locale = 'de-DE'
|
184
|
+
assert_equal 'foo', post.subject
|
185
|
+
assert_equal 'bar', post.content
|
186
|
+
end
|
187
|
+
|
188
|
+
test "resolves a complex fallback without reloading" do
|
189
|
+
I18n.fallbacks.map 'de' => %w(en he)
|
190
|
+
I18n.locale = 'de'
|
191
|
+
post = Post.new
|
192
|
+
I18n.locale = 'en'
|
193
|
+
post.subject = 'foo'
|
194
|
+
I18n.locale = 'he'
|
195
|
+
post.subject = 'baz'
|
196
|
+
post.content = 'bar'
|
197
|
+
I18n.locale = 'de'
|
198
|
+
assert_equal 'foo', post.subject
|
199
|
+
assert_equal 'bar', post.content
|
200
|
+
end
|
201
|
+
|
202
|
+
test "returns nil if no translations are found" do
|
203
|
+
post = Post.new :subject => 'foo'
|
204
|
+
assert_equal 'foo', post.subject
|
205
|
+
assert_nil post.content
|
206
|
+
end
|
207
|
+
|
208
|
+
test "returns nil if no translations are found; reloaded" do
|
209
|
+
post = Post.create :subject => 'foo'
|
210
|
+
post = Post.first
|
211
|
+
assert_equal 'foo', post.subject
|
212
|
+
assert_nil post.content
|
213
|
+
end
|
214
|
+
|
215
|
+
test "works with associations" do
|
216
|
+
blog = Blog.create
|
217
|
+
post1 = blog.posts.create :subject => 'foo'
|
218
|
+
I18n.locale = 'de-DE'
|
219
|
+
post2 = blog.posts.create :subject => 'bar'
|
220
|
+
assert_equal 2, blog.posts.size
|
221
|
+
I18n.locale = 'en-US'
|
222
|
+
assert_equal 'foo', blog.posts.first.subject
|
223
|
+
assert_nil blog.posts.last.subject
|
224
|
+
I18n.locale = 'de-DE'
|
225
|
+
assert_equal 'bar', blog.posts.last.subject
|
226
|
+
end
|
227
|
+
|
228
|
+
test "works with simple dynamic finders" do
|
229
|
+
foo = Post.create :subject => 'foo'
|
230
|
+
Post.create :subject => 'bar'
|
231
|
+
post = Post.find_by_subject('foo')
|
232
|
+
assert_equal foo, post
|
233
|
+
end
|
234
|
+
|
235
|
+
test 'change attribute on globalized model' do
|
236
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
237
|
+
assert_equal [], post.changed
|
238
|
+
post.subject = 'baz'
|
239
|
+
assert_equal [ 'subject' ], post.changed
|
240
|
+
post.content = 'quux'
|
241
|
+
assert_member 'subject', post.changed
|
242
|
+
assert_member 'content', post.changed
|
243
|
+
end
|
244
|
+
|
245
|
+
test 'change attribute on globalized model after locale switching' do
|
246
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
247
|
+
assert_equal [], post.changed
|
248
|
+
post.subject = 'baz'
|
249
|
+
I18n.locale = :de
|
250
|
+
assert_equal [ 'subject' ], post.changed
|
251
|
+
end
|
252
|
+
|
253
|
+
test 'fallbacks with lots of locale switching' do
|
254
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
255
|
+
post = Post.create :subject => 'foo'
|
256
|
+
|
257
|
+
I18n.locale = :'de-DE'
|
258
|
+
assert_equal 'foo', post.subject
|
259
|
+
|
260
|
+
I18n.locale = :'en-US'
|
261
|
+
post.update_attribute :subject, 'bar'
|
262
|
+
|
263
|
+
I18n.locale = :'de-DE'
|
264
|
+
assert_equal 'bar', post.subject
|
265
|
+
end
|
266
|
+
|
267
|
+
test 'reload' do
|
268
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
269
|
+
post.subject = 'baz'
|
270
|
+
assert_equal 'foo', post.reload.subject
|
271
|
+
end
|
272
|
+
|
273
|
+
test 'complex writing and stashing' do
|
274
|
+
post = Post.create :subject => 'foo', :content => 'bar'
|
275
|
+
post.subject = nil
|
276
|
+
assert_nil post.subject
|
277
|
+
assert !post.valid?
|
278
|
+
end
|
279
|
+
|
280
|
+
test 'translated class locale setting' do
|
281
|
+
assert ActiveRecord::Base.respond_to?(:locale)
|
282
|
+
assert_equal :'en-US', I18n.locale
|
283
|
+
assert_equal :'en-US', ActiveRecord::Base.locale
|
284
|
+
I18n.locale = :de
|
285
|
+
assert_equal :de, I18n.locale
|
286
|
+
assert_equal :de, ActiveRecord::Base.locale
|
287
|
+
ActiveRecord::Base.locale = :es
|
288
|
+
assert_equal :de, I18n.locale
|
289
|
+
assert_equal :es, ActiveRecord::Base.locale
|
290
|
+
I18n.locale = :fr
|
291
|
+
assert_equal :fr, I18n.locale
|
292
|
+
assert_equal :es, ActiveRecord::Base.locale
|
293
|
+
end
|
294
|
+
|
295
|
+
test "untranslated class responds to locale" do
|
296
|
+
assert Blog.respond_to?(:locale)
|
297
|
+
end
|
298
|
+
|
299
|
+
test "to ensure locales in different classes are the same" do
|
300
|
+
ActiveRecord::Base.locale = :de
|
301
|
+
assert_equal :de, ActiveRecord::Base.locale
|
302
|
+
assert_equal :de, Parent.locale
|
303
|
+
Parent.locale = :es
|
304
|
+
assert_equal :es, ActiveRecord::Base.locale
|
305
|
+
assert_equal :es, Parent.locale
|
306
|
+
end
|
307
|
+
|
308
|
+
test "attribute saving goes by content locale and not global locale" do
|
309
|
+
ActiveRecord::Base.locale = :de
|
310
|
+
assert_equal :'en-US', I18n.locale
|
311
|
+
Post.create :subject => 'foo'
|
312
|
+
assert_equal :de, Post.first.globalize_translations.first.locale
|
313
|
+
end
|
314
|
+
|
315
|
+
test "attribute loading goes by content locale and not global locale" do
|
316
|
+
post = Post.create :subject => 'foo'
|
317
|
+
assert_equal :'en-US', ActiveRecord::Base.locale
|
318
|
+
ActiveRecord::Base.locale = :de
|
319
|
+
assert_equal :'en-US', I18n.locale
|
320
|
+
post.update_attribute :subject, 'foo [de]'
|
321
|
+
assert_equal 'foo [de]', Post.first.subject
|
322
|
+
ActiveRecord::Base.locale = :'en-US'
|
323
|
+
assert_equal 'foo', Post.first.subject
|
324
|
+
end
|
325
|
+
|
326
|
+
test "access content locale before setting" do
|
327
|
+
Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)"
|
328
|
+
assert_nothing_raised { ActiveRecord::Base.locale }
|
329
|
+
end
|
330
|
+
|
331
|
+
test "translated_locales" do
|
332
|
+
Post.locale = :de
|
333
|
+
post = Post.create :subject => 'foo'
|
334
|
+
Post.locale = :es
|
335
|
+
post.update_attribute :subject, 'bar'
|
336
|
+
Post.locale = :fr
|
337
|
+
post.update_attribute :subject, 'baz'
|
338
|
+
assert_equal [ :de, :es, :fr ], post.translated_locales
|
339
|
+
assert_equal [ :de, :es, :fr ], Post.first.translated_locales
|
340
|
+
end
|
341
|
+
|
342
|
+
test "including globalize_translations" do
|
343
|
+
I18n.locale = :de
|
344
|
+
Post.create :subject => "Foo1", :content => "Bar1"
|
345
|
+
Post.create :subject => "Foo2", :content => "Bar2"
|
346
|
+
|
347
|
+
class << Post
|
348
|
+
def tranlsations_included
|
349
|
+
self.all(:include => :globalize_translations)
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
default = Post.all.map {|x| [x.subject, x.content]}
|
354
|
+
with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
|
355
|
+
assert_equal default, with_include
|
356
|
+
end
|
357
|
+
|
358
|
+
test "setting multiple translations at once with options hash" do
|
359
|
+
Post.locale = :de
|
360
|
+
post = Post.create :subject => "foo1", :content => "foo1"
|
361
|
+
Post.locale = :en
|
362
|
+
post.update_attributes( :subject => "bar1", :content => "bar1" )
|
363
|
+
|
364
|
+
options = { :de => {:subject => "foo2", :content => "foo2"},
|
365
|
+
:en => {:subject => "bar2", :content => "bar2"} }
|
366
|
+
post.set_translations options
|
367
|
+
post.reload
|
368
|
+
|
369
|
+
assert ["bar2", "bar2"], [post.subject, post.content]
|
370
|
+
Post.locale = :de
|
371
|
+
assert ["foo2", "foo2"], [post.subject, post.content]
|
372
|
+
end
|
373
|
+
|
374
|
+
test "setting only one translation with set_translations" do
|
375
|
+
Post.locale = :de
|
376
|
+
post = Post.create :subject => "foo1", :content => "foo1"
|
377
|
+
Post.locale = :en
|
378
|
+
post.update_attributes( :subject => "bar1", :content => "bar1" )
|
379
|
+
|
380
|
+
options = { :en => {:subject => "bar2", :content => "bar2"} }
|
381
|
+
post.set_translations options
|
382
|
+
post.reload
|
383
|
+
|
384
|
+
assert ["bar2", "bar2"], [post.subject, post.content]
|
385
|
+
Post.locale = :de
|
386
|
+
assert ["foo1", "foo1"], [post.subject, post.content]
|
387
|
+
end
|
388
|
+
|
389
|
+
test "setting only selected attributes with set_translations" do
|
390
|
+
Post.locale = :de
|
391
|
+
post = Post.create :subject => "foo1", :content => "foo1"
|
392
|
+
Post.locale = :en
|
393
|
+
post.update_attributes( :subject => "bar1", :content => "bar1" )
|
394
|
+
|
395
|
+
options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
|
396
|
+
post.set_translations options
|
397
|
+
post.reload
|
398
|
+
|
399
|
+
assert ["bar2", "bar1"], [post.subject, post.content]
|
400
|
+
Post.locale = :de
|
401
|
+
assert ["foo1", "foo2"], [post.subject, post.content]
|
402
|
+
end
|
403
|
+
|
404
|
+
test "setting invalid attributes raises ArgumentError" do
|
405
|
+
Post.locale = :de
|
406
|
+
post = Post.create :subject => "foo1", :content => "foo1"
|
407
|
+
Post.locale = :en
|
408
|
+
post.update_attributes( :subject => "bar1", :content => "bar1" )
|
409
|
+
|
410
|
+
options = { :de => {:fake => "foo2"} }
|
411
|
+
exception = assert_raise(ActiveRecord::UnknownAttributeError) do
|
412
|
+
post.set_translations options
|
413
|
+
end
|
414
|
+
assert_equal "unknown attribute: fake", exception.message
|
415
|
+
end
|
416
|
+
|
417
|
+
test "reload accepting find options" do
|
418
|
+
p = Post.create :subject => "Foo", :content => "Bar"
|
419
|
+
assert p.reload(:readonly => true, :lock => true)
|
420
|
+
assert_raise(ArgumentError) { p.reload(:foo => :bar) }
|
421
|
+
end
|
422
|
+
|
423
|
+
test "dependent destroy of translation" do
|
424
|
+
p = Post.create :subject => "Foo", :content => "Bar"
|
425
|
+
assert_equal 1, PostTranslation.count
|
426
|
+
p.destroy
|
427
|
+
assert_equal 0, PostTranslation.count
|
428
|
+
end
|
429
|
+
|
430
|
+
test "translating subclass of untranslated comment model" do
|
431
|
+
translated_comment = TranslatedComment.create(:post => @post)
|
432
|
+
assert_nothing_raised { translated_comment.globalize_translations }
|
433
|
+
end
|
434
|
+
|
435
|
+
test "modifiying translated comments works as expected" do
|
436
|
+
I18n.locale = :en
|
437
|
+
translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
|
438
|
+
assert_equal 'foo', translated_comment.content
|
439
|
+
|
440
|
+
I18n.locale = :de
|
441
|
+
translated_comment.content = 'bar'
|
442
|
+
assert translated_comment.save
|
443
|
+
assert_equal 'bar', translated_comment.content
|
444
|
+
|
445
|
+
I18n.locale = :en
|
446
|
+
assert_equal 'foo', translated_comment.content
|
447
|
+
|
448
|
+
assert_equal 2, translated_comment.globalize_translations.size
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
# TODO should validate_presence_of take fallbacks into account? maybe we need
|
453
|
+
# an extra validation call, or more options for validate_presence_of.
|
454
|
+
# TODO error checking for fields that exist in main table, don't exist in
|
455
|
+
# proxy table, aren't strings or text
|
456
|
+
#
|
457
|
+
# TODO allow finding by translated attributes in conditions?
|
458
|
+
# TODO generate advanced dynamic finders?
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'mocha'
|
6
|
+
|
7
|
+
$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
|
8
|
+
|
9
|
+
class ActiveSupport::TestCase
|
10
|
+
def reset_db!( schema_path )
|
11
|
+
::ActiveRecord::Migration.verbose = false # Quiet down the migration engine
|
12
|
+
::ActiveRecord::Base.establish_connection({
|
13
|
+
:adapter => 'sqlite3',
|
14
|
+
:dbfile => ':memory:'
|
15
|
+
})
|
16
|
+
::ActiveRecord::Base.silence do
|
17
|
+
load schema_path
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def assert_member(item, arr)
|
22
|
+
assert_block "Item #{item} is not in array #{arr}" do
|
23
|
+
arr.member? item
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ActiveRecord
|
29
|
+
module ConnectionAdapters
|
30
|
+
class AbstractAdapter
|
31
|
+
def index_exists?(table_name, column_name)
|
32
|
+
indexes(table_name).any? { |index| index.name == index_name(table_name, column_name) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'test_helper' )
|
2
|
+
require 'globalize/translation'
|
3
|
+
|
4
|
+
class TranslationTest < ActiveSupport::TestCase
|
5
|
+
include Globalize
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@translation = Translation::Static.new 'foo', :locale => :'en-US'
|
9
|
+
end
|
10
|
+
|
11
|
+
test "responds to fallback?" do
|
12
|
+
assert @translation.respond_to?( :fallback? )
|
13
|
+
end
|
14
|
+
|
15
|
+
test "returns true when :locale and :requested_locale are not equal" do
|
16
|
+
@translation.requested_locale = :'de-DE'
|
17
|
+
assert @translation.fallback?
|
18
|
+
end
|
19
|
+
|
20
|
+
test "returns false when :locale and :requested_locale are equal" do
|
21
|
+
@translation.requested_locale = :'en-US'
|
22
|
+
assert !@translation.fallback?
|
23
|
+
end
|
24
|
+
|
25
|
+
test "has the attribute :locale" do
|
26
|
+
assert @translation.respond_to?( :locale )
|
27
|
+
end
|
28
|
+
|
29
|
+
test "has the attribute :requested_locale" do
|
30
|
+
assert @translation.respond_to?( :requested_locale )
|
31
|
+
end
|
32
|
+
|
33
|
+
test "has the attribute :options" do
|
34
|
+
assert @translation.respond_to?( :options )
|
35
|
+
end
|
36
|
+
|
37
|
+
test "has the attribute :plural_key" do
|
38
|
+
assert @translation.respond_to?( :plural_key )
|
39
|
+
end
|
40
|
+
|
41
|
+
test "has the attribute :original" do
|
42
|
+
assert @translation.respond_to?( :original )
|
43
|
+
end
|
44
|
+
|
45
|
+
test "Translation::Attribute has the attribute :locale" do
|
46
|
+
translation = Translation::Attribute.new 'foo'
|
47
|
+
assert translation.respond_to?( :locale )
|
48
|
+
end
|
49
|
+
|
50
|
+
test "Translation::Attribute has the attribute :requested_locale" do
|
51
|
+
translation = Translation::Attribute.new 'foo'
|
52
|
+
assert translation.respond_to?( :requested_locale )
|
53
|
+
end
|
54
|
+
end
|