Sha256: 26d4cbdbf49eb8bfbd0a71dac70102b43c496ba1a7e81fbb42b0508072139a03

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'active_support'
require 'active_model'
require 'serial_translator/version'
require 'serial_translator/serial_translator_validator'

module SerialTranslator
  extend ActiveSupport::Concern

  included do
    attr_writer :current_translation_locale, :translation_fallback
  end

  module ClassMethods
    attr_reader :serial_translator_attributes

    def serial_translator_for(*attributes)
      @serial_translator_attributes = attributes

      attributes.each do |attribute|
        serialize :"#{attribute}_translations", Hash

        define_method attribute do |locale = current_translation_locale|
          translations = translations_for(attribute)
          result = translations[locale] if translations[locale].present?
          result ||= translations.values.detect(&:present?) if translation_fallback?
          result
        end

        define_method "#{attribute}=" do |value|
          translations = translations_for(attribute)
          translations[current_translation_locale] = value
          __send__(:"#{attribute}_translations=", translations)
        end
      end
    end
  end

  def current_translation_locale
    (@current_translation_locale || I18n.locale).to_sym
  end

  def translation_fallback?
    @translation_fallback != false
  end

  def translated_locales
    result = []
    self.class.serial_translator_attributes.each do |attribute|
      translations = translations_for(attribute).select { |_, value| value.present? }
      result += translations.keys
    end
    result.uniq
  end

  def translations_for(attribute)
    __send__(:"#{attribute}_translations") || {}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serial_translator-0.0.1 lib/serial_translator.rb