Sha256: 1f1525d65410d0fe9d5a7096997b8ee465178730d16552e11db2ffb9157e9b73

Contents?: true

Size: 1.78 KB

Versions: 38

Compression:

Stored size: 1.78 KB

Contents

# encoding: utf-8
module Mongoid
  module Fields
    class Localized < Standard

      # Demongoize the object based on the current locale. Will look in the
      # hash for the current locale.
      #
      # @example Get the demongoized value.
      #   field.demongoize({ "en" => "testing" })
      #
      # @param [ Hash ] object The hash of translations.
      #
      # @return [ Object ] The value for the current locale.
      #
      # @since 2.3.0
      def demongoize(object)
        if object
          type.demongoize(lookup(object))
        end
      end

      # Is the field localized or not?
      #
      # @example Is the field localized?
      #   field.localized?
      #
      # @return [ true, false ] If the field is localized.
      #
      # @since 2.3.0
      def localized?
        true
      end

      # Convert the provided string into a hash for the locale.
      #
      # @example Serialize the value.
      #   field.mongoize("testing")
      #
      # @param [ String ] object The string to convert.
      #
      # @return [ Hash ] The locale with string translation.
      #
      # @since 2.3.0
      def mongoize(object)
        { ::I18n.locale.to_s => type.mongoize(object) }
      end

      private

      # Lookup the value from the provided object.
      #
      # @api private
      #
      # @example Lookup the value.
      #   field.lookup({ "en" => "test" })
      #
      # @param [ Hash ] object The localized object.
      #
      # @return [ Object ] The object for the locale.
      #
      # @since 3.0.0
      def lookup(object)
        locale = ::I18n.locale
        if ::I18n.respond_to?(:fallbacks)
          object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object[loc] }]
        else
          object[locale.to_s]
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 5 rubygems

Version Path
mongoid-3.1.7 lib/mongoid/fields/localized.rb
mongoid-4.0.0.alpha2 lib/mongoid/fields/localized.rb
mongoid-4.0.0.alpha1 lib/mongoid/fields/localized.rb
mongoid-3.1.6 lib/mongoid/fields/localized.rb
sepastian-mongoid-rails4-4.0.1.alpha lib/mongoid/fields/localized.rb
sepastian-mongoid-rails4-4.0.0.alpha lib/mongoid/fields/localized.rb
mongoid-3.1.5 lib/mongoid/fields/localized.rb
mongoid_heroku_stable-4.0.0 lib/mongoid/fields/localized.rb
mongoid_rails4-4.0.0 lib/mongoid/fields/localized.rb
mongoid-3.1.4 lib/mongoid/fields/localized.rb
mongoid-3.1.3 lib/mongoid/fields/localized.rb
mongoid-3.1.2 lib/mongoid/fields/localized.rb
mongoid-3.0.23 lib/mongoid/fields/localized.rb
mongoid-3.1.1 lib/mongoid/fields/localized.rb
mongoid-3.1.0 lib/mongoid/fields/localized.rb
mongoid-3.0.22 lib/mongoid/fields/localized.rb
mongoid-3.0.21 lib/mongoid/fields/localized.rb
mongoid-3.0.20 lib/mongoid/fields/localized.rb
mongoid-3.0.19 lib/mongoid/fields/localized.rb
mongoid-3.0.18 lib/mongoid/fields/localized.rb