releaf-i18n_database 2.0.0 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::Configuration do
4
- subject{ described_class.new(translation_auto_creation: true,
5
- translation_auto_creation_patterns: [1, 2],
6
- translation_auto_creation_exclusion_patterns: [3, 4]) }
7
-
8
- it do
9
- is_expected.to have_attributes(translation_auto_creation: true)
10
- is_expected.to have_attributes(translation_auto_creation_patterns: [1, 2])
11
- is_expected.to have_attributes(translation_auto_creation_exclusion_patterns: [3, 4])
12
- end
13
- end
@@ -1,24 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::HumanizeMissingTranslations do
4
- describe ".call" do
5
- context "when key is present and exception is I18n::MissingTranslation" do
6
- it "humanizes missing translations" do
7
- expect(I18n.t("some.missing translation")).to eq("Missing translation")
8
- end
9
- end
10
-
11
- context "when key is not present and exception is I18n::MissingTranslation" do
12
- it "does not intercept it" do
13
- expect(I18n.t(nil)).to eq("translation missing: en.no key")
14
- end
15
- end
16
-
17
- context "when exception is not I18n::MissingTranslation" do
18
- it "does not intercept it" do
19
- allow(I18n::Backend::Transliterator).to receive(:get).and_raise(I18n::ArgumentError)
20
- expect{I18n.transliterate("error?")}.to raise_error(I18n::ArgumentError)
21
- end
22
- end
23
- end
24
- end
@@ -1,150 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::ParseSpreadsheetTranslations do
4
- let(:translation){ Releaf::I18nDatabase::I18nEntry.new }
5
- let(:fixture_path){ File.expand_path('../../../fixtures/translations_import.xlsx', __dir__) }
6
- subject{ described_class.new(file_path: fixture_path, extension: "xlsx") }
7
-
8
- describe "#call" do
9
- it "returns translations" do
10
- allow(subject).to receive(:translations).and_return("x")
11
- expect(subject.call).to eq("x")
12
- end
13
- end
14
-
15
- describe "#rows" do
16
- let(:spreadsheet){ Roo::Spreadsheet.open(fixture_path) }
17
-
18
- before do
19
- allow(subject).to receive(:spreadsheet).and_return(spreadsheet)
20
- end
21
-
22
- it "returns speadsheet casted to array" do
23
- allow(spreadsheet).to receive(:to_a).and_return([1, 2])
24
- expect(subject.rows).to eq([1, 2])
25
- end
26
-
27
- it "caches casted array" do
28
- expect(spreadsheet).to receive(:to_a).and_return([1, 2]).once
29
- subject.rows
30
- subject.rows
31
- end
32
- end
33
-
34
- describe "#data_rows" do
35
- it "returns all rows except first and one with empty first value (key)" do
36
- allow(subject).to receive(:rows).and_return([[1, 2, 3], [:a, 2, 3], [nil, 2, 4], ["", 4, 1], [6, 2, ""]])
37
- expect(subject.data_rows).to eq([[:a, 2, 3], [6, 2, ""]])
38
- end
39
- end
40
-
41
- describe "#locales" do
42
- it "returns all non blank values from first row" do
43
- allow(subject).to receive(:rows).and_return([["lv", "", "de", nil, "en"], [:a, 2, 3], [6, 2, ""]])
44
- expect(subject.locales).to eq(["lv", "de", "en"])
45
- end
46
-
47
- it "caches resolved values" do
48
- expect(subject).to receive(:rows).and_return([["lv", "", "de", nil, "en"], [:a, 2, 3], [6, 2, ""]]).once
49
- subject.locales
50
- subject.locales
51
- end
52
- end
53
-
54
- describe "#spreadsheet" do
55
- it "returns Roo spreadsheet instance" do
56
- allow(Roo::Spreadsheet).to receive(:open).with(fixture_path, extension: "xlsx", file_warning: :ignore).and_return("instance")
57
- expect(subject.spreadsheet).to eq("instance")
58
- end
59
-
60
-
61
- context "when opening the file raises an unsupported content error" do
62
- it "raises Releaf::TranslationsImporter::UnsupportedFileFormatError" do
63
- error = ArgumentError.new("error message")
64
- allow(Roo::Spreadsheet).to receive(:open).and_raise( error )
65
- expect(subject).to receive(:file_format_error?).with( "ArgumentError", "error message").and_return true
66
- expect{ subject.spreadsheet }.to raise_error(described_class::UnsupportedFileFormatError)
67
- end
68
- end
69
-
70
- context "when opening the file raises any other error" do
71
- it "raises Releaf::TranslationsImporter::UnsupportedFileFormatError" do
72
- error = ArgumentError.new("error message")
73
- allow(Roo::Spreadsheet).to receive(:open).and_raise( error )
74
- expect(subject).to receive(:file_format_error?).with( "ArgumentError", "error message").and_return false
75
- expect{ subject.spreadsheet }.to raise_error( error )
76
- end
77
- end
78
-
79
- end
80
-
81
- describe "#file_format_error?" do
82
-
83
- context "when given error is an ArgumentError" do
84
- context "when the message contains 'Can't detect the type'" do
85
- it "returns true" do
86
- expect(subject.file_format_error?("ArgumentError", "Can't detect the type")).to be true
87
- end
88
- end
89
- context "when the message is different" do
90
- it "returns false" do
91
- expect(subject.file_format_error?("ArgumentError", "error message")).to be false
92
- end
93
- end
94
- end
95
-
96
- context "when the error is a Zip::Error" do
97
- it "returns true" do
98
- expect(subject.file_format_error?("Zip::Error", "error message")).to be true
99
- end
100
- end
101
-
102
- context "when the error is a Ole::Storage::FormatError" do
103
- it "returns true" do
104
- expect(subject.file_format_error?("Ole::Storage::FormatError", "error message")).to be true
105
- end
106
- end
107
-
108
-
109
- end
110
-
111
-
112
- describe "#translations" do
113
- it "returns array of processed `Releaf::I18nDatabase::Translation` instances" do
114
- allow(subject).to receive(:data_rows).and_return([[1, 2, 3], [:a, nil, 3], [6, 2, ""]])
115
- allow(subject).to receive(:translation_instance).with(1, ["2", "3"]).and_return("t1")
116
- allow(subject).to receive(:translation_instance).with(:a, ["", "3"]).and_return("t2")
117
- allow(subject).to receive(:translation_instance).with(6, ["2", ""]).and_return("t3")
118
- expect(subject.translations).to eq(["t1", "t2", "t3"])
119
- end
120
- end
121
-
122
- describe "#translation_instance" do
123
- it "returns first or initialized translation for given key and given localizations built" do
124
- where_scope = Releaf::I18nDatabase::I18nEntry.where(key: ["sommmmquery"])
125
- allow(Releaf::I18nDatabase::I18nEntry).to receive(:where).with(key: "as.ld").and_return(where_scope)
126
- allow(where_scope).to receive(:first_or_initialize).and_return(translation)
127
-
128
- expect(subject).to receive(:maintain_translation_locales).with(translation, ["x", "y"])
129
- expect(subject.translation_instance("as.ld", ["x", "y"])).to eq(translation)
130
- end
131
- end
132
-
133
- describe "#maintain_translation_locales" do
134
- before do
135
- allow(subject).to receive(:locales).and_return(["en", "de", "ge", "lv"])
136
- translation.i18n_entry_translation.build(locale: "de", text: "po")
137
- translation.i18n_entry_translation.build(locale: "ge", text: "x")
138
- end
139
-
140
- it "builds translation data for all missing locales" do
141
- expect{ subject.maintain_translation_locales(translation, ["ent", "det", "get", "lvt"]) }
142
- .to change{ translation.i18n_entry_translation.map{|td| td.locale } }.from(["de", "ge"]).to(["de", "ge", "en", "lv"])
143
- end
144
-
145
- it "overwrites existing translation data localizations only if new localizations is not empty" do
146
- expect{ subject.maintain_translation_locales(translation, ["ent", "", "get", "lvt"]) }
147
- .to change{ translation.i18n_entry_translation.map{|td| [td.locale, td.text] } }.to([["de", "po"], ["ge", "get"], ["en", "ent"], ["lv", "lvt"]])
148
- end
149
- end
150
- end
@@ -1,530 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::TranslationsStore do
4
- describe "#initialize" do
5
- it "assigns updated at from `Releaf::I18nDatabase::Backend` last updated at timestamp" do
6
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return("x")
7
- expect(subject.updated_at).to eq("x")
8
- end
9
-
10
- it "assigns empty hash to missing keys" do
11
- expect(subject.missing_keys).to eq({})
12
- end
13
- end
14
-
15
- describe "#config" do
16
- it "returns Releaf.application.config" do
17
- allow(Releaf.application).to receive(:config).and_return(:x)
18
- expect(subject.config).to eq(:x)
19
- end
20
- end
21
-
22
- describe "#expired?" do
23
- context "when last translation update differs from last cache load" do
24
- it "returns true" do
25
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return(1)
26
- subject.updated_at = 2
27
- expect(subject.expired?).to be true
28
- end
29
- end
30
-
31
- context "when last translation update does not differ from last cache load" do
32
- it "returns false" do
33
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return(1)
34
- subject.updated_at = 1
35
- expect(subject.expired?).to be false
36
- end
37
- end
38
- end
39
-
40
- describe "#exist?" do
41
- context "when given key exists within stored keys hash" do
42
- it "returns true" do
43
- allow(subject).to receive(:stored_keys).and_return("asd.xxx" => true)
44
- expect(subject.exist?("asd.xxx")).to be true
45
- end
46
- end
47
-
48
- context "when given key does not exist within keys hash" do
49
- it "returns false" do
50
- allow(subject).to receive(:stored_keys).and_return("asd.xxyy" => true)
51
- expect(subject.exist?("asd.xxx")).to be false
52
- end
53
- end
54
- end
55
-
56
- describe "#lookup" do
57
- it "returns first valid and returnable result" do
58
- allow(subject).to receive(:dig_valid_translation)
59
- .with(:ru, ["admin", "menu", "translations"], true, count: 23).and_return("x1")
60
- allow(subject).to receive(:returnable_result?)
61
- .with("x1", count: 23).and_return(false)
62
-
63
- allow(subject).to receive(:dig_valid_translation)
64
- .with(:ru, ["admin", "translations"], false, count: 23).and_return("x2")
65
- allow(subject).to receive(:returnable_result?)
66
- .with("x2", count: 23).and_return(true)
67
-
68
- expect(subject.lookup(:ru, "admin.menu.translations", count: 23)).to eq("x2")
69
- end
70
-
71
- it "iterates throught all translation scopes until last key" do
72
- expect(subject).to receive(:dig_valid_translation)
73
- .with(:ru, ["admin", "menu", "another", "translations"], true, count: 23).and_return(nil)
74
- expect(subject).to receive(:dig_valid_translation)
75
- .with(:ru, ["admin", "menu", "translations"], false, count: 23).and_return(nil)
76
- expect(subject).to receive(:dig_valid_translation)
77
- .with(:ru, ["admin", "translations"], false, count: 23).and_return(nil)
78
- expect(subject).to receive(:dig_valid_translation)
79
- .with(:ru, ["translations"], false, count: 23).and_return("x1")
80
-
81
- subject.lookup(:ru, "admin.menu.another.translations", count: 23)
82
- end
83
-
84
- context "when no matches found" do
85
- it "returns nil" do
86
- allow(subject).to receive(:dig_valid_translation).and_return(nil)
87
- expect(subject.lookup(:ru, "admin.menu.translations", count: 23)).to be nil
88
- end
89
- end
90
- end
91
-
92
- describe "#dig_translation" do
93
- before do
94
- allow(subject).to receive(:stored_translations).and_return(
95
- lv: {
96
- admin: {
97
- some_scope: {
98
- save: "Saglabāt"
99
- }
100
- }
101
- },
102
- lt: {
103
- public: {
104
- another_scope: {
105
- email: "E-pastas"
106
- }
107
- }
108
- }
109
- )
110
- end
111
-
112
- it "dig given locale prefixed keys hash against stored translations and returns matched value (String or Hash)" do
113
- expect(subject.dig_translation(:lv, [:admin, :some_scope, :save])).to eq("Saglabāt")
114
- expect(subject.dig_translation(:lt, [:public, :another_scope, :email])).to eq("E-pastas")
115
- expect(subject.dig_translation(:lt, [:public, :another_scope])).to eq(email: "E-pastas")
116
- end
117
-
118
- context "when no match found" do
119
- it "returns nil" do
120
- expect(subject.dig_translation(:lt, [:admin, :some_scope, :save])).to be nil
121
- expect(subject.dig_translation(:lt, [:public, :email])).to be nil
122
- expect(subject.dig_translation(:ge, [:public, :email])).to be nil
123
- end
124
- end
125
- end
126
-
127
- describe "#dig_valid_translation" do
128
- before do
129
- allow(subject).to receive(:dig_translation).with(:de, [:admin, :save]).and_return("translated_value")
130
- end
131
-
132
- context "when digged translation has valid result" do
133
- it "returns digged translation result" do
134
- allow(subject).to receive(:invalid_result?).with(:de, "translated_value", true, a: :b).and_return(false)
135
- expect(subject.dig_valid_translation(:de, [:admin, :save], true, a: :b)).to eq("translated_value")
136
- end
137
- end
138
-
139
- context "when digged translation has invalid result" do
140
- it "returns nil" do
141
- allow(subject).to receive(:invalid_result?).with(:de, "translated_value", true, a: :b).and_return(true)
142
- expect(subject.dig_valid_translation(:de, [:admin, :save], true, a: :b)).to be nil
143
- end
144
- end
145
- end
146
-
147
- describe "#invalid_result?" do
148
- before do
149
- allow(subject).to receive(:invalid_nonpluralized_result?).with("xx", true, a: :b).and_return(false)
150
- allow(subject).to receive(:invalid_pluralized_result?).with(:ge, "xx", a: :b).and_return(false)
151
- end
152
-
153
- context "when invalid nonplurarized result" do
154
- it "returns true" do
155
- allow(subject).to receive(:invalid_nonpluralized_result?).with("xx", true, a: :b).and_return(true)
156
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be true
157
- end
158
- end
159
-
160
- context "when invalid plurarized result" do
161
- it "returns true" do
162
- allow(subject).to receive(:invalid_pluralized_result?).with(:ge, "xx", a: :b).and_return(true)
163
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be true
164
- end
165
- end
166
-
167
- context "when no invalid nonplurarized or pluralized result" do
168
- it "returns false" do
169
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be false
170
- end
171
- end
172
- end
173
-
174
- describe "#invalid_nonpluralized_result?" do
175
- context "when hash result, not first lookup and options without count" do
176
- it "returns true" do
177
- expect(subject.invalid_nonpluralized_result?({a: :b}, false, scope: "xxx")).to be true
178
- end
179
- end
180
-
181
- context "when hash result, first lookup and options without count" do
182
- it "returns false" do
183
- expect(subject.invalid_nonpluralized_result?({a: :b}, true, scope: "xxx")).to be false
184
- end
185
- end
186
-
187
- context "when hash result, not first lookup and options with count" do
188
- it "returns false" do
189
- expect(subject.invalid_nonpluralized_result?({a: :b}, false, count: 43, scope: "xxx")).to be false
190
- end
191
- end
192
-
193
- context "when non hash result, not first lookup and options without count" do
194
- it "returns false" do
195
- expect(subject.invalid_nonpluralized_result?("x", false, scope: "xxx")).to be false
196
- end
197
- end
198
- end
199
-
200
- describe "#invalid_pluralized_result?" do
201
- before do
202
- allow(subject).to receive(:valid_pluralized_result?).with(:ge, 12, a: :b).and_return(false)
203
- end
204
-
205
- context "when hash result, options has count and invalid pluralized result" do
206
- it "returns true" do
207
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {count: 12})).to be true
208
- end
209
- end
210
-
211
- context "when non hash result, options has count and invalid pluralized result" do
212
- it "returns false" do
213
- expect(subject.invalid_pluralized_result?(:ge, "X", {count: 12})).to be false
214
- end
215
- end
216
-
217
- context "when hash result, options has no count value and invalid pluralized result" do
218
- it "returns false" do
219
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {scope: "xx.xx"})).to be false
220
- end
221
- end
222
-
223
- context "when hash result, options has count and valid pluralized result" do
224
- it "returns false" do
225
- allow(subject).to receive(:valid_pluralized_result?).with(:ge, 12, a: :b).and_return(true)
226
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {count: 12})).to be false
227
- end
228
- end
229
- end
230
-
231
- describe "#valid_pluralized_result?" do
232
- context "when given hash contains valid result for given locale and count" do
233
- it "return true" do
234
- expect(subject.valid_pluralized_result?(:lv, 2, one: "x", other: "y")).to be true
235
- end
236
- end
237
-
238
- context "when given hash contains invalid result for given locale and count" do
239
- it "return false" do
240
- expect(subject.valid_pluralized_result?(:lv, 2, one: "x", few: "y")).to be false
241
- end
242
- end
243
- end
244
-
245
- describe "#returnable_result?" do
246
- context "when given result is not blank" do
247
- it "returns true" do
248
- expect(subject.returnable_result?("x", a: "b")).to be true
249
- end
250
- end
251
-
252
- context "when `inherit_scopes` option value is boolean `false`" do
253
- it "returns true" do
254
- expect(subject.returnable_result?(nil, inherit_scopes: false)).to be true
255
- end
256
- end
257
-
258
- context "when given result is blank and `inherit_scopes` option value is not boolean `false`" do
259
- it "returns false" do
260
- expect(subject.returnable_result?(nil, inherit_scopes: true)).to be false
261
- expect(subject.returnable_result?(nil, a: "b")).to be false
262
- end
263
- end
264
- end
265
-
266
- describe "#localization_data" do
267
- it "returns hash with all non empty translations with locale prefixed key and localization as value" do
268
- i18n_entry_1 = Releaf::I18nDatabase::I18nEntry.create(key: "some.food")
269
- i18n_entry_1.i18n_entry_translation.create(locale: "lv", text: "suņi")
270
- i18n_entry_1.i18n_entry_translation.create(locale: "en", text: "dogs")
271
-
272
- i18n_entry_2 = Releaf::I18nDatabase::I18nEntry.create(key: "some.good")
273
- i18n_entry_2.i18n_entry_translation.create(locale: "lv", text: "xx")
274
- i18n_entry_2.i18n_entry_translation.create(locale: "en", text: "")
275
-
276
- expect(subject.localization_data).to eq("lv.some.food" => "suņi", "en.some.food" => "dogs",
277
- "lv.some.good" => "xx")
278
- end
279
-
280
- it "has cached method result" do
281
- expect(described_class).to cache_instance_method(:localization_data)
282
- end
283
- end
284
-
285
- describe "#stored_keys" do
286
- it "returns hash with existing translation keys" do
287
- Releaf::I18nDatabase::I18nEntry.create(key: "some.food")
288
- Releaf::I18nDatabase::I18nEntry.create(key: "some.good")
289
- expect(subject.stored_keys).to eq("some.food" => true, "some.good" => true)
290
- end
291
-
292
- it "has cached method result" do
293
- expect(described_class).to cache_instance_method(:stored_keys)
294
- end
295
- end
296
-
297
- describe "#stored_translations" do
298
- it "returns deep merged hash from all key translation hashes" do
299
- allow(subject).to receive(:stored_keys).and_return("some.food" => true, "some.good" => true)
300
- allow(subject).to receive(:key_hash).with("some.food").and_return(lv: {a: "a1"}, en: {d: {e: "ee"}})
301
- allow(subject).to receive(:key_hash).with("some.good").and_return(lv: {b: "b2"}, en: {d: {u: "ll"}})
302
- expect(subject.stored_translations).to eq(lv: {a: "a1", b: "b2"}, en: {d: {e: "ee", u: "ll"}})
303
- end
304
-
305
- context "when no keys exists" do
306
- it "returns empty hash" do
307
- allow(subject).to receive(:stored_keys).and_return({})
308
- expect(subject.stored_translations).to eq({})
309
- end
310
- end
311
-
312
- it "has cached method result" do
313
- expect(described_class).to cache_instance_method(:stored_translations)
314
- end
315
- end
316
-
317
- describe "#key_hash" do
318
- it "build stored translation hash with keys and translated values for given key" do
319
- allow(subject.config).to receive(:all_locales).and_return([:ge, :de])
320
- allow(subject).to receive(:localization_data).and_return(
321
- "lv.admin.releaf_i18n_database_translations.Save" => "zc",
322
- "ge.admin.Save" => "asdasd",
323
- "de.admin.releaf_i18n_database_translations.Save" => "Seiv",
324
- "de.admin.releaf_i18n_database_translations.Cancel" => "dCancel",
325
- "ge.admin.releaf_i18n_database_translations.Cancel" => "gCancel",
326
- )
327
-
328
- expect(subject.key_hash("admin.releaf_i18n_database_translations.Save")).to eq(
329
- ge: {
330
- admin: {
331
- releaf_i18n_database_translations: {
332
- Save: nil
333
- }
334
- }
335
- },
336
- de: {
337
- admin: {
338
- releaf_i18n_database_translations: {
339
- Save: "Seiv"
340
- }
341
- }
342
- }
343
- )
344
-
345
- expect(subject.key_hash("admin.releaf_i18n_database_translations.Cancel")).to eq(
346
- ge: {
347
- admin: {
348
- releaf_i18n_database_translations: {
349
- Cancel: "gCancel"
350
- }
351
- }
352
- },
353
- de: {
354
- admin: {
355
- releaf_i18n_database_translations: {
356
- Cancel: "dCancel"
357
- }
358
- }
359
- }
360
- )
361
- end
362
- end
363
-
364
- describe "#missing?" do
365
- context "when given locale and key combination exists within missing keys hash" do
366
- it "returns true" do
367
- allow(subject).to receive(:missing_keys).and_return("lv.asd.xxx" => true)
368
- expect(subject.missing?(:lv, "asd.xxx")).to be true
369
- end
370
- end
371
-
372
- context "when given locale and key combination does not exist missing keys hash" do
373
- it "returns false" do
374
- allow(subject).to receive(:missing_keys).and_return("en.asd.xxx" => true)
375
- expect(subject.missing?(:lv, "asd.xxx")).to be false
376
- end
377
- end
378
- end
379
-
380
- describe "#missing" do
381
- it "adds given locale and key combination as missing" do
382
- expect{ subject.missing(:de, "as.pasd", a: "x") }.to change { subject.missing_keys["de.as.pasd"] }
383
- .from(nil).to(true)
384
- end
385
-
386
- context "when missing translation creation is available for given key and options" do
387
- it "creates missing translation" do
388
- allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(true)
389
- expect(subject).to receive(:auto_create).with("ps.asda", a: "x")
390
- subject.missing(:de, "ps.asda", a: "x")
391
- end
392
- end
393
-
394
- context "when missing translation creation is not available for given key and options" do
395
- it "does not create missing translation" do
396
- allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(false)
397
- expect(subject).to_not receive(:auto_create)
398
- subject.missing(:de, "ps.asda", a: "x")
399
- end
400
- end
401
- end
402
-
403
- describe "#auto_create?" do
404
- before do
405
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation).and_return(true)
406
- allow(subject).to receive(:stored_keys).and_return("xxxome.save" => "xxxome.save")
407
- allow(subject).to receive(:auto_creation_inclusion?).with("some.save").and_return(true)
408
- allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(false)
409
- end
410
-
411
- context "when missing translation creation is enabled globally by i18n config and not disabled by `auto_create` option" do
412
- it "returns true" do
413
- expect(subject.auto_create?("some.save", {})).to be true
414
- expect(subject.auto_create?("some.save", auto_create: true)).to be true
415
- expect(subject.auto_create?("some.save", auto_create: nil)).to be true
416
- end
417
- end
418
-
419
- context "when no auto creation inclusion" do
420
- it "returns false" do
421
- allow(subject).to receive(:auto_creation_inclusion?).with("some.save").and_return(false)
422
- expect(subject.auto_create?("some.save", {})).to be false
423
- end
424
- end
425
-
426
- context "when auto creation exception" do
427
- it "returns false" do
428
- allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(true)
429
- expect(subject.auto_create?("some.save", {})).to be false
430
- end
431
- end
432
-
433
- context "when missing translation creation is disabled globally by i18n config" do
434
- it "returns false" do
435
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation).and_return(false)
436
- expect(subject.auto_create?("some.save", {})).to be false
437
- end
438
- end
439
-
440
- context "when missing translation creation is disabled by `auto_create` option" do
441
- it "returns false" do
442
- expect(subject.auto_create?("some.save", auto_create: false)).to be false
443
- end
444
- end
445
-
446
- context "when key already exists within stored keys hash" do
447
- it "returns false" do
448
- allow(subject).to receive(:stored_keys).and_return("some.save" => "some.save")
449
- expect(subject.auto_create?("some.save", {})).to be false
450
- end
451
- end
452
- end
453
-
454
- describe "#auto_creation_inclusion?" do
455
- context "when given key matches any auto creation pattern" do
456
- it "returns true" do
457
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_patterns).and_return([/^another\./, /^some\./])
458
- expect(subject.auto_creation_inclusion?("some.save")).to be true
459
- end
460
- end
461
-
462
- context "when given key matches no auto creation pattern" do
463
- it "returns false" do
464
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_patterns).and_return([/^another\./, /^foo\./])
465
- expect(subject.auto_creation_inclusion?("some.save")).to be false
466
- end
467
- end
468
- end
469
-
470
- describe "#auto_creation_exception?" do
471
- context "when given key matches any auto creation exception pattern" do
472
- it "returns true" do
473
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_exclusion_patterns).and_return([/^another\./, /^some\./])
474
- expect(subject.auto_creation_exception?("some.save")).to be true
475
- end
476
- end
477
-
478
- context "when given key matches no auto creation exception pattern" do
479
- it "returns false" do
480
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_exclusion_patterns).and_return([/^another\./, /^foo\./])
481
- expect(subject.auto_creation_exception?("some.save")).to be false
482
- end
483
- end
484
- end
485
-
486
- describe "#auto_create" do
487
- before do
488
- allow(Releaf::I18nDatabase::Backend).to receive(:locales_pluralizations).and_return([:one, :many, :other])
489
- end
490
-
491
- context "when pluralizable translation given" do
492
- it "creates translation for each pluralization form" do
493
- allow(subject).to receive(:pluralizable_translation?).with(a: "b").and_return(true)
494
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.one")
495
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.many")
496
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.other")
497
- subject.auto_create("aasd.oihgja.sd", a: "b")
498
- end
499
- end
500
-
501
- context "when non pluralizable translation given" do
502
- it "creates translation" do
503
- allow(subject).to receive(:pluralizable_translation?).with(a: "b").and_return(false)
504
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd")
505
- subject.auto_create("aasd.oihgja.sd", a: "b")
506
- end
507
- end
508
- end
509
-
510
- describe "#pluralizable_translation?" do
511
- context "when given options has count key and `create_plurals` key is true" do
512
- it "returns true" do
513
- expect(subject.pluralizable_translation?(count: 3, create_plurals: true)).to be true
514
- end
515
- end
516
-
517
- context "when given options has count key and `create_plurals` key is not true" do
518
- it "returns false" do
519
- expect(subject.pluralizable_translation?(count: 3, create_plurals: false)).to be false
520
- expect(subject.pluralizable_translation?(count: 3)).to be false
521
- end
522
- end
523
-
524
- context "when given options has no count key and `create_plurals` key is true" do
525
- it "returns false" do
526
- expect(subject.pluralizable_translation?(create_plurals: true)).to be false
527
- end
528
- end
529
- end
530
- end