Skip to content

Instantly share code, notes, and snippets.

@tomasc
Created July 11, 2018 17:20
Show Gist options
  • Save tomasc/40f4bd62541b79576711e2d1cc7058e3 to your computer and use it in GitHub Desktop.
Save tomasc/40f4bd62541b79576711e2d1cc7058e3 to your computer and use it in GitHub Desktop.
Localized patch
# 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