i18n-active_record 0.2.2 → 1.4.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.
@@ -1,112 +1,225 @@
1
- require_relative './test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
2
4
 
3
5
  class I18nBackendActiveRecordTest < I18n::TestCase
4
6
  def setup
7
+ super
8
+
9
+ I18n::Backend::ActiveRecord::Translation.destroy_all
5
10
  I18n.backend = I18n::Backend::ActiveRecord.new
6
- store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
11
+
12
+ store_translations(:en, foo: { bar: 'bar', baz: 'baz' })
7
13
  end
8
14
 
9
15
  def teardown
10
- I18n::Backend::ActiveRecord::Translation.destroy_all
11
16
  I18n::Backend::ActiveRecord.instance_variable_set :@config, I18n::Backend::ActiveRecord::Configuration.new
17
+
12
18
  super
13
19
  end
14
20
 
15
- test "store_translations does not allow ambiguous keys (1)" do
16
- I18n::Backend::ActiveRecord::Translation.delete_all
17
- I18n.backend.store_translations(:en, :foo => 'foo')
18
- I18n.backend.store_translations(:en, :foo => { :bar => 'bar' })
19
- I18n.backend.store_translations(:en, :foo => { :baz => 'baz' })
21
+ class WithoutCacheTest < I18nBackendActiveRecordTest
22
+ def setup
23
+ super
20
24
 
21
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo').all
22
- assert_equal %w(bar baz), translations.map(&:value)
25
+ I18n::Backend::ActiveRecord.config.cache_translations = false
26
+ end
23
27
 
24
- assert_equal({ :bar => 'bar', :baz => 'baz' }, I18n.t(:foo))
25
- end
28
+ test 'store_translations does not allow ambiguous keys (1)' do
29
+ store_translations(:en, foo: 'foo')
30
+ store_translations(:en, foo: { bar: 'bar' })
31
+ store_translations(:en, foo: { baz: 'baz' })
26
32
 
27
- test "store_translations does not allow ambiguous keys (2)" do
28
- I18n::Backend::ActiveRecord::Translation.delete_all
29
- I18n.backend.store_translations(:en, :foo => { :bar => 'bar' })
30
- I18n.backend.store_translations(:en, :foo => { :baz => 'baz' })
31
- I18n.backend.store_translations(:en, :foo => 'foo')
33
+ translations = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo').all
34
+ assert_equal %w[bar baz], translations.map(&:value)
32
35
 
33
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo').all
34
- assert_equal %w(foo), translations.map(&:value)
36
+ assert_equal({ bar: 'bar', baz: 'baz' }, I18n.t(:foo))
37
+ end
35
38
 
36
- assert_equal 'foo', I18n.t(:foo)
37
- end
39
+ test 'store_translations does not allow ambiguous keys (2)' do
40
+ store_translations(:en, foo: { bar: 'bar' })
41
+ store_translations(:en, foo: { baz: 'baz' })
42
+ store_translations(:en, foo: 'foo')
38
43
 
39
- test "can store translations with keys that are translations containing special chars" do
40
- I18n.backend.store_translations(:es, :"Pagina's" => "Pagina's" )
41
- assert_equal "Pagina's", I18n.t(:"Pagina's", :locale => :es)
42
- end
44
+ translations = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo').all
45
+ assert_equal %w[foo], translations.map(&:value)
43
46
 
44
- test "missing translations table does not cause an error in #available_locales" do
45
- I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid, 'msg')
46
- assert_equal [], I18n.backend.available_locales
47
- end
47
+ assert_equal 'foo', I18n.t(:foo)
48
+ end
48
49
 
49
- test "expand_keys" do
50
- assert_equal %w(foo foo.bar foo.bar.baz), I18n.backend.send(:expand_keys, :'foo.bar.baz')
51
- end
50
+ test 'can store translations with keys that are translations containing special chars' do
51
+ store_translations(:es, "Pagina's": "Pagina's")
52
52
 
53
- test "available_locales returns uniq locales" do
54
- I18n::Backend::ActiveRecord::Translation.delete_all
55
- I18n.backend.store_translations(:en, :foo => { :bar => 'bar' })
56
- I18n.backend.store_translations(:en, :foo => { :baz => 'baz' })
57
- I18n.backend.store_translations(:de, :foo1 => 'foo')
58
- I18n.backend.store_translations(:de, :foo2 => 'foo')
59
- I18n.backend.store_translations(:uk, :foo3 => 'foo')
60
-
61
- available_locales = I18n::Backend::ActiveRecord::Translation.available_locales
62
- assert_equal 3, available_locales.size
63
- assert_includes available_locales, :en
64
- assert_includes available_locales, :de
65
- assert_includes available_locales, :uk
66
- end
53
+ assert_equal "Pagina's", I18n.t(:"Pagina's", locale: :es)
54
+ end
67
55
 
68
- test "the default configuration has cleanup_with_destroy == false" do
69
- refute I18n::Backend::ActiveRecord.config.cleanup_with_destroy
70
- end
56
+ test 'missing translations table does not cause an error in #available_locales' do
57
+ I18n::Backend::ActiveRecord::Translation
58
+ .expects(:available_locales)
59
+ .raises(::ActiveRecord::StatementInvalid, 'msg')
60
+ assert_equal [], I18n.backend.available_locales
61
+ end
71
62
 
72
- test "the configuration supports cleanup_with_destroy being set" do
73
- I18n::Backend::ActiveRecord.configure do |config|
74
- config.cleanup_with_destroy = true
63
+ test 'expand_keys' do
64
+ assert_equal %w[foo foo.bar foo.bar.baz], I18n.backend.send(:expand_keys, :'foo.bar.baz')
75
65
  end
76
66
 
77
- assert I18n::Backend::ActiveRecord.config.cleanup_with_destroy
78
- end
67
+ test 'available_locales returns uniq locales' do
68
+ store_translations(:en, foo: { bar: 'bar' })
69
+ store_translations(:en, foo: { baz: 'baz' })
70
+ store_translations(:de, foo1: 'foo')
71
+ store_translations(:de, foo2: 'foo')
72
+ store_translations(:uk, foo3: 'foo')
73
+
74
+ available_locales = I18n::Backend::ActiveRecord::Translation.available_locales
75
+ assert_equal 3, available_locales.size
76
+ assert_includes available_locales, :en
77
+ assert_includes available_locales, :de
78
+ assert_includes available_locales, :uk
79
+ end
79
80
 
80
- test "fetching subtree of translations" do
81
- I18n::Backend::ActiveRecord::Translation.delete_all
82
- I18n.backend.store_translations(:en, foo: { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } })
83
- assert_equal I18n.t(:foo), { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } }
84
- end
81
+ test 'the default configuration has cleanup_with_destroy == false' do
82
+ refute I18n::Backend::ActiveRecord.config.cleanup_with_destroy
83
+ end
84
+
85
+ test 'the configuration supports cleanup_with_destroy being set' do
86
+ I18n::Backend::ActiveRecord.configure do |config|
87
+ config.cleanup_with_destroy = true
88
+ end
89
+
90
+ assert I18n::Backend::ActiveRecord.config.cleanup_with_destroy
91
+ end
92
+
93
+ test 'fetching subtree of translations' do
94
+ store_translations(:en, foo: { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } })
95
+
96
+ assert_equal I18n.t(:foo), { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } }
97
+ end
98
+
99
+ test 'build_translation_hash_by_key' do
100
+ translation = I18n::Backend::ActiveRecord::Translation.new(value: 'translation', key: 'foo.bar.fizz.buzz')
101
+ expected_hash = { 'bar' => { 'fizz' => { 'buzz' => 'translation' } } }
102
+
103
+ assert_equal I18n.backend.send(:build_translation_hash_by_key, 'foo', translation), expected_hash
104
+ end
105
+
106
+ test 'returning all keys via .' do
107
+ expected_hash = { foo: { bar: 'bar', baz: 'baz' } }
85
108
 
86
- test "build_translation_hash_by_key" do
87
- translation = I18n::Backend::ActiveRecord::Translation.new(value: 'translation', key: 'foo.bar.fizz.buzz')
88
- expected_hash = { 'bar' => { 'fizz' => { 'buzz' => 'translation' } } }
89
- assert_equal I18n.backend.send(:build_translation_hash_by_key, 'foo', translation), expected_hash
109
+ assert_equal expected_hash, I18n.t('.')
110
+ end
111
+
112
+ test 'accessing keys with a trailing/leading period' do
113
+ expected_hash = { bar: 'bar', baz: 'baz' }
114
+
115
+ assert_equal expected_hash, I18n.t('foo')
116
+ assert_equal expected_hash, I18n.t('.foo')
117
+ assert_equal expected_hash, I18n.t('foo.')
118
+ assert_equal expected_hash, I18n.t('.foo.')
119
+ assert_equal expected_hash, I18n.t('.foo.')
120
+ end
121
+
122
+ test 'returning all keys via . when there are no keys' do
123
+ I18n.t('.') # Fixes test flakiness by loading available locales
124
+ I18n::Backend::ActiveRecord::Translation.destroy_all
125
+
126
+ assert_match(/[Tt]ranslation missing: en\.no key/, I18n.t('.'))
127
+ end
128
+
129
+ test 'intially unitinitialized' do
130
+ refute I18n.backend.initialized?
131
+
132
+ I18n.backend.init_translations
133
+ assert I18n.backend.initialized?
134
+
135
+ I18n.backend.reload!
136
+ refute I18n.backend.initialized?
137
+
138
+ I18n.backend.init_translations
139
+ assert I18n.backend.initialized?
140
+ end
141
+
142
+ test 'translations returns all translations' do
143
+ expected_hash = { en: { foo: { bar: 'bar', baz: 'baz' } } }
144
+ I18n.backend.init_translations
145
+
146
+ assert_equal expected_hash, I18n.backend.send(:translations)
147
+ assert I18n.backend.initialized?
148
+ end
149
+
150
+ test 'translations initialized with do_init argument' do
151
+ expected_hash = { en: { foo: { bar: 'bar', baz: 'baz' } } }
152
+
153
+ refute I18n.backend.initialized?
154
+ assert_equal expected_hash, I18n.backend.send(:translations, do_init: true)
155
+ assert I18n.backend.initialized?
156
+ end
90
157
  end
91
158
 
92
- test "returning all keys via ." do
93
- expected_hash = {:foo => { :bar => 'bar', :baz => 'baz' }}
94
- assert_equal expected_hash, I18n.t('.')
159
+ class WithCacheTest < WithoutCacheTest
160
+ def setup
161
+ super
162
+
163
+ I18n::Backend::ActiveRecord.config.cache_translations = true
164
+ end
165
+
166
+ test 'reinitialization when the DB is empty' do
167
+ I18n::Backend::ActiveRecord::Translation.destroy_all
168
+
169
+ I18n.backend.init_translations
170
+ I18n.backend.expects(:init_translations).never
171
+
172
+ I18n.t('foo')
173
+ end
95
174
  end
96
175
 
97
- test "accessing keys with a trailing/leading period" do
98
- expected_hash = { :bar => 'bar', :baz => 'baz' }
99
- assert_equal expected_hash, I18n.t('foo')
100
- assert_equal expected_hash, I18n.t('.foo')
101
- assert_equal expected_hash, I18n.t('foo.')
102
- assert_equal expected_hash, I18n.t('.foo.')
103
- assert_equal expected_hash, I18n.t('.foo.')
176
+ class WithCustomTranslationModel < I18nBackendActiveRecordTest
177
+ class CustomTranslation < I18n::Backend::ActiveRecord::Translation
178
+ def value=(val)
179
+ super("custom #{val}")
180
+ end
181
+ end
182
+
183
+ def setup
184
+ super
185
+
186
+ I18n::Backend::ActiveRecord.config.translation_model = CustomTranslation
187
+ end
188
+
189
+ test 'use a custom model' do
190
+ store_translations(:en, foo: 'foo')
191
+ assert_equal I18n.t(:foo), 'custom foo'
192
+ end
104
193
  end
105
194
 
106
- test "returning all keys via . when there are no keys" do
107
- I18n.t('.') # Fixes test flakiness by loading available locales
108
- I18n::Backend::ActiveRecord::Translation.destroy_all
195
+ class ScopeTest < I18nBackendActiveRecordTest
196
+ def setup
197
+ super
109
198
 
110
- assert_equal "translation missing: en.no key", I18n.t('.')
199
+ I18n::Backend::ActiveRecord.config.scope = 'scope1'
200
+ end
201
+
202
+ test 'scope config option divides translations into isolated sets' do
203
+ store_translations(:en, foo: 'foo1')
204
+ assert_equal('foo1', I18n.t(:foo))
205
+
206
+ I18n::Backend::ActiveRecord.config.scope = 'scope2'
207
+ store_translations(:en, foo: 'foo2')
208
+ assert_equal('foo2', I18n.t(:foo))
209
+
210
+ I18n::Backend::ActiveRecord.config.scope = 'scope1'
211
+ assert_equal('foo1', I18n.t(:foo))
212
+ end
213
+
214
+ test 'scope config of nil disables scope' do
215
+ store_translations(:en, bar1: 'bar1')
216
+
217
+ I18n::Backend::ActiveRecord.config.scope = 'scope2'
218
+ store_translations(:en, bar2: 'bar2')
219
+
220
+ I18n::Backend::ActiveRecord.config.scope = nil
221
+ assert_equal('bar1', I18n.t(:bar1))
222
+ assert_equal('bar2', I18n.t(:bar2))
223
+ end
111
224
  end
112
225
  end
data/test/api_test.rb CHANGED
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class I18nActiveRecordApiTest < I18n::TestCase
4
6
  def setup
5
7
  I18n.backend = I18n::Backend::ActiveRecord.new
8
+
6
9
  super
7
10
  end
8
11
 
@@ -23,7 +26,7 @@ class I18nActiveRecordApiTest < I18n::TestCase
23
26
  include I18n::Tests::Localization::Time
24
27
  include I18n::Tests::Localization::Procs if can_store_procs?
25
28
 
26
- test "make sure we use an ActiveRecord backend" do
29
+ test 'make sure we use an ActiveRecord backend' do
27
30
  assert_equal I18n::Backend::ActiveRecord, I18n.backend.class
28
31
  end
29
32
  end
data/test/missing_test.rb CHANGED
@@ -1,70 +1,107 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class I18nActiveRecordMissingTest < I18n::TestCase
4
- class Backend < I18n::Backend::ActiveRecord
6
+ class BackendWithMissing < I18n::Backend::ActiveRecord
5
7
  include I18n::Backend::ActiveRecord::Missing
6
8
  end
7
9
 
8
10
  def setup
9
- I18n.backend.store_translations(:en, :bar => 'Bar', :i18n => { :plural => { :keys => [:zero, :one, :other] } })
10
- I18n.backend = I18n::Backend::Chain.new(Backend.new, I18n.backend)
11
- I18n::Backend::ActiveRecord::Translation.delete_all
12
- end
11
+ super
12
+
13
+ I18n::Backend::ActiveRecord::Translation.destroy_all
14
+ I18n.backend = BackendWithMissing.new
13
15
 
14
- test "can persist interpolations" do
15
- translation = I18n::Backend::ActiveRecord::Translation.new(:key => 'foo', :value => 'bar', :locale => :en)
16
- translation.interpolations = %w(count name)
17
- translation.save
18
- assert translation.valid?
16
+ store_translations(:en, bar: 'Bar', i18n: { plural: { keys: %i[zero one other] } })
19
17
  end
20
18
 
21
- test "lookup persists the key" do
22
- I18n.t('foo.bar.baz')
19
+ def teardown
20
+ I18n::Backend::ActiveRecord.instance_variable_set :@config, I18n::Backend::ActiveRecord::Configuration.new
23
21
 
24
- assert_equal 1, I18n::Backend::ActiveRecord::Translation.count
25
- assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
22
+ super
26
23
  end
27
24
 
28
- test "lookup does not persist the key twice" do
29
- 2.times { I18n.t('foo.bar.baz') }
30
- assert_equal 1, I18n::Backend::ActiveRecord::Translation.count
31
- assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
32
- end
25
+ class WithoutCacheTest < I18nActiveRecordMissingTest
26
+ def setup
27
+ super
33
28
 
34
- test "lookup persists interpolation keys when looked up directly" do
35
- I18n.t('foo.bar.baz', :cow => "lucy" ) # creates stub translation.
36
- translation_stub = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo.bar.baz').first
37
- assert translation_stub.interpolates?(:cow)
38
- end
29
+ I18n::Backend::ActiveRecord.config.cache_translations = false
30
+ end
39
31
 
40
- test "creates one stub per pluralization" do
41
- I18n.t('foo', :count => 999)
42
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).where key: %w{ foo.zero foo.one foo.other }
43
- assert_equal 3, translations.length
44
- end
32
+ test 'can persist interpolations' do
33
+ translation = I18n::Backend::ActiveRecord::Translation.new(key: 'foo', value: 'bar', locale: :en)
34
+ translation.interpolations = %w[count name]
35
+ translation.save
45
36
 
46
- test "creates no stub for base key in pluralization" do
47
- I18n.t('foo', :count => 999)
48
- assert_equal 3, I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo").count
49
- assert !I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key("foo")
50
- end
37
+ assert translation.valid?
38
+ end
51
39
 
52
- test "creates a stub when a custom separator is used" do
53
- I18n.t('foo|baz', :separator => '|')
54
- I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz").first.update_attributes(:value => 'baz!')
55
- assert_equal 'baz!', I18n.t('foo|baz', :separator => '|')
56
- end
40
+ test 'lookup persists the key' do
41
+ I18n.t('foo.bar.baz')
42
+
43
+ assert_equal 3, I18n::Backend::ActiveRecord::Translation.count
44
+ assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
45
+ end
46
+
47
+ test 'lookup does not persist the key twice' do
48
+ 2.times { I18n.t('foo.bar.baz') }
49
+
50
+ assert_equal 3, I18n::Backend::ActiveRecord::Translation.count
51
+ assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
52
+ end
53
+
54
+ test 'lookup persists interpolation keys when looked up directly' do
55
+ I18n.t('foo.bar.baz', cow: 'lucy') # creates stub translation.
56
+ translation_stub = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo.bar.baz').first
57
57
 
58
- test "creates a stub per pluralization when a custom separator is used" do
59
- I18n.t('foo|bar', :count => 999, :separator => '|')
60
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).where key: %w{ foo.bar.zero foo.bar.one foo.bar.other }
61
- assert_equal 3, translations.length
58
+ assert translation_stub.interpolates?(:cow)
59
+ end
60
+
61
+ test 'creates one stub per pluralization' do
62
+ I18n.t('foo', count: 999)
63
+ translations = I18n::Backend::ActiveRecord::Translation.locale(:en).where(key: %w[foo.zero foo.one foo.other])
64
+
65
+ assert_equal 3, translations.length
66
+ end
67
+
68
+ test 'creates no stub for base key in pluralization' do
69
+ I18n.t('foo', count: 999)
70
+
71
+ assert_equal 3, I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo').count
72
+ assert !I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo')
73
+ end
74
+
75
+ test 'creates a stub when a custom separator is used' do
76
+ I18n.t('foo|baz', separator: '|')
77
+ I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo.baz').first.update(value: 'baz!')
78
+
79
+ assert_equal 'baz!', I18n.t('foo|baz', separator: '|')
80
+ end
81
+
82
+ test 'creates a stub per pluralization when a custom separator is used' do
83
+ I18n.t('foo|bar', count: 999, separator: '|')
84
+ translations = I18n::Backend::ActiveRecord::Translation
85
+ .locale(:en)
86
+ .where(key: %w[foo.bar.zero foo.bar.one foo.bar.other])
87
+
88
+ assert_equal 3, translations.length
89
+ end
90
+
91
+ test 'creates a stub when a custom separator is used and the key contains the flatten separator(a dot character)' do
92
+ key = 'foo|baz.zab'
93
+ I18n.t(key, separator: '|')
94
+ I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz\001zab").first.update(value: 'baz!')
95
+
96
+ assert_equal 'baz!', I18n.t(key, separator: '|')
97
+ end
62
98
  end
63
99
 
64
- test "creates a stub when a custom separator is used and the key contains the flatten separator (a dot character)" do
65
- key = 'foo|baz.zab'
66
- I18n.t(key, :separator => '|')
67
- I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz\001zab").first.update_attributes(:value => 'baz!')
68
- assert_equal 'baz!', I18n.t(key, :separator => '|')
100
+ class WithCacheTest < WithoutCacheTest
101
+ def setup
102
+ super
103
+
104
+ I18n::Backend::ActiveRecord.config.cache_translations = true
105
+ end
69
106
  end
70
107
  end
data/test/test_helper.rb CHANGED
@@ -1,8 +1,8 @@
1
- $KCODE = 'u' if RUBY_VERSION <= '1.9'
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'minitest/autorun'
5
- require 'mocha/setup'
5
+ require 'mocha/minitest'
6
6
  require 'test_declarative'
7
7
 
8
8
  require 'i18n/active_record'
@@ -10,72 +10,91 @@ require 'i18n/tests'
10
10
 
11
11
  begin
12
12
  require 'active_record'
13
- ::ActiveRecord::Base.connection
13
+ ActiveRecord::Base.connection
14
14
  rescue LoadError => e
15
15
  puts "can't use ActiveRecord backend because: #{e.message}"
16
- rescue ::ActiveRecord::ConnectionNotEstablished
16
+ rescue ActiveRecord::ConnectionNotEstablished
17
17
  require 'i18n/backend/active_record'
18
- case ENV['DB']
18
+
19
+ case ENV.fetch('DB', nil)
19
20
  when 'postgres'
20
- ::ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'i18n_unittest', username: ENV['PG_USER'] || 'i18n', password: '', host: 'localhost'
21
+ ActiveRecord::Base.establish_connection(
22
+ adapter: 'postgresql',
23
+ database: 'i18n_unittest',
24
+ username: ENV.fetch('PG_USER', 'postgres'),
25
+ password: ENV.fetch('PG_PASSWORD', 'postgres'),
26
+ host: 'localhost'
27
+ )
21
28
  when 'mysql'
22
- ::ActiveRecord::Base.establish_connection adapter: 'mysql2', database: 'i18n_unittest', username: 'root', password: '', host: 'localhost'
29
+ ActiveRecord::Base.establish_connection(
30
+ adapter: 'mysql2',
31
+ database: 'i18n_unittest',
32
+ username: ENV.fetch('MYSQL_USER', 'root'),
33
+ password: ENV.fetch('MYSQL_PASSWORD', ''),
34
+ host: '127.0.0.1'
35
+ )
23
36
  else
24
- ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
37
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
25
38
  end
26
- ::ActiveRecord::Migration.verbose = false
27
- ::ActiveRecord::Schema.define(:version => 1) do
28
- create_table :translations, :force => true do |t|
39
+
40
+ ActiveRecord::Migration.verbose = false
41
+ ActiveRecord::Schema.define(version: 1) do
42
+ create_table :translations, force: true do |t|
43
+ t.string :scope
29
44
  t.string :locale
30
45
  t.string :key
31
46
  t.text :value
32
47
  t.text :interpolations
33
- t.boolean :is_proc, :default => false
48
+ t.boolean :is_proc, default: false
34
49
  end
35
- add_index :translations, [:locale, :key], :unique => true
50
+ add_index :translations, %i[scope locale key], unique: true
51
+ end
52
+
53
+ if ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes=)
54
+ ActiveRecord::Base.yaml_column_permitted_classes = [Symbol]
55
+ elsif ActiveRecord.respond_to?(:yaml_column_permitted_classes=)
56
+ ActiveRecord.yaml_column_permitted_classes = [Symbol]
36
57
  end
37
58
  end
38
59
 
39
60
  TEST_CASE = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
40
61
 
41
- class TEST_CASE
42
- alias :assert_raise :assert_raises
43
- alias :assert_not_equal :refute_equal
62
+ class TEST_CASE # rubocop:disable Naming/ClassAndModuleCamelCase
63
+ alias assert_raise assert_raises
64
+ alias assert_not_equal refute_equal
44
65
 
45
- def assert_nothing_raised(*args)
66
+ def assert_nothing_raised(*_args)
46
67
  yield
47
68
  end
48
69
  end
49
70
 
50
- class I18n::TestCase < TEST_CASE
51
- def setup
52
- I18n.enforce_available_locales = false
53
- I18n.available_locales = []
54
- I18n.locale = :en
55
- I18n.default_locale = :en
56
- I18n.load_path = []
57
- super
58
- end
59
-
60
- def teardown
61
- I18n.enforce_available_locales = false
62
- I18n.available_locales = []
63
- I18n.locale = :en
64
- I18n.default_locale = :en
65
- I18n.load_path = []
66
- I18n.backend = nil
67
- super
68
- end
71
+ module I18n
72
+ class TestCase < TEST_CASE
73
+ def setup
74
+ I18n.enforce_available_locales = false
75
+ I18n.available_locales = []
76
+ I18n.locale = :en
77
+ I18n.default_locale = :en
78
+ I18n.load_path = []
79
+ super
80
+ end
69
81
 
70
- def translations
71
- I18n.backend.instance_variable_get(:@translations)
72
- end
82
+ def teardown
83
+ I18n.enforce_available_locales = false
84
+ I18n.available_locales = []
85
+ I18n.locale = :en
86
+ I18n.default_locale = :en
87
+ I18n.load_path = []
88
+ I18n.backend = nil
89
+ super
90
+ end
73
91
 
74
- def store_translations(locale, data)
75
- I18n.backend.store_translations(locale, data)
76
- end
92
+ def store_translations(locale, data)
93
+ I18n.backend.store_translations(locale, data)
94
+ end
77
95
 
78
- def locales_dir
79
- File.dirname(__FILE__) + '/test_data/locales'
96
+ def locales_dir
97
+ "#{File.dirname(__FILE__)}/test_data/locales"
98
+ end
80
99
  end
81
100
  end