has_localization_table 0.3.8 → 0.3.9
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/lib/has_localization_table.rb +12 -0
- data/lib/has_localization_table/active_record/attributes.rb +3 -2
- data/lib/has_localization_table/config.rb +2 -0
- data/lib/has_localization_table/version.rb +1 -1
- data/spec/active_record/attributes_spec.rb +11 -0
- data/spec/has_localization_table_spec.rb +22 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmUyZDNjN2QwNTJmNDg0YWIzNjM3MjJmNzY4NWE5N2MxNTMyYWRhNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDgxYThkMzM3NGVkM2ZkZWNkNWFiYzAyYjcyM2VhMzEzN2ZkNDBhYw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWE3ZjVhY2I1YTE2YWQyYTAyMzY2NWUyNGI0YTc3Yjc5ODA1OTRmMTcxODky
|
10
|
+
OGE3MGViOWZhZDEyZmFmZDIwODAyYTE2MDY3MWY4MTk3ZjgyMTU4OWIyNjlm
|
11
|
+
N2RjZjk5MmRlZWU2YTllMWNjN2NjZWVjZTI0M2UyNjkyNTNiODY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzNjZDg2ZDg5NzYxOTlkNjI5Y2QzNGYyNjIwYWQ0ZDNmNzE1M2EyNTc0M2Y5
|
14
|
+
YmI4YmMyYWVkMWUxY2RiYTVhNTllMmJlMjU3ZGM3YTI2ZGQ3OTk0NGI2NGQ5
|
15
|
+
NmU1MDM0OTQ0OGRiMmQ5ZjUzZjJjNmQ0YmVmMmEyODAxYzZlNDc=
|
@@ -12,4 +12,16 @@ module HasLocalizationTable
|
|
12
12
|
val
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
def self.with_options(options, &block)
|
17
|
+
# Ugly but we need to make sure we don't clobber the existing configuration
|
18
|
+
old_config = @config.dup
|
19
|
+
old_config.instance_variable_set('@_config', @config.config.dup)
|
20
|
+
|
21
|
+
@config.config.merge!(options.slice(*HasLocalizationTable.config.config.keys))
|
22
|
+
|
23
|
+
yield
|
24
|
+
|
25
|
+
@config = old_config
|
26
|
+
end
|
15
27
|
end
|
@@ -6,8 +6,9 @@ module HasLocalizationTable
|
|
6
6
|
|
7
7
|
attribute_cache[attribute.to_sym][locale.id] ||= begin
|
8
8
|
attr = localization_association.detect{ |a| a.send(HasLocalizationTable.locale_foreign_key) == locale.id }.send(attribute) rescue nil
|
9
|
-
if options
|
10
|
-
fallback = options
|
9
|
+
if options.fetch(:fallback, HasLocalizationTable.config.fallback_locale) && !attr
|
10
|
+
fallback = options.fetch(:fallback, HasLocalizationTable.config.fallback_locale)
|
11
|
+
fallback = fallback.call(self) if fallback.respond_to?(:call)
|
11
12
|
attr = read_localized_attribute(attribute, fallback)
|
12
13
|
end
|
13
14
|
attr
|
@@ -28,6 +28,7 @@ module HasLocalizationTable
|
|
28
28
|
config_accessor :class_suffix
|
29
29
|
config_accessor :default_association_name
|
30
30
|
config_accessor :create_has_one_by_default # Should a has_one association be automatically created?
|
31
|
+
config_accessor :fallback_locale
|
31
32
|
end
|
32
33
|
|
33
34
|
# this is ugly. why can't we pass the default value to config_accessor...?
|
@@ -40,5 +41,6 @@ module HasLocalizationTable
|
|
40
41
|
config.current_locale = ->{ config.locale_class.constantize.first }
|
41
42
|
config.all_locales = ->{ config.locale_class.constantize.all }
|
42
43
|
config.create_has_one_by_default = true
|
44
|
+
config.fallback_locale = nil
|
43
45
|
end
|
44
46
|
end
|
@@ -156,5 +156,16 @@ describe HasLocalizationTable do
|
|
156
156
|
it 'should return a given locale when specified' do
|
157
157
|
a.name(Locale.find(3)).must_equal 'Test'
|
158
158
|
end
|
159
|
+
|
160
|
+
it 'should not evaluate a proc if the fallback is not required' do
|
161
|
+
HasLocalizationTable.config.current_locale = es
|
162
|
+
a.name(fallback: -> * { raise ArgumentError }).must_equal 'Test'
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should use the fallback specified in configuration' do
|
166
|
+
HasLocalizationTable.with_options(fallback_locale: -> * { Locale.find(3) }) do
|
167
|
+
a.name.must_equal 'Test'
|
168
|
+
end
|
169
|
+
end
|
159
170
|
end
|
160
171
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HasLocalizationTable do
|
4
|
+
describe '.with_options' do
|
5
|
+
before do
|
6
|
+
HasLocalizationTable.configure do |c|
|
7
|
+
c.locale_class = 'Locale'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should make the options available within the block' do
|
12
|
+
HasLocalizationTable.with_options(locale_class: 'Language') do
|
13
|
+
HasLocalizationTable.locale_class.must_equal 'Language'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should revert to the original option values after the block' do
|
18
|
+
HasLocalizationTable.with_options(locale_class: 'Language') {}
|
19
|
+
HasLocalizationTable.locale_class.must_equal 'Locale'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Vandersluis
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- spec/active_record/relation_spec.rb
|
112
112
|
- spec/active_record/validations_spec.rb
|
113
113
|
- spec/active_record_spec.rb
|
114
|
+
- spec/has_localization_table_spec.rb
|
114
115
|
- spec/spec_helper.rb
|
115
116
|
- spec/support/setup.rb
|
116
117
|
homepage: https://github.com/dvandersluis/has_localization_table
|
@@ -144,6 +145,7 @@ test_files:
|
|
144
145
|
- spec/active_record/relation_spec.rb
|
145
146
|
- spec/active_record/validations_spec.rb
|
146
147
|
- spec/active_record_spec.rb
|
148
|
+
- spec/has_localization_table_spec.rb
|
147
149
|
- spec/spec_helper.rb
|
148
150
|
- spec/support/setup.rb
|
149
151
|
has_rdoc:
|