has_localization_table 0.3.12 → 0.3.13

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
- MzFkNDcyZjc2NTQ0ODU3ZDFjODk1MDYwMTg3Yjk5ZGVkYmMyYWUyMg==
4
+ N2ZlMDQ3ZWRiY2EwYzhmZWU1YmVjYjcwNzk1ZDNlY2VkZDYwNDhjYg==
5
5
  data.tar.gz: !binary |-
6
- NDRkNDMxYTU1NTJkNzgyMTU2OTI0Y2IxNDY0N2QwYzRmZjlmYmJiOA==
6
+ OGFhMjlkZWU0ZTdiMzA3OGEwNTMzYzAxYjkzYTgxNmFkODVmM2RlNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTc0YTRjZmU4MDQ4ZWY5MTEwNzEwMDFmMzRlZjIyNWY3ZTQ3ZmM4ZDYzYzli
10
- NDcwZTU0NTAyNDY3NWMxOTZhZjJmOTdlZTIwNTA4NmJmOTgwODBhZTc3N2Qy
11
- NTdiZTc5YjVhNWU4ZWQxNGFhNGFjZmFkMmE3NDg4NDJkMDg1YzE=
9
+ ODQyMTUzOGY0YjRjNGY0YTY0YzVlMTZhZTQyN2EzMDhiNzZlMmZhZDZmYmE5
10
+ NmMwOTAyNTMzNWY4NDRkMmZhOGMwMGVlMWJjOWMzZDEyY2ZiNzllOWU1N2Y3
11
+ NWJhZWJiNGNhMGIzNGRjNmQ2MWE5ZDI1M2ZiZWZlNTBjYTU5NmM=
12
12
  data.tar.gz: !binary |-
13
- NDY1YzI2MDQ3YjgzNmQzYjlhM2JmMjA5ODYxODM4YzcxNzg3OTVlNmI3Njcw
14
- MjhmMmUxOTc3MzFlZTA0NjRjYjk5NTM5YTM4OTNlNTZiNDI3YjYxOGU5ZjM5
15
- NTExZWJmZmU4MThlNmZiNDE3Yzg2MDRjYjc4YTE0M2YyNjJmZGY=
13
+ ODQ4MGU3OWE4ZjM5Mjk3OGE5YTJlNDU3NWYyYTg1M2Y0YTIyZmQ5M2NmYjNm
14
+ MjE3ZmQyMGViMDRkMmFkMWExOWJiNGM2YTBkZDc5NDE1ZDJhYWM2MDg2ZTc1
15
+ MWU4ZDFkMDhiZmJjYjIyNzI4MGQyMWE4N2EyZDllOWM5NmVmZmQ=
@@ -5,12 +5,17 @@ module HasLocalizationTable
5
5
  locale ||= HasLocalizationTable.current_locale
6
6
 
7
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.fetch(:fallback, HasLocalizationTable.config.fallback_locale) && !attr
10
- fallback = options.fetch(:fallback, HasLocalizationTable.config.fallback_locale)
8
+ attr = localization_association.detect{ |a| a.send(HasLocalizationTable.locale_foreign_key) == locale.id }.send(attribute) rescue ''
9
+ attr ||= '' # if the attribute somehow is nil, change it to a blank string so we're always dealing with strings
10
+
11
+ fallback = options.fetch(:fallback, HasLocalizationTable.config.fallback_locale)
12
+
13
+ if fallback && attr.blank?
11
14
  fallback = fallback.call(self) if fallback.respond_to?(:call)
12
- attr = read_localized_attribute(attribute, fallback)
15
+
16
+ return read_localized_attribute(attribute, fallback) unless fallback == locale
13
17
  end
18
+
14
19
  attr
15
20
  end
16
21
  end
@@ -50,6 +55,7 @@ module HasLocalizationTable
50
55
  end
51
56
 
52
57
  private
58
+
53
59
  def attribute_cache
54
60
  @localization_attribute_cache ||= localized_attributes.inject({}) { |memo, attr| memo[attr] = {}; memo }
55
61
  end
@@ -41,6 +41,6 @@ module HasLocalizationTable
41
41
  config.current_locale = ->{ config.locale_class.constantize.first }
42
42
  config.all_locales = ->{ config.locale_class.constantize.all }
43
43
  config.create_has_one_by_default = true
44
- config.fallback_locale = nil
44
+ config.fallback_locale = -> * { HasLocalizationTable.primary_locale }
45
45
  end
46
46
  end
@@ -1,3 +1,3 @@
1
1
  module HasLocalizationTable
2
- VERSION = "0.3.12"
2
+ VERSION = "0.3.13"
3
3
  end
@@ -6,7 +6,7 @@ describe HasLocalizationTable do
6
6
  HasLocalizationTable.configure do |c|
7
7
  c.primary_locale = Locale.first
8
8
  c.current_locale = Locale.first
9
- c.all_locales = Locale.all
9
+ c.all_locales = -> { Locale.all }
10
10
  end
11
11
 
12
12
  Object.send(:remove_const, :Article) rescue nil
@@ -50,7 +50,7 @@ describe HasLocalizationTable do
50
50
 
51
51
  it "should use the current locale when setting" do
52
52
  a
53
-
53
+
54
54
  HasLocalizationTable.configure do |c|
55
55
  c.current_locale = Locale.last
56
56
  end
@@ -145,16 +145,26 @@ describe HasLocalizationTable do
145
145
  HasLocalizationTable.config.current_locale = Locale.first
146
146
  end
147
147
 
148
- it "should return the fallback locale's string" do
149
- a.name(fallback: Locale.find(3)).must_equal 'Test'
148
+ it "should return the fallback locale's string when there isn't a string for the current locale" do
149
+ a.name(fallback: es).must_equal 'Test'
150
+ end
151
+
152
+ it "should return the fallback locale's string when the string for the current locale is blank" do
153
+ a.update_attributes!(name: '', description: 'Description')
154
+ a.name(fallback: es).must_equal 'Test'
155
+ end
156
+
157
+ it 'should return a blank string if neither locale has a string' do
158
+ HasLocalizationTable.with_options(current_locale: es) { a.update_attributes!(name: '') }
159
+ a.name(fallback: es).must_be_empty
150
160
  end
151
161
 
152
162
  it "should evaluate a proc" do
153
- a.name(fallback: -> * { Locale.find(3) }).must_equal 'Test'
163
+ a.name(fallback: -> * { es }).must_equal 'Test'
154
164
  end
155
165
 
156
166
  it 'should return a given locale when specified' do
157
- a.name(Locale.find(3)).must_equal 'Test'
167
+ a.name(es).must_equal 'Test'
158
168
  end
159
169
 
160
170
  it 'should not evaluate a proc if the fallback is not required' do
@@ -163,9 +173,50 @@ describe HasLocalizationTable do
163
173
  end
164
174
 
165
175
  it 'should use the fallback specified in configuration' do
166
- HasLocalizationTable.with_options(fallback_locale: -> * { Locale.find(3) }) do
176
+ HasLocalizationTable.with_options(fallback_locale: -> * { es }) do
167
177
  a.name.must_equal 'Test'
168
178
  end
169
179
  end
170
180
  end
181
+
182
+ describe 'when a fallback is not provided' do
183
+ describe 'when the primary locale is the current locale' do
184
+ it 'should return a blank string when the localization is nil' do
185
+ a = Article.new(name: nil)
186
+ a.name.must_be_empty
187
+ end
188
+
189
+ it 'should return a blank string if there is no localization' do
190
+ a = Article.create!
191
+ a.name.must_be_empty
192
+ end
193
+
194
+ it 'should return a blank string if the localization is blank' do
195
+ a.update_attributes!(name: '')
196
+ a.name.must_be_empty
197
+ end
198
+ end
199
+
200
+ describe 'when the primary locale is different than the current locale' do
201
+ let(:es) { Locale.create!(name: 'Spanish') }
202
+
203
+ before do
204
+ HasLocalizationTable.config.primary_locale = HasLocalizationTable.config.current_locale = es
205
+
206
+ a.save!
207
+
208
+ HasLocalizationTable.config.current_locale = Locale.first
209
+ a.update_attributes!(name: '', description: 'Description')
210
+ end
211
+
212
+ it 'should return the primary locale string if the current locale string is blank' do
213
+ a.name.must_equal 'Test'
214
+ end
215
+
216
+ it 'should return a blank string if neither locale has a string' do
217
+ HasLocalizationTable.with_options(current_locale: HasLocalizationTable.primary_locale) { a.update_attributes!(name: '') }
218
+ a.name.must_be_empty
219
+ end
220
+ end
221
+ end
171
222
  end
@@ -21,7 +21,7 @@ describe HasLocalizationTable do
21
21
 
22
22
  it 'should revert nil values' do
23
23
  HasLocalizationTable.with_options(fallback_locale: -> { }) { }
24
- HasLocalizationTable.fallback_locale.must_be_nil
24
+ HasLocalizationTable.fallback_locale.must_equal HasLocalizationTable.primary_locale
25
25
  end
26
26
 
27
27
  it 'should revert values if an exception was raised in the block' do
@@ -33,7 +33,7 @@ describe HasLocalizationTable do
33
33
  rescue => e
34
34
  end
35
35
 
36
- HasLocalizationTable.config.fallback_locale.must_be_nil
36
+ HasLocalizationTable.fallback_locale.must_equal HasLocalizationTable.primary_locale
37
37
  end
38
38
  end
39
39
  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.12
4
+ version: 0.3.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Vandersluis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  type: :runtime