has_localization_table 0.3.7 → 0.3.8
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,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTI1M2E1MmY0ODY5NGE5YzU4Y2JiZGEzNGVmZmU1YjA0NDUzYWFiNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDQwNGY1ZTVmN2NlM2M0MjEyYjk5N2YzMzY0NjYxMzVlMTllOWMxNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzMyOTAwZmFlYjk3MmQ0YWE5Y2Q4NDZkMzA1NzIyM2U3YjJiMTNhOGI2Yzlj
|
10
|
+
NWUwMzc4YTRjNGY0ODU5ZGI2ZDYxMTRjOWYzYzY5ZDIzOWI0YTdlOGViMGFl
|
11
|
+
ZmIxZGIxMjhlMmZhOWY3MDUwNWQzOWI5MTI0N2ZiNjE1MWVkYjI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NWFkYmJlMzM1MTE5YWRjNDBjMzQ4NmU2OTVlMzBkNDk0MDU3MzY3NzdhMTA3
|
14
|
+
MmYzNmMzM2E1MDg4YWExNTY4MGVhNGQ0YmYzNzdlZDgzOTMzMGQ5YTFkMWZi
|
15
|
+
NzZkZmM4ZWM4ZmNmYWI0ODYxMmE0ZDUwNzg4NjE5ZGY4M2E4MDA=
|
@@ -1,8 +1,17 @@
|
|
1
1
|
module HasLocalizationTable
|
2
2
|
module ActiveRecord
|
3
3
|
module Attributes
|
4
|
-
def read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale)
|
5
|
-
|
4
|
+
def read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, options = {})
|
5
|
+
locale ||= HasLocalizationTable.current_locale
|
6
|
+
|
7
|
+
attribute_cache[attribute.to_sym][locale.id] ||= begin
|
8
|
+
attr = localization_association.detect{ |a| a.send(HasLocalizationTable.locale_foreign_key) == locale.id }.send(attribute) rescue nil
|
9
|
+
if options[:fallback] && !attr
|
10
|
+
fallback = options[:fallback].respond_to?(:call) ? options[:fallback].call(self) : options[:fallback]
|
11
|
+
attr = read_localized_attribute(attribute, fallback)
|
12
|
+
end
|
13
|
+
attr
|
14
|
+
end
|
6
15
|
end
|
7
16
|
|
8
17
|
def write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale)
|
@@ -22,7 +31,8 @@ module HasLocalizationTable
|
|
22
31
|
# Try to load a string for the given locale
|
23
32
|
# If that fails, try for the primary locale
|
24
33
|
raise ArgumentError, "wrong number of arguments (#{args.size} for 0 or 1)" unless args.size.between?(0, 1)
|
25
|
-
|
34
|
+
options = args.extract_options!
|
35
|
+
return read_localized_attribute($1, args.first, options) || read_localized_attribute($1, HasLocalizationTable.primary_locale, options)
|
26
36
|
else
|
27
37
|
raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1
|
28
38
|
return write_localized_attribute($1, args.first)
|
@@ -135,4 +135,26 @@ describe HasLocalizationTable do
|
|
135
135
|
aa.name.must_equal "French Name"
|
136
136
|
aa.description.must_equal "French Description"
|
137
137
|
end
|
138
|
+
|
139
|
+
describe 'when a fallback is provided' do
|
140
|
+
let(:es) { Locale.create!(name: 'Spanish') }
|
141
|
+
|
142
|
+
before do
|
143
|
+
HasLocalizationTable.config.current_locale = es
|
144
|
+
a.save!
|
145
|
+
HasLocalizationTable.config.current_locale = Locale.first
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should return the fallback locale's string" do
|
149
|
+
a.name(fallback: Locale.find(3)).must_equal 'Test'
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should evaluate a proc" do
|
153
|
+
a.name(fallback: -> * { Locale.find(3) }).must_equal 'Test'
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'should return a given locale when specified' do
|
157
|
+
a.name(Locale.find(3)).must_equal 'Test'
|
158
|
+
end
|
159
|
+
end
|
138
160
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_localization_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Vandersluis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
type: :runtime
|