Skip to content

Commit ecb65b2

Browse files
authored
MONGOID-5334 ensure localized demongoization works with symbol keys (#5416)
* MONGOID-5334 ensure localized demongoization works with symbol keys * MONGOID-5334 add type check * MONGOID-5334 change back type check
1 parent 90e1711 commit ecb65b2

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/mongoid/fields/localized.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ class Localized < Standard
1414
#
1515
# @return [ Object ] The value for the current locale.
1616
def demongoize(object)
17-
if object
18-
type.demongoize(lookup(object))
19-
end
17+
return if object.nil?
18+
type.demongoize(lookup(object))
2019
end
2120

2221
# Is the field localized or not?
@@ -86,7 +85,10 @@ def lookup(object)
8685
end
8786
return value unless value.nil?
8887
if fallbacks? && ::I18n.respond_to?(:fallbacks)
89-
object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object.has_key?(loc) }]
88+
fallback_key = ::I18n.fallbacks[locale].find do |loc|
89+
object.key?(loc.to_s) || object.key?(loc)
90+
end
91+
object[fallback_key.to_s] || object[fallback_key]
9092
end
9193
end
9294
end

spec/mongoid/fields/localized_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@
102102
end
103103
end
104104

105+
context "when key is a symbol" do
106+
107+
let(:value) do
108+
field.demongoize({ :de => "This is a test" })
109+
end
110+
111+
it "returns the string from the set locale" do
112+
expect(value).to eq("This is a test")
113+
end
114+
end
115+
105116
context "when the value does not exist" do
106117

107118
context "when not using fallbacks" do
@@ -142,6 +153,17 @@
142153
end
143154
end
144155

156+
context "when the fallback translation exists and is a symbol" do
157+
158+
let(:value) do
159+
field.demongoize({ :es => "testing" })
160+
end
161+
162+
it "returns the fallback translation" do
163+
expect(value).to eq("testing")
164+
end
165+
end
166+
145167
context "when another fallback translation exists" do
146168

147169
let(:value) do

0 commit comments

Comments
 (0)