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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b558d968305b0326939037fb16d4a871092c39cc
4
- data.tar.gz: 204f5478a41436d55d0c19954ce0ef971f86dc45
3
+ metadata.gz: c6593f9b00040b46726794a81103b204f708badd
4
+ data.tar.gz: 65bb9ab64475d9935ecdee1adf0369898d7c8a90
5
5
  SHA512:
6
- metadata.gz: 4e4745b20173434cb983d53bb2cb18074f398685f2fea9efb011e41f95842fe3108644fa11a0e971752bd30db436219d45455839d969aed988ce4936414ed28d
7
- data.tar.gz: ccc4cdbbccf49ff129f2d9e634e1a9aab80650a1600d820f82cfabf8a2977ab669496fdeca1f3ffd7749f2abfc3eba6351fdca74773e8115245a503f35db871b
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, true)
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 == :any
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
@@ -1,3 +1,3 @@
1
1
  module Traco
2
- VERSION = "2.2.0"
2
+ VERSION = "3.0.0"
3
3
  end
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: true" do
175
+ it "still falls back if called with fallback: :default" do
176
176
  I18n.locale = :ru
177
- post.title(fallback: true).should == "Halloa"
177
+ post.title(fallback: :default).should == "Halloa"
178
178
  end
179
179
  end
180
180
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traco
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh