Sha256: 00c141acfbbdbdad4412ad5b0eec9a1302b19742250e613be94b27de2cdfeaea

Contents?: true

Size: 1.26 KB

Versions: 39

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.to_s,
          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

39 entries across 39 versions & 1 rubygems

Version Path
udongo-7.0.2 app/models/concerns/translatable.rb
udongo-7.0.1 app/models/concerns/translatable.rb
udongo-7.0.0 app/models/concerns/translatable.rb
udongo-6.6.3 app/models/concerns/translatable.rb
udongo-6.6.2 app/models/concerns/translatable.rb
udongo-6.6.1 app/models/concerns/translatable.rb
udongo-6.6.0 app/models/concerns/translatable.rb
udongo-6.5.2 app/models/concerns/translatable.rb
udongo-6.5.1 app/models/concerns/translatable.rb
udongo-6.5.0 app/models/concerns/translatable.rb
udongo-6.4.1 app/models/concerns/translatable.rb
udongo-6.4.0 app/models/concerns/translatable.rb
udongo-6.3.2 app/models/concerns/translatable.rb
udongo-6.3.1 app/models/concerns/translatable.rb
udongo-6.3.0 app/models/concerns/translatable.rb
udongo-6.2.1 app/models/concerns/translatable.rb
udongo-6.2.0 app/models/concerns/translatable.rb
udongo-6.1.0 app/models/concerns/translatable.rb
udongo-6.0.0 app/models/concerns/translatable.rb
udongo-5.9.0 app/models/concerns/translatable.rb