Sha256: 4ba4aef47eb972bf52db78fc28ceb9ad4aaf37c1817a857720550beebaaae5cf

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# To use this module you need to add a text column to your model. This will be
# used to determine in which locales the translations exist.
module Concerns
  module Translatable
    extend ActiveSupport::Concern

    included do
      serialize :locales, Array
      has_many :translations, as: :translatable, dependent: :destroy
      scope :within_locale, ->(locale) { where('locales LIKE ?', "%#{locale}%")}
    end

    def translation(locale = I18n.locale)
      @translation_collections = {} unless @translation_collections
      @translation_collections[locale.to_sym] ||= Concerns::Translatable::Collection.new(
        self, self.class.translation_config, locale
      )
    end

    def translatable?
      true
    end

    module ClassMethods
      def translatable_field(name)
        delegate name, to: :translation
        translation_config.add(name)
      end

      def translatable_fields(*args)
        args.each { |name| translatable_field(name) }
      end

      def translation_config
        @translation_config ||= Concerns::Translatable::Config.new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
udongo-0.1.0 app/models/concerns/translatable.rb