globalize2 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. data/.gitignore +2 -1
  2. data/README.textile +27 -143
  3. data/VERSION +1 -1
  4. data/generators/templates/db_backend_migration.rb +3 -3
  5. data/globalize2.gemspec +30 -49
  6. data/init.rb +1 -8
  7. data/lib/globalize.rb +15 -0
  8. data/lib/globalize/active_record.rb +195 -0
  9. data/lib/globalize/active_record/adapter.rb +80 -0
  10. data/lib/globalize/active_record/attributes.rb +25 -0
  11. data/lib/globalize/active_record/migration.rb +40 -0
  12. data/lib/{globalize/i18n → i18n}/missing_translations_log_handler.rb +8 -8
  13. data/lib/{globalize/i18n → i18n}/missing_translations_raise_handler.rb +3 -5
  14. data/test/active_record/fallbacks_test.rb +102 -0
  15. data/test/{model/active_record → active_record}/migration_test.rb +21 -26
  16. data/test/{model/active_record → active_record}/sti_translated_test.rb +4 -30
  17. data/test/active_record/translates_test.rb +87 -0
  18. data/test/active_record/translation_class_test.rb +30 -0
  19. data/test/active_record/validation_tests.rb +75 -0
  20. data/test/active_record_test.rb +451 -0
  21. data/test/data/models.rb +16 -0
  22. data/test/data/schema.rb +23 -7
  23. data/test/i18n/missing_translations_test.rb +6 -6
  24. data/test/test_helper.rb +55 -15
  25. metadata +33 -46
  26. data/lib/globalize/backend/chain.rb +0 -102
  27. data/lib/globalize/backend/pluralizing.rb +0 -37
  28. data/lib/globalize/backend/static.rb +0 -61
  29. data/lib/globalize/load_path.rb +0 -63
  30. data/lib/globalize/locale/fallbacks.rb +0 -63
  31. data/lib/globalize/locale/language_tag.rb +0 -81
  32. data/lib/globalize/model/active_record.rb +0 -56
  33. data/lib/globalize/model/active_record/adapter.rb +0 -100
  34. data/lib/globalize/model/active_record/translated.rb +0 -174
  35. data/lib/globalize/translation.rb +0 -32
  36. data/lib/locale/root.yml +0 -3
  37. data/lib/rails_edge_load_path_patch.rb +0 -40
  38. data/notes.textile +0 -51
  39. data/test/backends/chained_test.rb +0 -175
  40. data/test/backends/pluralizing_test.rb +0 -63
  41. data/test/backends/static_test.rb +0 -147
  42. data/test/data/locale/all.yml +0 -2
  43. data/test/data/locale/de-DE.yml +0 -2
  44. data/test/data/locale/en-US.yml +0 -2
  45. data/test/data/locale/en-US/module.yml +0 -2
  46. data/test/data/locale/fi-FI/module.yml +0 -2
  47. data/test/data/locale/root.yml +0 -0
  48. data/test/load_path_test.rb +0 -49
  49. data/test/locale/fallbacks_test.rb +0 -154
  50. data/test/locale/language_tag_test.rb +0 -130
  51. data/test/model/active_record/translated_test.rb +0 -487
  52. data/test/translation_test.rb +0 -54
@@ -1,2 +0,0 @@
1
- en-US:
2
- from-all-file: From the "all" file.
@@ -1,2 +0,0 @@
1
- de-DE:
2
- from-locale-file: Aus der Locale Datei.
@@ -1,2 +0,0 @@
1
- en-US:
2
- from-locale-file: From the locale file.
@@ -1,2 +0,0 @@
1
- en-US:
2
- from-locale-dir: From the locale directory.
@@ -1,2 +0,0 @@
1
- fi-FI:
2
- from-locale-dir: Locale hakemistosta.
File without changes
@@ -1,49 +0,0 @@
1
- require File.join( File.dirname(__FILE__), 'test_helper' )
2
- require 'globalize/load_path'
3
-
4
- class LoadPathTest < ActiveSupport::TestCase
5
- def setup
6
- @plugin_dir = "#{File.dirname(__FILE__)}/.."
7
- @locale_dir = "#{File.dirname(__FILE__)}/data/locale"
8
- @load_path = Globalize::LoadPath.new
9
- end
10
-
11
- test "returns glob patterns for all locales and ruby + yaml files by default" do
12
- patterns = %w(locales/all.rb
13
- locales/*.rb
14
- locales/*/**/*.rb
15
- locales/all.yml
16
- locales/*.yml
17
- locales/*/**/*.yml)
18
- assert_equal patterns, @load_path.send(:patterns, 'locales')
19
- end
20
-
21
- test "returns the glob patterns for registered locales and extensions" do
22
- @load_path.locales = [:en, :de]
23
- @load_path.extensions = [:sql]
24
- patterns = %w(locales/all.sql
25
- locales/en.sql
26
- locales/en/**/*.sql
27
- locales/de.sql
28
- locales/de/**/*.sql)
29
- assert_equal patterns, @load_path.send(:patterns, 'locales')
30
- end
31
-
32
- test "expands paths using yml as a default file extension" do
33
- @load_path << @locale_dir
34
- expected = %w(all.yml de-DE.yml en-US.yml en-US/module.yml fi-FI/module.yml root.yml)
35
- assert_equal expected, @load_path.map{|path| path.sub("#{@locale_dir}\/", '')}
36
- end
37
-
38
- test "appends new paths to the collection so earlier collected paths preceed later collected ones" do
39
- @load_path.locales = [:root]
40
- @load_path << "#{@plugin_dir}/lib/locale"
41
- @load_path << @locale_dir
42
-
43
- expected = %W(#{@plugin_dir}/lib/locale/root.yml
44
- #{@locale_dir}/all.yml
45
- #{@locale_dir}/root.yml)
46
- assert_equal expected, @load_path
47
- end
48
-
49
- end
@@ -1,154 +0,0 @@
1
- require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2
- require 'globalize/locale/fallbacks'
3
-
4
- include Globalize::Locale
5
- I18n.default_locale = :'en-US' # This has to be set explicitly, no longer default for I18n
6
-
7
- class FallbacksTest < ActiveSupport::TestCase
8
- def setup
9
- I18n.fallbacks = Fallbacks.new
10
- end
11
-
12
- def teardown
13
- I18n.default_locale = :'en-US'
14
- end
15
-
16
- test "#[] caches computed results" do
17
- I18n.fallbacks['en']
18
- assert_equal( { :en => [:en, :"en-US", :root] }, I18n.fallbacks )
19
- end
20
-
21
- test "#defaults always reflect the I18n.default_locale if no default has been set manually" do
22
- I18n.default_locale = :'en-US'
23
- assert_equal( [:'en-US', :en, :root], I18n.fallbacks.defaults )
24
- end
25
-
26
- test "#defaults always reflect a manually passed default locale if any" do
27
- I18n.fallbacks = Fallbacks.new(:'fi-FI')
28
- assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults )
29
- I18n.default_locale = :'de-DE'
30
- assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults )
31
- end
32
-
33
- test "#defaults allows to set multiple defaults" do
34
- I18n.fallbacks = Fallbacks.new(:'fi-FI', :'se-FI')
35
- assert_equal( [:'fi-FI', :fi, :'se-FI', :se, :root], I18n.fallbacks.defaults )
36
- end
37
- end
38
-
39
- class NoMappingFallbacksTest < ActiveSupport::TestCase
40
- def setup
41
- @fallbacks = Fallbacks.new(:'en-US')
42
- end
43
-
44
- test "returns [:es, :en-US, :root] for :es" do
45
- assert_equal [:es, :"en-US", :en, :root], @fallbacks[:es]
46
- end
47
-
48
- test "returns [:es-ES, :es, :en-US, :root] for :es-ES" do
49
- assert_equal [:"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"es-ES"]
50
- end
51
-
52
- test "returns [:es-MX, :es, :en-US, :root] for :es-MX" do
53
- assert_equal [:"es-MX", :es, :"en-US", :en, :root], @fallbacks[:"es-MX"]
54
- end
55
-
56
- test "returns [:es-Latn-ES, :es-Latn, :es, :en-US, :root] for :es-Latn-ES" do
57
- assert_equal [:"es-Latn-ES", :"es-Latn", :es, :"en-US", :en, :root], @fallbacks[:'es-Latn-ES']
58
- end
59
-
60
- test "returns [:en, :en-US, :root] for :en" do
61
- assert_equal [:en, :"en-US", :root], @fallbacks[:en]
62
- end
63
-
64
- test "returns [:en-US, :en, :root] for :en-US (special case: locale == default)" do
65
- assert_equal [:"en-US", :en, :root], @fallbacks[:"en-US"]
66
- end
67
- end
68
-
69
- class CaMappingFallbacksTest < ActiveSupport::TestCase
70
- # Most people who speak Catalan also live in Spain, so test is safe to assume
71
- # that they also speak Spanish as spoken in Spain.
72
- def setup
73
- @fallbacks = Fallbacks.new(:'en-US')
74
- @fallbacks.map :ca => :"es-ES"
75
- end
76
-
77
- test "returns [:ca, :es-ES, :es, :en-US, :root] for :ca" do
78
- assert_equal [:ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:ca]
79
- end
80
-
81
- test "returns [:ca-ES, :ca, :es-ES, :es, :en-US, :root] for :ca-ES" do
82
- assert_equal [:"ca-ES", :ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"ca-ES"]
83
- end
84
- end
85
-
86
- class ArMappingFallbacksTest < ActiveSupport::TestCase
87
- # People who speak Arabic as spoken in Palestine often times also speak
88
- # Hebrew as spoken in Israel. However test is in no way safe to assume that
89
- # everybody who speaks Arabic also speaks Hebrew.
90
- def setup
91
- @fallbacks = Fallbacks.new(:'en-US')
92
- @fallbacks.map :"ar-PS" => :"he-IL"
93
- end
94
-
95
- test "returns [:ar, :en-US, :root] for :ar" do
96
- assert_equal [:ar, :"en-US", :en, :root], @fallbacks[:ar]
97
- end
98
-
99
- test "returns [:ar-EG, :ar, :en-US, :root] for :ar-EG" do
100
- assert_equal [:"ar-EG", :ar, :"en-US", :en, :root], @fallbacks[:"ar-EG"]
101
- end
102
-
103
- test "returns [:ar-PS, :ar, :he-IL, :he, :en-US, :root] for :ar-PS" do
104
- assert_equal [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en, :root], @fallbacks[:"ar-PS"]
105
- end
106
- end
107
-
108
- class SmsMappingFallbacksTest < ActiveSupport::TestCase
109
- # Sami people live in several scandinavian countries. In Finnland many people
110
- # know Swedish and Finnish. Thus, test can be assumed that Sami living in
111
- # Finnland also speak Swedish and Finnish.
112
- def setup
113
- @fallbacks = Fallbacks.new(:'en-US')
114
- @fallbacks.map :sms => [:"se-FI", :"fi-FI"]
115
- end
116
-
117
- test "returns [:sms-FI, :sms, :se-FI, :se, :fi-FI, :fi, :en-US, :root] for :sms-FI" do
118
- assert_equal [:"sms-FI", :sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en, :root], @fallbacks[:"sms-FI"]
119
- end
120
- end
121
-
122
- class DeAtMappingFallbacksTest < ActiveSupport::TestCase
123
- def setup
124
- @fallbacks = Fallbacks.new(:'en-US')
125
- @fallbacks.map :"de-AT" => :"de-DE"
126
- end
127
-
128
- test "returns [:de, :en-US, :root] for de" do
129
- assert_equal [:de, :"en-US", :en, :root], @fallbacks[:"de"]
130
- end
131
-
132
- test "returns [:de-DE, :de, :en-US, :root] for de-DE" do
133
- assert_equal [:"de-DE", :de, :"en-US", :en, :root], @fallbacks[:"de-DE"]
134
- end
135
-
136
- test "returns [:de-AT, :de, :de-DE, :en-US, :root] for de-AT" do
137
- assert_equal [:"de-AT", :de, :"de-DE", :"en-US", :en, :root], @fallbacks[:"de-AT"]
138
- end
139
- end
140
-
141
- class DeMappingFallbacksTest < ActiveSupport::TestCase
142
- def setup
143
- @fallbacks = Fallbacks.new(:'en-US')
144
- @fallbacks.map :de => :en, :he => :en
145
- end
146
-
147
- test "returns [:de, :en, :root] for :de" do
148
- assert_equal [:de, :en, :"en-US", :root], @fallbacks[:de]
149
- end
150
-
151
- test "returns [:he, :en, :root] for :de" do
152
- assert_equal [:he, :en, :"en-US", :root], @fallbacks[:he]
153
- end
154
- end
@@ -1,130 +0,0 @@
1
- require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2
- require 'globalize/locale/language_tag'
3
-
4
- include Globalize::Locale
5
-
6
- class LanguageTagTest < ActiveSupport::TestCase
7
- test "given a valid tag 'de' returns an LanguageTag from #tag" do
8
- assert_instance_of LanguageTag, LanguageTag.tag('de')
9
- end
10
-
11
- test "given a valid tag 'de' returns an array of subtags" do
12
- assert_equal ['de', nil, nil, nil, nil, nil, nil], LanguageTag::SimpleParser.match('de')
13
- end
14
-
15
- test "given a valid tag 'de-DE' returns an array of subtags" do
16
- assert_equal ['de', nil, 'DE', nil, nil, nil, nil], LanguageTag::SimpleParser.match('de-DE')
17
- end
18
-
19
- test "given a valid lowercase tag 'de-latn-de-variant-x-phonebk' returns an array of subtags" do
20
- assert_equal ['de', 'latn', 'de', 'variant', nil, 'x-phonebk', nil],
21
- LanguageTag::SimpleParser.match('de-latn-de-variant-x-phonebk')
22
- end
23
-
24
- test "given a valid uppercase tag 'DE-LATN-DE-VARIANT-X-PHONEBK' returns an array of subtags" do
25
- assert_equal ['DE', 'LATN', 'DE', 'VARIANT', nil, 'X-PHONEBK', nil],
26
- LanguageTag::SimpleParser.match('DE-LATN-DE-VARIANT-X-PHONEBK')
27
- end
28
-
29
- test "given an invalid tag 'a-DE' test returns false" do
30
- assert !LanguageTag::SimpleParser.match('a-DE')
31
- end
32
-
33
- test "given an invalid tag 'de-419-DE' test returns false" do
34
- assert !LanguageTag::SimpleParser.match('de-419-DE')
35
- end
36
- end
37
-
38
- class DeLatnLanguageTagTest < ActiveSupport::TestCase
39
- def setup
40
- subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon)
41
- @tag = LanguageTag.new *subtags
42
- end
43
-
44
- test "returns 'de' as the language subtag in lowercase" do
45
- assert_equal 'de', @tag.language
46
- end
47
-
48
- test "returns 'Latn' as the script subtag in titlecase" do
49
- assert_equal 'Latn', @tag.script
50
- end
51
-
52
- test "returns 'DE' as the region subtag in uppercase" do
53
- assert_equal 'DE', @tag.region
54
- end
55
-
56
- test "returns 'variant' as the variant subtag in lowercase" do
57
- assert_equal 'variant', @tag.variant
58
- end
59
-
60
- test "returns 'a-ext' as the extension subtag" do
61
- assert_equal 'a-ext', @tag.extension
62
- end
63
-
64
- test "returns 'x-phonebk' as the privateuse subtag" do
65
- assert_equal 'x-phonebk', @tag.privateuse
66
- end
67
-
68
- test "returns 'i-klingon' as the grandfathered subtag" do
69
- assert_equal 'i-klingon', @tag.grandfathered
70
- end
71
-
72
- test "returns a formatted tag string from #to_s" do
73
- assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon', @tag.to_s
74
- end
75
-
76
- test "returns an array containing the formatted subtags from #to_a" do
77
- assert_equal %w(de Latn DE variant a-ext x-phonebk i-klingon), @tag.to_a
78
- end
79
- end
80
-
81
- class InheritanceLanguageTagTest < ActiveSupport::TestCase
82
- test "returns 'de-Latn-DE-variant-a-ext-x-phonebk' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
83
- tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
84
- assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk', tag.parent.to_s
85
- end
86
-
87
- test "returns 'de-Latn-DE-variant-a-ext' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk'" do
88
- tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk)
89
- assert_equal 'de-Latn-DE-variant-a-ext', tag.parent.to_s
90
- end
91
-
92
- test "returns 'de-Latn-DE-variant' as the parent of 'de-Latn-DE-variant-a-ext'" do
93
- tag = LanguageTag.new *%w(de Latn DE variant a-ext)
94
- assert_equal 'de-Latn-DE-variant', tag.parent.to_s
95
- end
96
-
97
- test "returns 'de-Latn-DE' as the parent of 'de-Latn-DE-variant'" do
98
- tag = LanguageTag.new *%w(de Latn DE variant)
99
- assert_equal 'de-Latn-DE', tag.parent.to_s
100
- end
101
-
102
- test "returns 'de-Latn' as the parent of 'de-Latn-DE'" do
103
- tag = LanguageTag.new *%w(de Latn DE)
104
- assert_equal 'de-Latn', tag.parent.to_s
105
- end
106
-
107
- test "returns 'de' as the parent of 'de-Latn'" do
108
- tag = LanguageTag.new *%w(de Latn)
109
- assert_equal 'de', tag.parent.to_s
110
- end
111
-
112
- # TODO RFC4647 says: "If no language tag matches the request, the "default" value is returned."
113
- # where should we set the default language?
114
- # test "returns '' as the parent of 'de'" do
115
- # tag = LanguageTag.new *%w(de)
116
- # assert_equal '', tag.parent.to_s
117
- # end
118
-
119
- test "returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
120
- parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon
121
- de-Latn-DE-variant-a-ext-x-phonebk
122
- de-Latn-DE-variant-a-ext
123
- de-Latn-DE-variant
124
- de-Latn-DE
125
- de-Latn
126
- de)
127
- tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
128
- assert_equal parents, tag.parents.map{|tag| tag.to_s}
129
- end
130
- end
@@ -1,487 +0,0 @@
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', 'models' )
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 "saving record correctly after post-save reload" do
343
- reloader = Reloader.create :content => 'foo'
344
- assert_equal 'foo', reloader.content
345
- end
346
-
347
- test "including globalize_translations" do
348
- I18n.locale = :de
349
- Post.create :subject => "Foo1", :content => "Bar1"
350
- Post.create :subject => "Foo2", :content => "Bar2"
351
-
352
- class << Post
353
- def tranlsations_included
354
- self.all(:include => :globalize_translations)
355
- end
356
- end
357
-
358
- default = Post.all.map {|x| [x.subject, x.content]}
359
- with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
360
- assert_equal default, with_include
361
- end
362
-
363
- test "setting multiple translations at once with options hash" do
364
- Post.locale = :de
365
- post = Post.create :subject => "foo1", :content => "foo1"
366
- Post.locale = :en
367
- post.update_attributes( :subject => "bar1", :content => "bar1" )
368
-
369
- options = { :de => {:subject => "foo2", :content => "foo2"},
370
- :en => {:subject => "bar2", :content => "bar2"} }
371
- post.set_translations options
372
- post.reload
373
-
374
- assert ["bar2", "bar2"], [post.subject, post.content]
375
- Post.locale = :de
376
- assert ["foo2", "foo2"], [post.subject, post.content]
377
- end
378
-
379
- test "setting only one translation with set_translations" do
380
- Post.locale = :de
381
- post = Post.create :subject => "foo1", :content => "foo1"
382
- Post.locale = :en
383
- post.update_attributes( :subject => "bar1", :content => "bar1" )
384
-
385
- options = { :en => {:subject => "bar2", :content => "bar2"} }
386
- post.set_translations options
387
- post.reload
388
-
389
- assert ["bar2", "bar2"], [post.subject, post.content]
390
- Post.locale = :de
391
- assert ["foo1", "foo1"], [post.subject, post.content]
392
- end
393
-
394
- test "setting only selected attributes with set_translations" do
395
- Post.locale = :de
396
- post = Post.create :subject => "foo1", :content => "foo1"
397
- Post.locale = :en
398
- post.update_attributes( :subject => "bar1", :content => "bar1" )
399
-
400
- options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
401
- post.set_translations options
402
- post.reload
403
-
404
- assert ["bar2", "bar1"], [post.subject, post.content]
405
- Post.locale = :de
406
- assert ["foo1", "foo2"], [post.subject, post.content]
407
- end
408
-
409
- test "setting invalid attributes raises ArgumentError" do
410
- Post.locale = :de
411
- post = Post.create :subject => "foo1", :content => "foo1"
412
- Post.locale = :en
413
- post.update_attributes( :subject => "bar1", :content => "bar1" )
414
-
415
- options = { :de => {:fake => "foo2"} }
416
- exception = assert_raise(ActiveRecord::UnknownAttributeError) do
417
- post.set_translations options
418
- end
419
- assert_equal "unknown attribute: fake", exception.message
420
- end
421
-
422
- test "reload accepting find options" do
423
- p = Post.create :subject => "Foo", :content => "Bar"
424
- assert p.reload(:readonly => true, :lock => true)
425
- assert_raise(ArgumentError) { p.reload(:foo => :bar) }
426
- end
427
-
428
- test "dependent destroy of translation" do
429
- p = Post.create :subject => "Foo", :content => "Bar"
430
- assert_equal 1, PostTranslation.count
431
- p.destroy
432
- assert_equal 0, PostTranslation.count
433
- end
434
-
435
- test "translating subclass of untranslated comment model" do
436
- translated_comment = TranslatedComment.create(:post => @post)
437
- assert_nothing_raised { translated_comment.globalize_translations }
438
- end
439
-
440
- test "modifiying translated comments works as expected" do
441
- I18n.locale = :en
442
- translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
443
- assert_equal 'foo', translated_comment.content
444
-
445
- I18n.locale = :de
446
- translated_comment.content = 'bar'
447
- assert translated_comment.save
448
- assert_equal 'bar', translated_comment.content
449
-
450
- I18n.locale = :en
451
- assert_equal 'foo', translated_comment.content
452
-
453
- assert_equal 2, translated_comment.globalize_translations.size
454
- end
455
-
456
- test "can create a proxy class for a namespaced model" do
457
- module Foo
458
- module Bar
459
- class Baz < ActiveRecord::Base
460
- translates :bumm
461
- end
462
- end
463
- end
464
- end
465
-
466
- test "attribute translated before type cast" do
467
- Post.locale = :en
468
- post = Post.create :subject => 'foo', :content => 'bar'
469
- Post.locale = :de
470
- post.update_attribute :subject, "German foo"
471
- assert_equal 'German foo', post.subject_before_type_cast
472
- Post.locale = :en
473
- assert_equal 'foo', post.subject_before_type_cast
474
- end
475
-
476
- test "don't override existing translation model" do
477
- assert PostTranslation.new.respond_to?(:existing_method)
478
- end
479
- end
480
-
481
- # TODO should validate_presence_of take fallbacks into account? maybe we need
482
- # an extra validation call, or more options for validate_presence_of.
483
- # TODO error checking for fields that exist in main table, don't exist in
484
- # proxy table, aren't strings or text
485
- #
486
- # TODO allow finding by translated attributes in conditions?
487
- # TODO generate advanced dynamic finders?