Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5334 ensure localized demongoization works with symbol keys #5416

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/mongoid/fields/localized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ class Localized < Standard
#
# @return [ Object ] The value for the current locale.
def demongoize(object)
if object
type.demongoize(lookup(object))
end
return if object.nil?
type.demongoize(lookup(object))
end

# Is the field localized or not?
Expand Down Expand Up @@ -86,7 +85,10 @@ def lookup(object)
end
return value unless value.nil?
if fallbacks? && ::I18n.respond_to?(:fallbacks)
object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object.has_key?(loc) }]
fallback_key = ::I18n.fallbacks[locale].find do |loc|
object.key?(loc.to_s) || object.key?(loc)
end
object[fallback_key.to_s] || object[fallback_key]
p-mongo marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/mongoid/fields/localized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
end
end

context "when key is a symbol" do

let(:value) do
field.demongoize({ :de => "This is a test" })
end

it "returns the string from the set locale" do
expect(value).to eq("This is a test")
end
end

context "when the value does not exist" do

context "when not using fallbacks" do
Expand Down Expand Up @@ -142,6 +153,17 @@
end
end

context "when the fallback translation exists and is a symbol" do

let(:value) do
field.demongoize({ :es => "testing" })
end

it "returns the fallback translation" do
expect(value).to eq("testing")
end
end

context "when another fallback translation exists" do

let(:value) do
Expand Down