Sha256: a83cd9b2dbb2e0adddf41163e000b40e7b127592bf548be0ab1d2ab55d7628d9

Contents?: true

Size: 881 Bytes

Versions: 2

Compression:

Stored size: 881 Bytes

Contents

module ActiveData
  module Model
    module Localizable
      extend ActiveSupport::Concern

      included do
      end

      module ClassMethods
        def fallbacks locale
          ::I18n.respond_to?(:fallbacks) ? ::I18n.fallbacks[locale] : [locale]
        end

        def locale
          I18n.locale
        end
      end

      def read_localized_attribute name, locale = self.class.locale
        translations = read_attribute(name.to_s)
        translations[self.class.fallbacks(locale).detect { |fallback| translations[fallback.to_s] }.to_s]
      end
      alias_method :read_localized_attribute_before_type_cast, :read_localized_attribute

      def write_localized_attribute name, value, locale = self.class.locale
        translations = read_attribute(name.to_s)
        write_attribute(name, translations.merge(locale.to_s => value))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_data-0.3.0 lib/active_data/model/localizable.rb
active_data-0.2.0 lib/active_data/model/localizable.rb