Sha256: 38a8cb46e8f5689f6188c0ca7284926d13bee1602bb160424921c81a8ef27e36

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 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
    include Concerns::Storable

    included do
      serialize :locales, Array

      after_save do
        locales = ::Store.where(
          storable_type: self.class,
          storable_id: self.id,
          name: self.class.translatable_fields_list
        ).pluck(:collection).uniq

        update_column :locales, locales
      end

      scope :with_locale, ->(locale) { where('locales LIKE ?', "%#{locale}%")}
    end

    def translatable?
      true
    end

    def translation(locale = I18n.locale)
      store(locale)
    end

    module ClassMethods
      def translatable_field(name, type = String, default = nil)
        delegate name, "#{name}=", to: :translation
        self.store_config.add name, type, default

        unless translatable_fields_list.include?(name.to_sym)
          translatable_fields_list << name.to_sym
        end
      end

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

      def translatable_fields_list
        @translatable_fields_list ||= []
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
udongo-1.0.3 app/models/concerns/translatable.rb
udongo-1.0.2 app/models/concerns/translatable.rb
udongo-1.0.1 app/models/concerns/translatable.rb
udongo-1.0.0 app/models/concerns/translatable.rb