traco 2.2.0 → 3.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 +4 -4
- data/README.md +2 -0
- data/lib/traco/attribute_setup.rb +1 -1
- data/lib/traco/localized_reader.rb +16 -2
- data/lib/traco/version.rb +1 -1
- data/spec/traco_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6593f9b00040b46726794a81103b204f708badd
|
4
|
+
data.tar.gz: 65bb9ab64475d9935ecdee1adf0369898d7c8a90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcb768c9e6f93a6f2682875c87e5b028c6c5168f4d044e5970a3d3ecf57a768e39cda38a67ca381f31eb4da066b7379fdf9fb71830d2da9e4d506f0392670196
|
7
|
+
data.tar.gz: 5083c41205a6d2c144b6dceb585b8fa88ee9cc66e133c8e86d567c3fa968e89000c7dba315a4c28e7de9605f841b0faa83b2969a760c15e07475b074ea8657e2
|
data/README.md
CHANGED
@@ -94,6 +94,8 @@ You can specify e.g. `translates :title, fallback: :any` to fall back first to t
|
|
94
94
|
|
95
95
|
You can override the default fallback strategy with a parameter passed to the reader: `post.title(fallback: :any)`.
|
96
96
|
|
97
|
+
If you need to declare the default locale fallback, do `post.title(fallback: :default)`.
|
98
|
+
|
97
99
|
|
98
100
|
### Overriding methods
|
99
101
|
|
@@ -49,7 +49,7 @@ module Traco
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def define_localized_reader(attribute, options)
|
52
|
-
default_fallback = options.fetch(:fallback,
|
52
|
+
default_fallback = options.fetch(:fallback, Traco::LocalizedReader::DEFAULT_FALLBACK)
|
53
53
|
|
54
54
|
custom_define_method(attribute) do |method_opts = {}|
|
55
55
|
fallback = method_opts.fetch(:fallback, default_fallback)
|
@@ -1,9 +1,16 @@
|
|
1
1
|
module Traco
|
2
2
|
class LocalizedReader
|
3
|
+
FALLBACK_OPTIONS = [
|
4
|
+
DEFAULT_FALLBACK = :default,
|
5
|
+
ANY_FALLBACK = :any,
|
6
|
+
NO_FALLBACK = false,
|
7
|
+
]
|
8
|
+
|
3
9
|
def initialize(record, attribute, options)
|
4
10
|
@record = record
|
5
11
|
@attribute = attribute
|
6
12
|
@fallback = options[:fallback]
|
13
|
+
validate_fallback
|
7
14
|
end
|
8
15
|
|
9
16
|
def value
|
@@ -24,13 +31,20 @@ module Traco
|
|
24
31
|
def locale_chain
|
25
32
|
chain = []
|
26
33
|
chain << I18n.locale
|
27
|
-
chain << I18n.default_locale if @fallback
|
28
|
-
chain += I18n.available_locales if @fallback ==
|
34
|
+
chain << I18n.default_locale if [DEFAULT_FALLBACK, ANY_FALLBACK].include?(@fallback)
|
35
|
+
chain += I18n.available_locales if @fallback == ANY_FALLBACK
|
29
36
|
chain.map { |locale| Traco.locale_suffix(locale) }
|
30
37
|
end
|
31
38
|
|
32
39
|
def locales_for_attribute
|
33
40
|
@record.class.locales_for_attribute(@attribute)
|
34
41
|
end
|
42
|
+
|
43
|
+
def validate_fallback
|
44
|
+
unless FALLBACK_OPTIONS.include?(@fallback)
|
45
|
+
valids = FALLBACK_OPTIONS.map(&:inspect).join(", ")
|
46
|
+
raise "Unsupported fallback: #{@fallback.inspect} (expected one of #{valids})"
|
47
|
+
end
|
48
|
+
end
|
35
49
|
end
|
36
50
|
end
|
data/lib/traco/version.rb
CHANGED
data/spec/traco_spec.rb
CHANGED
@@ -172,9 +172,9 @@ describe Post, "#title" do
|
|
172
172
|
post.title.should be_nil
|
173
173
|
end
|
174
174
|
|
175
|
-
it "still falls back if called with fallback:
|
175
|
+
it "still falls back if called with fallback: :default" do
|
176
176
|
I18n.locale = :ru
|
177
|
-
post.title(fallback:
|
177
|
+
post.title(fallback: :default).should == "Halloa"
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|