traco 3.1.3 → 3.1.4
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 +8 -8
- data/CHANGELOG.md +19 -1
- data/lib/traco/class_methods.rb +7 -7
- data/lib/traco/locale_fallbacks.rb +9 -13
- data/lib/traco/version.rb +1 -1
- data/spec/locale_fallbacks_spec.rb +17 -14
- data/spec/spec_helper_models.rb +4 -1
- data/spec/traco_spec.rb +15 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzMxZDEzNjdmODYxYjJmNWVkOGUxNzI4YWYwNjE0Zjc2OTE1OWIzNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjFmNjBhMDFhYzFhODgzYjkzYmFiYzJjZWQ4YzhlMTc0MTc1YWVkOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmNkMjExZjYwODBlOGIyMWU1ODhhNmI4NTRkMjIxNjY3M2ZlYTUxZjdhZTQ5
|
10
|
+
ZDU0MjJjNWRkZWFjZGZlMjg3ZDE1ZGVkZjY1NmNlZTk1ZTk5NTVmNmY3N2Mx
|
11
|
+
NzYzYzE2ZmZlNDkzYTg1ZWJkNzkyNWMwMTQ2NWMzNWRmNDIwMjk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDAxNjQ0NjM4NWY2ODcxYmFmZjA5ZTVlOTRkZTAwZjY4NDRjNGNhMGNmOTE4
|
14
|
+
NGE4NTQwYTFlODYzYjVhMDgzNWMwNTZlZmNkMjYzMjFjNTdjMTFlMmE0ODMy
|
15
|
+
MTA3OTMzZmIwMDkxMzYyZDcyZTIxNjE2NWQyMDBlYzNhYWVkYmI=
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 3.1.4
|
4
|
+
|
5
|
+
* Bugfix: restore sorting of `locale_columns` and `locales_for_attribute` to put default locale first, not current locale. Thanks to Leung Ho Kuen.
|
6
|
+
|
7
|
+
## 3.1.3
|
8
|
+
|
9
|
+
* ~20 time speedup thanks to optimizations by Andrii Malyshko.
|
10
|
+
|
11
|
+
Reading a Traco translated attribute used to be ~250x slower than an untranslated attribute; now it's down to ~10x slower.
|
12
|
+
|
13
|
+
## 3.1.2
|
14
|
+
|
15
|
+
* Bugfix: `.current_locale_column` handles dashed locales like "pt-BR" correctly. Thanks to Leung Ho Kuen.
|
16
|
+
|
17
|
+
## 3.1.1
|
18
|
+
|
19
|
+
* Bugfix around fallbacks and memoization. Thanks to Leung Ho Kuen.
|
20
|
+
|
3
21
|
## 3.1.0
|
4
22
|
|
5
23
|
* Introduce `.current_locale_column`, e.g. `Post.current_locale_column(:title) # => :title_sv`.
|
@@ -18,7 +36,7 @@
|
|
18
36
|
|
19
37
|
## 2.0.0
|
20
38
|
|
21
|
-
* Backwards incompatible: for dashed locales like
|
39
|
+
* Backwards incompatible: for dashed locales like "pt-BR", the column names are now expected to end in e.g. `_pt_br`, not `_pt-BR`.
|
22
40
|
|
23
41
|
## 1.3.0
|
24
42
|
|
data/lib/traco/class_methods.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
module Traco
|
2
2
|
module ClassMethods
|
3
3
|
def locales_for_attribute(attribute)
|
4
|
-
traco_cache(attribute, LocaleFallbacks::
|
5
|
-
end
|
6
|
-
|
7
|
-
# Consider this method internal.
|
8
|
-
def _locale_columns_for_attribute(attribute, fallback)
|
9
|
-
traco_cache(attribute, fallback).values
|
4
|
+
traco_cache(attribute, LocaleFallbacks::DEFAULT_FIRST_FALLBACK).keys
|
10
5
|
end
|
11
6
|
|
12
7
|
def locale_columns(*attributes)
|
13
8
|
attributes.each_with_object([]) do |attribute, columns|
|
14
|
-
columns.concat(_locale_columns_for_attribute(attribute, LocaleFallbacks::
|
9
|
+
columns.concat(_locale_columns_for_attribute(attribute, LocaleFallbacks::DEFAULT_FIRST_FALLBACK))
|
15
10
|
end
|
16
11
|
end
|
17
12
|
|
13
|
+
# Consider this method internal.
|
14
|
+
def _locale_columns_for_attribute(attribute, fallback)
|
15
|
+
traco_cache(attribute, fallback).values
|
16
|
+
end
|
17
|
+
|
18
18
|
def current_locale_column(attribute)
|
19
19
|
Traco.column(attribute, I18n.locale)
|
20
20
|
end
|
@@ -6,6 +6,7 @@ module Traco
|
|
6
6
|
DEFAULT_FALLBACK = :default,
|
7
7
|
ANY_FALLBACK = :any,
|
8
8
|
NO_FALLBACK = false,
|
9
|
+
DEFAULT_FIRST_FALLBACK = :default_first,
|
9
10
|
]
|
10
11
|
|
11
12
|
attr_reader :fallback_option
|
@@ -17,23 +18,18 @@ module Traco
|
|
17
18
|
@available_locales = I18n.available_locales.sort
|
18
19
|
end
|
19
20
|
|
20
|
-
def [](
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def [](current_locale)
|
22
|
+
case fallback_option
|
23
|
+
when DEFAULT_FALLBACK then [ current_locale, @default_locale ]
|
24
|
+
when ANY_FALLBACK then [ current_locale, @default_locale, *@available_locales ].uniq
|
25
|
+
when NO_FALLBACK then [ current_locale ]
|
26
|
+
when DEFAULT_FIRST_FALLBACK then [ @default_locale, *@available_locales ].uniq
|
27
|
+
else raise "Unknown fallback." # Should never get here.
|
28
|
+
end
|
25
29
|
end
|
26
30
|
|
27
31
|
private
|
28
32
|
|
29
|
-
def include_default_locale?
|
30
|
-
[ DEFAULT_FALLBACK, ANY_FALLBACK ].include?(fallback_option)
|
31
|
-
end
|
32
|
-
|
33
|
-
def include_available_locales?
|
34
|
-
ANY_FALLBACK == fallback_option
|
35
|
-
end
|
36
|
-
|
37
33
|
def validate_option(fallback_option)
|
38
34
|
if OPTIONS.include?(fallback_option)
|
39
35
|
fallback_option
|
data/lib/traco/version.rb
CHANGED
@@ -2,23 +2,15 @@ require "spec_helper"
|
|
2
2
|
require "traco/locale_fallbacks"
|
3
3
|
|
4
4
|
describe Traco::LocaleFallbacks do
|
5
|
-
describe "
|
6
|
-
it "
|
7
|
-
expect {
|
8
|
-
Traco::LocaleFallbacks.new(:default)
|
9
|
-
Traco::LocaleFallbacks.new(:any)
|
10
|
-
Traco::LocaleFallbacks.new(false)
|
11
|
-
}.not_to raise_error
|
12
|
-
end
|
13
|
-
|
14
|
-
it "raises ArgumentError if invalid argument passed in" do
|
15
|
-
expect { Traco::LocaleFallbacks.new(:invalid) }.to raise_error(ArgumentError)
|
5
|
+
describe ".new" do
|
6
|
+
it "raises ArgumentError if an unknown argument passed in" do
|
7
|
+
expect { Traco::LocaleFallbacks.new(:foo) }.to raise_error(ArgumentError)
|
16
8
|
expect { Traco::LocaleFallbacks.new(nil) }.to raise_error(ArgumentError)
|
17
9
|
end
|
18
10
|
end
|
19
11
|
|
20
12
|
describe "#[]" do
|
21
|
-
context "with the :default option" do
|
13
|
+
context "with the ':default' option" do
|
22
14
|
it "returns given locale, then default locale" do
|
23
15
|
I18n.default_locale = :en
|
24
16
|
subject = Traco::LocaleFallbacks.new(:default)
|
@@ -26,20 +18,31 @@ describe Traco::LocaleFallbacks do
|
|
26
18
|
end
|
27
19
|
end
|
28
20
|
|
29
|
-
context "with the :any option" do
|
21
|
+
context "with the ':any' option" do
|
30
22
|
it "returns given locale, then default locale, then remaining available locales alphabetically" do
|
31
23
|
I18n.default_locale = :en
|
32
24
|
I18n.available_locales = [ :en, :sv, :uk, :de ]
|
25
|
+
|
33
26
|
subject = Traco::LocaleFallbacks.new(:any)
|
34
27
|
expect(subject[:sv]).to eq [ :sv, :en, :de, :uk ]
|
35
28
|
end
|
36
29
|
end
|
37
30
|
|
38
|
-
context "with the false option" do
|
31
|
+
context "with the 'false' option" do
|
39
32
|
it "returns only given locale" do
|
40
33
|
subject = Traco::LocaleFallbacks.new(false)
|
41
34
|
expect(subject[:sv]).to eq [ :sv ]
|
42
35
|
end
|
43
36
|
end
|
37
|
+
|
38
|
+
context "with the ':default_first' option" do
|
39
|
+
it "returns default locale, then remaining available locales alphabetically" do
|
40
|
+
I18n.default_locale = :uk
|
41
|
+
I18n.available_locales = [ :en, :sv, :uk, :de ]
|
42
|
+
|
43
|
+
subject = Traco::LocaleFallbacks.new(:default_first)
|
44
|
+
expect(subject[:sv]).to eq [ :uk, :de, :en, :sv ]
|
45
|
+
end
|
46
|
+
end
|
44
47
|
end
|
45
48
|
end
|
data/spec/spec_helper_models.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
RSpec.configure do |config|
|
2
|
-
# Clear class state before each spec.
|
3
2
|
config.before(:each) do
|
3
|
+
# Clear class state before each spec.
|
4
4
|
Object.send(:remove_const, 'Post')
|
5
5
|
Object.send(:remove_const, 'SubPost')
|
6
6
|
load "app/post.rb"
|
7
|
+
|
8
|
+
# Known state.
|
9
|
+
I18n.default_locale = :sv
|
7
10
|
end
|
8
11
|
end
|
9
12
|
|
data/spec/traco_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require "spec_helper_models"
|
4
3
|
require "spec_helper"
|
4
|
+
require "spec_helper_models"
|
5
5
|
require "traco"
|
6
6
|
|
7
7
|
describe Traco, ".split_localized_column" do
|
@@ -80,44 +80,46 @@ end
|
|
80
80
|
describe Post, ".locales_for_attribute" do
|
81
81
|
before do
|
82
82
|
Post.translates :title
|
83
|
-
I18n.
|
83
|
+
I18n.default_locale = :"pt-BR"
|
84
|
+
I18n.locale = :en
|
84
85
|
end
|
85
86
|
|
86
|
-
it "lists the
|
87
|
-
expect(Traco).to receive(:locale_with_fallbacks).with(:"pt-BR", :any).and_return([:"pt-BR", :en, :sv])
|
87
|
+
it "lists the locales for that attribute, default locale first and then alphabetically" do
|
88
88
|
expect(Post.locales_for_attribute(:title)).to eq [ :"pt-BR", :en, :sv ]
|
89
89
|
end
|
90
90
|
|
91
91
|
it "doesn't include a locale if there's no corresponding column for it" do
|
92
|
-
|
93
|
-
|
92
|
+
I18n.available_locales += [ :ru ]
|
93
|
+
|
94
|
+
expect(Post.locales_for_attribute(:title)).not_to include(:ru)
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
97
98
|
describe Post, ".locale_columns" do
|
98
99
|
before do
|
99
100
|
Post.translates :title
|
100
|
-
I18n.
|
101
|
+
I18n.default_locale = :"pt-BR"
|
102
|
+
I18n.locale = :en
|
101
103
|
end
|
102
104
|
|
103
|
-
it "lists the columns-with-locale for
|
104
|
-
expect(Traco).to receive(:locale_with_fallbacks).with(:"pt-BR", :any).and_return([:"pt-BR", :en, :sv])
|
105
|
+
it "lists the columns-with-locale for that attribute, default locale first and then alphabetically" do
|
105
106
|
expect(Post.locale_columns(:title)).to eq [
|
106
107
|
:title_pt_br, :title_en, :title_sv
|
107
108
|
]
|
108
109
|
end
|
109
110
|
|
110
111
|
it "doesn't include a column-with-locale if it doesn't exist" do
|
111
|
-
|
112
|
-
|
112
|
+
I18n.available_locales += [ :ru ]
|
113
|
+
|
114
|
+
expect(Post.locale_columns(:title)).not_to include(:title_ru)
|
113
115
|
end
|
114
116
|
|
115
117
|
it "supports multiple attributes" do
|
116
118
|
Post.translates :body
|
117
|
-
|
119
|
+
|
118
120
|
expect(Post.locale_columns(:body, :title)).to eq [
|
119
121
|
:body_pt_br, :body_en, :body_sv,
|
120
|
-
:title_pt_br, :title_en, :title_sv
|
122
|
+
:title_pt_br, :title_en, :title_sv,
|
121
123
|
]
|
122
124
|
end
|
123
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|