Created
July 11, 2018 17:20
-
-
Save tomasc/40f4bd62541b79576711e2d1cc7058e3 to your computer and use it in GitHub Desktop.
Localized patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# makes sure that | |
# • we fallback on all :blank? values | |
# • an only on :present? values | |
module Mongoid | |
module Fields | |
module LocalizedPatch | |
def lookup(object) | |
locale = ::I18n.locale | |
value = if object.key?(locale.to_s) | |
object[locale.to_s] | |
elsif object.key?(locale) | |
object[locale] | |
end | |
return value unless value.blank? | |
if fallbacks? && ::I18n.respond_to?(:fallbacks) | |
object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object.has_key?(loc) && object[loc].present? }] | |
end | |
end | |
end | |
end | |
end | |
Mongoid::Fields::Localized.send(:prepend, Mongoid::Fields::LocalizedPatch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment