traco 5.3.3 → 6.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f574ca00b4a00cb29c1b6222d4b1afd7ce8e2a84561cd2eba142c3dc7ad9e3e5
4
- data.tar.gz: ff1a6327086f994627df877d57b46a6c16389315e2183406f07ca6215b376aef
3
+ metadata.gz: 5de70b4e4831449104ded9af0acc78de7225a0cf0ae44e6f6559d913cbf0a2e0
4
+ data.tar.gz: 2db12568f7e0901facaade861013fd92cef6894f8c536ad1bcef1b9a887af1fe
5
5
  SHA512:
6
- metadata.gz: 1b54c9864d9d378f5fdbcf9ff8952413dfbee488effedccf5485cdcd3b3c34a12e0798a5d46a6b9e9a0e40dbabc5e089af14ccf4d6c83278d98bb23dd31e63e8
7
- data.tar.gz: 4c281b250b2065bf0d62754be45cc4efa873b859f1d666b1bd6741420dda3d29d756d7155869ea347a47e7fb2bc0d73314b8fed0d9585f63b0ed15cc9048e58f
6
+ metadata.gz: '095d8a4db69e3b98fc74fc2ae3e5c8fc1f3606a671baf96ee597a23de3d514e39090f4baceabc9cc4de824a0e02c1a7a95b2bb892956ca98698068eb35ee2b5c'
7
+ data.tar.gz: c4d7ae492cab31e788840689306ef572a620872398d071a076d6010a1726da3be32bd7d5e20f9f96d57fc2d3409dc8bb38d4ab2e2242a44cde4be684165153ab
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.0.0
4
+
5
+ * Return e.g. `{}` rather than `nil` for falsey values that are not the empty string.
6
+
3
7
  ## 5.3.3
4
8
 
5
9
  * Force MFA for privileged Rubygems actions.
@@ -28,12 +28,14 @@ module Traco
28
28
  return send(Traco.column(:#{attribute}, locale)) if locale
29
29
 
30
30
  columns_to_try = self.class._locale_columns_for_attribute(:#{attribute}, fallback: fallback)
31
+ value = nil
31
32
  columns_to_try.each do |column|
32
33
  value = send(column)
33
34
  return value if value.present?
34
35
  end
35
36
 
36
- nil
37
+ # With no present value in any locale, return nil for strings, or the last seen blank value (e.g. {} for JSON columns with that default).
38
+ value.is_a?(String) ? nil : value
37
39
  end
38
40
  EOM
39
41
  end
data/lib/traco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Traco
2
- VERSION = "5.3.3"
2
+ VERSION = "6.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traco
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.3.3
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-11-19 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -24,63 +23,6 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '4.2'
27
- - !ruby/object:Gem::Dependency
28
- name: sqlite3
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: appraisal
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- description:
84
26
  email:
85
27
  - henrik@barsoom.se
86
28
  executables: []
@@ -95,18 +37,11 @@ files:
95
37
  - lib/traco/locale_fallbacks.rb
96
38
  - lib/traco/translates.rb
97
39
  - lib/traco/version.rb
98
- - spec/app/post.rb
99
- - spec/app/sv.yml
100
- - spec/locale_fallbacks_spec.rb
101
- - spec/spec_helper.rb
102
- - spec/spec_helper_models.rb
103
- - spec/traco_spec.rb
104
40
  homepage: ''
105
41
  licenses:
106
42
  - MIT
107
43
  metadata:
108
44
  rubygems_mfa_required: 'true'
109
- post_install_message:
110
45
  rdoc_options: []
111
46
  require_paths:
112
47
  - lib
@@ -121,14 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
56
  - !ruby/object:Gem::Version
122
57
  version: '0'
123
58
  requirements: []
124
- rubygems_version: 3.2.31
125
- signing_key:
59
+ rubygems_version: 3.6.9
126
60
  specification_version: 4
127
61
  summary: Translatable columns for Rails 4.2 or better, stored in the model table itself.
128
- test_files:
129
- - spec/app/post.rb
130
- - spec/app/sv.yml
131
- - spec/locale_fallbacks_spec.rb
132
- - spec/spec_helper.rb
133
- - spec/spec_helper_models.rb
134
- - spec/traco_spec.rb
62
+ test_files: []
data/spec/app/post.rb DELETED
@@ -1,5 +0,0 @@
1
- class Post < ActiveRecord::Base
2
- end
3
-
4
- class SubPost < Post
5
- end
data/spec/app/sv.yml DELETED
@@ -1,7 +0,0 @@
1
- sv:
2
- attributes:
3
- title_sv: "Svensk titel"
4
- title: "Titel"
5
- i18n:
6
- languages:
7
- en: "engelska"
@@ -1,69 +0,0 @@
1
- require "traco/locale_fallbacks"
2
-
3
- RSpec.describe Traco::LocaleFallbacks do
4
- describe ".new" do
5
- it "raises ArgumentError if an unknown argument passed in" do
6
- expect { Traco::LocaleFallbacks.new(:foo) }.to raise_error(ArgumentError)
7
- expect { Traco::LocaleFallbacks.new(nil) }.to raise_error(ArgumentError)
8
- end
9
- end
10
-
11
- describe "#[]" do
12
- context "with the ':default' option" do
13
- it "returns given locale, then default locale" do
14
- I18n.default_locale = :en
15
- subject = Traco::LocaleFallbacks.new(:default)
16
- expect(subject[:sv]).to eq [ :sv, :en ]
17
- end
18
- end
19
-
20
- context "with the ':any' option" do
21
- it "returns given locale, then default locale, then remaining available locales alphabetically" do
22
- I18n.default_locale = :en
23
- I18n.available_locales = [ :en, :sv, :uk, :de ]
24
-
25
- subject = Traco::LocaleFallbacks.new(:any)
26
- expect(subject[:sv]).to eq [ :sv, :en, :de, :uk ]
27
- end
28
- end
29
-
30
- context "with the 'false' option" do
31
- it "returns only given locale" do
32
- subject = Traco::LocaleFallbacks.new(false)
33
- expect(subject[:sv]).to eq [ :sv ]
34
- end
35
- end
36
-
37
- context "with an explicit list of locales" do
38
- it "returns only those locales, after the current one" do
39
- subject = Traco::LocaleFallbacks.new([ :en ])
40
- expect(subject[:sv]).to eq [ :sv, :en ]
41
- end
42
- end
43
-
44
- context "with the ':default_first' option" do
45
- it "returns default locale, then remaining available locales alphabetically" do
46
- I18n.default_locale = :uk
47
- I18n.available_locales = [ :en, :sv, :uk, :de ]
48
-
49
- subject = Traco::LocaleFallbacks.new(:default_first)
50
- expect(subject[:sv]).to eq [ :uk, :de, :en, :sv ]
51
- end
52
- end
53
-
54
- context "with the ':i18n' option" do
55
- it "returns what is configured as a I18n fallback" do
56
- I18n.fallbacks = I18n::Locale::Fallbacks.new(
57
- en: [ :en, :uk, :de, :sv ],
58
- uk: [ :uk, :en, :de, :sv ],
59
- de: [ :de, :uk, :en, :sv ],
60
- sv: [ :sv, :en, :uk, :de ],
61
- )
62
-
63
- subject = Traco::LocaleFallbacks.new(:i18n)
64
- expect(subject[:sv]).to eq [ :sv, :en, :uk, :de ]
65
- expect(subject[:de]).to eq [ :de, :uk, :en, :sv ]
66
- end
67
- end
68
- end
69
- end
data/spec/spec_helper.rb DELETED
@@ -1,11 +0,0 @@
1
- require "i18n"
2
-
3
- RSpec.configure do |config|
4
- config.filter_run focus: true
5
- config.run_all_when_everything_filtered = true
6
- config.disable_monkey_patching!
7
- end
8
-
9
- I18n.enforce_available_locales = false
10
-
11
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
@@ -1,29 +0,0 @@
1
- RSpec.configure do |config|
2
- config.before do
3
- # Clear class state before each spec.
4
- Object.send(:remove_const, "Post")
5
- Object.send(:remove_const, "SubPost")
6
- load "app/post.rb"
7
-
8
- # Known state.
9
- I18n.default_locale = :sv
10
- end
11
- end
12
-
13
- # Test against real ActiveRecord models.
14
- # Very much based on the test setup in
15
- # https://github.com/iain/translatable_columns/
16
-
17
- require "active_record"
18
- require "app/post.rb"
19
-
20
- ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
21
-
22
- ActiveRecord::Schema.define(version: 0) do
23
- create_table :posts, force: true do |t|
24
- t.string :title_sv, :title_en, :title_pt_br, :title_de
25
- t.string :body_sv, :body_en, :body_pt_br, :body_de
26
- end
27
- end
28
-
29
- I18n.load_path << "spec/app/sv.yml"
data/spec/traco_spec.rb DELETED
@@ -1,384 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper_models"
4
- require "traco"
5
-
6
- RSpec.describe Traco, ".split_localized_column" do
7
- subject { described_class }
8
-
9
- it "returns attribute and locale" do
10
- expect(subject.split_localized_column("title_sv")).to eq [ :title, :sv ]
11
- end
12
-
13
- it "handles normalized locales" do
14
- expect(subject.split_localized_column("title_pt_br")).to eq [ :title, :"pt-BR" ]
15
- end
16
-
17
- it "returns nil if column is not localized" do
18
- expect(subject.split_localized_column("title")).to be_nil
19
- end
20
-
21
- it "returns nil if column is not localized but with undercores" do
22
- expect(subject.split_localized_column("long_title")).to be_nil
23
- end
24
- end
25
-
26
- RSpec.describe ActiveRecord::Base, ".translates" do
27
- it "is available" do
28
- expect(Post).to respond_to :translates
29
- end
30
-
31
- it "adds functionality" do
32
- expect(Post.new).not_to respond_to :title
33
- Post.translates :title
34
- expect(Post.new).to respond_to :title
35
- end
36
-
37
- it "can be run more than once" do
38
- expect(Post.new).not_to respond_to :title, :body
39
- Post.translates :title
40
- Post.translates :body
41
- expect(Post.new).to respond_to :title, :body
42
- end
43
-
44
- it "allows to define several attributes at once" do
45
- expect(Post.new).not_to respond_to :title, :body
46
- Post.translates :title, :body
47
- expect(Post.new).to respond_to :title, :body
48
- end
49
-
50
- it "inherits columns from the superclass" do
51
- Post.translates :title
52
- SubPost.translates :body
53
- expect(SubPost.new).to respond_to :title, :body
54
- expect(Post.new).to respond_to :title
55
- expect(Post.new).not_to respond_to :body
56
- end
57
- end
58
-
59
- RSpec.describe Post, ".translatable_attributes" do
60
- before do
61
- Post.translates :title
62
- end
63
-
64
- it "lists the translatable attributes" do
65
- expect(Post.translatable_attributes).to eq [ :title ]
66
- end
67
-
68
- it "inherits attributes from superclass to a subclass" do
69
- SubPost.translates :body
70
- expect(SubPost.translatable_attributes).to eq [ :title, :body ]
71
- end
72
-
73
- it "doesn't inherit attributes from a subclass to superclass" do
74
- SubPost.translates :body
75
- expect(Post.translatable_attributes).to eq [ :title ]
76
- end
77
- end
78
-
79
- RSpec.describe Post, ".locales_for_attribute" do
80
- before do
81
- Post.translates :title
82
- I18n.default_locale = :"pt-BR"
83
- I18n.locale = :en
84
- end
85
-
86
- it "lists the locales for that attribute, current locale first, then default locale, and then alphabetically" do
87
- expect(Post.locales_for_attribute(:title)).to eq [ :en, :"pt-BR", :de, :sv ]
88
- end
89
-
90
- it "doesn't include a locale if there's no corresponding column for it" do
91
- I18n.available_locales += [ :ru ]
92
-
93
- expect(Post.locales_for_attribute(:title)).not_to include(:ru)
94
- end
95
- end
96
-
97
- RSpec.describe Post, ".locale_columns" do
98
- before do
99
- Post.translates :title
100
- I18n.default_locale = :"pt-BR"
101
- I18n.locale = :en
102
- end
103
-
104
- it "lists the columns-with-locale for that attribute, current locale first, then default locale, and then alphabetically" do
105
- expect(Post.locale_columns(:title)).to eq [
106
- :title_en, :title_pt_br, :title_de, :title_sv,
107
- ]
108
- end
109
-
110
- it "doesn't include a column-with-locale if it doesn't exist" do
111
- I18n.available_locales += [ :ru ]
112
-
113
- expect(Post.locale_columns(:title)).not_to include(:title_ru)
114
- end
115
-
116
- it "supports multiple attributes" do
117
- Post.translates :body
118
-
119
- expect(Post.locale_columns(:body, :title)).to eq [
120
- :body_en, :body_pt_br, :body_de, :body_sv,
121
- :title_en, :title_pt_br, :title_de, :title_sv,
122
- ]
123
- end
124
-
125
- it "returns the columns of all translated attributes if no params are supplied" do
126
- expect(Post.locale_columns).to eq [
127
- :title_en, :title_pt_br, :title_de, :title_sv,
128
- ]
129
- end
130
- end
131
-
132
- RSpec.describe Post, ".current_locale_column" do
133
- before do
134
- Post.translates :title
135
- end
136
-
137
- it "returns the column name for the current locale" do
138
- I18n.locale = :sv
139
- expect(Post.current_locale_column(:title)).to eq :title_sv
140
- end
141
-
142
- it "returns the column name with right format for the current locale with region identifier" do
143
- I18n.locale = :"pt-BR"
144
- expect(Post.current_locale_column(:title)).to eq :title_pt_br
145
- end
146
- end
147
-
148
- RSpec.describe Post, "#title" do
149
- let(:post) {
150
- Post.new(title_sv: "Hej", title_en: "Halloa", title_pt_br: "Olá")
151
- }
152
-
153
- before do
154
- Post.translates :title
155
- I18n.locale = :sv
156
- I18n.default_locale = :en
157
- end
158
-
159
- it "gives the title in the current locale" do
160
- expect(post.title).to eq "Hej"
161
- end
162
-
163
- it "handles dashed locales" do
164
- I18n.locale = :"pt-BR"
165
- expect(post.title_pt_br).to eq "Olá"
166
- expect(post.title).to eq "Olá"
167
- end
168
-
169
- it "falls back to the default locale if locale has no column" do
170
- I18n.locale = :ru
171
- expect(post.title).to eq "Halloa"
172
- end
173
-
174
- it "falls back to the default locale if blank" do
175
- post.title_sv = " "
176
- expect(post.title).to eq "Halloa"
177
- end
178
-
179
- it "does not fall back to any other locale if default locale is blank" do
180
- post.title_sv = " "
181
- post.title_en = ""
182
- expect(post.title).to be_nil
183
- end
184
-
185
- it "does not fall back if called with fallback: false" do
186
- I18n.locale = :sv
187
- post.title_sv = ""
188
- expect(post.title(fallback: false)).to be_nil
189
- end
190
-
191
- # Had a bug: https://github.com/barsoom/traco/issues/25
192
- it "does not incorrectly memoize fallback state" do
193
- I18n.locale = :sv
194
- post.title_sv = ""
195
- expect(post.title(fallback: :any)).not_to be_nil
196
- expect(post.title(fallback: false)).to be_nil
197
- end
198
-
199
- it "returns nil if all are blank" do
200
- post.title_sv = " "
201
- post.title_en = ""
202
- post.title_pt_br = nil
203
- expect(post.title).to be_nil
204
- end
205
-
206
- it "is overridable" do
207
- class Post
208
- def title
209
- super.reverse
210
- end
211
- end
212
-
213
- expect(post.title).to eq "jeH"
214
- end
215
-
216
- # Had a regression.
217
- it "handles multiple columns" do
218
- Post.translates :title, :body
219
- post.title_sv = "title"
220
- post.body_sv = "body"
221
- expect(post.title).to eq "title"
222
- expect(post.body).to eq "body"
223
- end
224
-
225
- it "reflects locale change" do
226
- expect(post.title).to eq "Hej"
227
- I18n.locale = :en
228
- expect(post.title).to eq "Halloa"
229
- I18n.locale = :sv
230
- expect(post.title).to eq "Hej"
231
- end
232
-
233
- context "when given a 'locale' argument" do
234
- it "gives the title in that locale, without fallback" do
235
- post = Post.new(title_sv: "Hej", title_en: "Halloa")
236
- I18n.default_locale = :en
237
- I18n.locale = :en
238
-
239
- expect(post.title(locale: :sv)).to eq "Hej"
240
-
241
- post.title_sv = nil
242
- expect(post.title(locale: :sv)).to eq nil
243
- end
244
- end
245
-
246
- context "when the translation was defined with fallback: false" do
247
- let(:post) {
248
- Post.new(title_sv: "Hej", title_en: "Halloa")
249
- }
250
-
251
- before do
252
- Post.translates :title, fallback: false
253
- I18n.default_locale = :en
254
- end
255
-
256
- it "does not fall back to the default locale if locale has no column" do
257
- I18n.locale = :ru
258
- expect(post.title).to be_nil
259
- end
260
-
261
- it "does not fall back to the default locale if blank" do
262
- I18n.locale = :sv
263
- post.title_sv = " "
264
- expect(post.title).to be_nil
265
- end
266
-
267
- it "still falls back if called with fallback: :default" do
268
- I18n.locale = :ru
269
- expect(post.title(fallback: :default)).to eq "Halloa"
270
- end
271
- end
272
-
273
- context "when the translation was defined with fallback: :any" do
274
- before do
275
- Post.translates :title, fallback: :any
276
- I18n.default_locale = :en
277
- I18n.locale = :"pt-BR"
278
- end
279
-
280
- it "falls back to any locale, not just the default" do
281
- post = Post.new(title_en: "", title_pt_br: "", title_sv: "Hej")
282
- expect(post.title).to eq "Hej"
283
- end
284
-
285
- it "prefers the default locale" do
286
- post = Post.new(title_en: "Hello", title_pt_br: "", title_sv: "Hej")
287
- expect(post.title).to eq "Hello"
288
- end
289
- end
290
- end
291
-
292
- RSpec.describe Post, "#title=" do
293
- before do
294
- Post.translates :title
295
- end
296
-
297
- let(:post) { Post.new }
298
-
299
- it "assigns in the current locale" do
300
- I18n.locale = :sv
301
- post.title = "Hej"
302
- expect(post.title_sv).to eq "Hej"
303
- end
304
-
305
- it "handles dashed locales" do
306
- I18n.locale = :"pt-BR"
307
- post.title = "Olá"
308
- expect(post.title_pt_br).to eq "Olá"
309
- end
310
-
311
- it "raises if locale has no column" do
312
- I18n.locale = :ru
313
- expect {
314
- post.title = "Privet"
315
- }.to raise_error(NoMethodError, /title_ru/)
316
- end
317
- end
318
-
319
- RSpec.describe Post, "#title?" do
320
- before do
321
- Post.translates :title
322
- I18n.locale = :sv
323
- I18n.default_locale = :en
324
- end
325
-
326
- it "is true if the title is present, with fallback" do
327
- post = Post.new(title_sv: "", title_en: " ", title_pt_br: nil)
328
- expect(post.title?).to be false
329
-
330
- post.title_en = "Hello"
331
-
332
- expect(post.title?).to be true
333
- end
334
-
335
- context "when given a 'locale' argument" do
336
- it "is true if the title if present in that locale, with no fallback" do
337
- post = Post.new(title_sv: "Hej", title_en: "Halloa")
338
- I18n.default_locale = :en
339
- I18n.locale = :en
340
-
341
- expect(post.title?(locale: :sv)).to be true
342
-
343
- post.title_sv = nil
344
- expect(post.title?(locale: :sv)).to be false
345
- end
346
- end
347
- end
348
-
349
- RSpec.describe Post, ".human_attribute_name" do
350
- before do
351
- Post.translates :title
352
- I18n.locale = :sv
353
- end
354
-
355
- it "uses explicit translations if present" do
356
- expect(Post.human_attribute_name(:title_sv)).to eq "Svensk titel"
357
- end
358
-
359
- it "appends translated language name if present" do
360
- expect(Post.human_attribute_name(:title_en)).to eq "Titel (engelska)"
361
- end
362
-
363
- it "appends an abbreviation when language name is not translated" do
364
- expect(Post.human_attribute_name(:title_pt_br)).to eq "Titel (PT-BR)"
365
- end
366
-
367
- it "passes through the default behavior for untranslated attributes" do
368
- expect(Post.human_attribute_name(:title)).to eq "Titel"
369
- end
370
-
371
- it "passes through untranslated attributes even if the name suggests it's translated" do
372
- expect(Post.human_attribute_name(:body_sv)).to eq "Body sv"
373
- end
374
-
375
- # ActiveModel::Errors#full_messages passes in an ugly default.
376
-
377
- it "does not honor passed-in defaults for locale columns" do
378
- expect(Post.human_attribute_name(:title_en, default: "Title en")).to eq "Titel (engelska)"
379
- end
380
-
381
- it "passes through defaults" do
382
- expect(Post.human_attribute_name(:body_sv, default: "Boday")).to eq "Boday"
383
- end
384
- end